The hostname is known as the system’s identifier, which is set at the time when we are installing the Ubuntu operating system.
This tutorial will explain setting the hostname for your system or virtual machine after Ubuntu 18.04 installation.
Before going through with this tutorial, make sure your system has Ubuntu 18.04 Linux installed and have user access with sudo privileges to execute administrative commands.
Display the Current Hostname
To check the current hostname on the Ubuntu system, use the hostnamectl command as shown below:
Static hostname : satishsystem Icon name : satish-vm Chassis : vm Machine ID : 6f17445f53074505a008c9abd8ed64a5 Boot ID : 1c769ab73b924a188c5caeaf8c72e0f4 Virtualization : kvm Operating System : Ubuntu 18.04 LTS Kernel : Linux 4.15.0-22-generic Architecture : x86-64
You can see in the above output; the current hostname is set to the satishsystem.
Change the Hostname
To set the new hostname for your Ubuntu system, use the following steps.
Step 1 – Change hostname using hostnamectl command.
You can change the hostname in Ubuntu 18.04 using the hostnamectl command.
For example, we can change the system static hostname to linuxconcept by using the following command:
$ sudo hostnamectl set-hostname linuxconcept
The command hostnamectl does not return any output on success. It returns 0 on success or failure code on failure results.
Step 2 – Edit the /etc/hosts configuration file
Open the /etc/hosts file in your preferred editor and change the old hostname with a new desire hostname.
127.0.0.1 localhost 127.0.0.1 linuxconcept # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters
Step 3 – Edit the cloud.cfg configuration file
If you are using the cloud-init package in your Ubuntu machine, then you need to edit the cloud.cfg file also. Using your Ubuntu box on a cloud like AWS, this package will be preinstalled on those instances used to handle the instance’s initialization.
You can check the package is installed in your Ubuntu machine or not use the ls command:
$ ls -l /etc/cloud/cloud.cfg
If the package is not installed, you will get output on the screen like below:
output: ls: cannot access '/etc/cloud/cloud.cfg': No such file or directory
But, if the package is installed, you will get output like this:
output: -rw-r--r-- 1 root root 3169 Apr 27 09:30 /etc/cloud/cloud.cfg
In the case of the package is installed, to change the hostname, open the file with your text editor:
$ sudo vim /etc/cloud/cloud.cfg
Now, search for string preserve_hostname and change the value of it from false to true, as shown below:
# This will cause the set+update hostname module to not operate (if true) preserve_hostname: true
And close the file after saving it.
Verify new hostname after change
You can verify the hostname has changed in your Ubuntu system by checking hostname using the hostnamectl command, as shown below:
You will get your new hostname on the output screen.
Static hostname : linuxconcept Icon name : satish-vm Chassis : vm Machine ID : 6f17445f53074505a008c9abd8ed64a5 Boot ID : 1c769ab73b924a188c5caeaf8c72e0f4 Virtualization : kvm Operating System : Ubuntu 18.04 LTS Kernel : Linux 4.15.0-22-generic Architecture : x86-64
Now, you have learned to change the hostname for your installed Ubuntu 18.04 Linux server or system without restarting the system.
If you get any problem with changing the hostname of your system, please leave a comment below.