How toLinux

How to change hostname on Ubuntu 18.x

Obtain current hostname:

Use hostnamectl command to retrieve a current system hostname:

ubuntu@ip-172-31-40-128:~$ hostnamectl
Static hostname: ip-172-31-40-128
Icon name: computer-vm
Chassis: vm
Machine ID: dbec540729424e8fa106d97ee9445822
Boot ID: 9fc64012a950420fb11d02d38e77cb88
Virtualization: xen
Operating System: Ubuntu 18.04.2 LTS
Kernel: Linux 4.15.0-1032-aws
Architecture: x86-64

Change Hostname:

$ sudo hostnamectl set-hostname ubuntu-server

Furthermore, check for the existence of /etc/cloud/cloud.cfg configuration. If the file exists edit the file and change the settings withi
FROM: preserve_hostname: false 
TO: preserve_hostname: tru

example from the file - /etc/cloud/cloud.cfg:
The top level settings are used as module
and system configuration.
A set of users which may be applied and/or used by various modules
when a 'default' entry is found it will reference the 'default_user'
from the distro configuration specified below
users:
default
If this is set, 'root' will not be able to ssh in and they
will get a message to login instead as the default $user
disable_root: true
This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true
Example datasource config
datasource:
Ec2:
metadata_urls: [ 'blah.com' ] timeout: 5 # (defaults to 50 seconds)
max_wait: 10 # (defaults to 120 second

confirm the new hostname:

ubuntu@ip-172-31-40-128:~$ hostnamectl
Static hostname: ubuntu-server
Icon name: computer-vm
Chassis: vm
Machine ID: dbec540729424e8fa106d97ee9445822
Boot ID: 9fc64012a950420fb11d02d38e77cb88
Virtualization: xen
Operating System: Ubuntu 18.04.2 LTS
Kernel: Linux 4.15.0-1032-aws
Architecture: x86-64

Your command line prompt will update to reflect the new hostname settings the next time you login.

Appendix:

You may also want to update your “/etc/hosts” file so the system can resolve itself via new hostname. Open “/etc/hosts” file:

$ sudo vi /etc/host

and append your new hostnane right after the old hostname:

$ cat /etc/hosts

127.0.0.1 localhost ubuntu-server
127.0.1.1 ubuntu-server
The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

YaogjB

Please type the text above:

Check Also

Close
Close
Close