How to Change Hostname on Ubuntu 20.04

Harshit Khandelwal
3 min readOct 19, 2020

A Hostname is a unique human-readable name given to a server or a computer in a network that helps you to access it without having to remember its associated IP address. The default value of the Hostname is localhost on any Linux distribution.

In this guide, we will see how to change the default hostname in Ubuntu 20.04.

Check the Current Hostname

To check the current hostname of your Ubuntu machine, use the hostnamectl command mentioned below.

hostnamectl

Output:

root@localhost:~# hostnamectl
Static Hostname: ath.system36
Icon name: computer-vm
...

As seen in the output above, the current hostname is set to ‘ath.system36’.

You could also simply use the hostname command to check your machine’s hostname.

root@localhost:~# Hostname
ath.system36

Types of Hostnames

There are three different types of Hostname:

  1. Transient — The network configuration gives this Hostname.
  2. Static — It is stored in /etc/hostname file and provided by the kernel. It is used to initialize the kernel during the boot process. The transient Hostname is not used when the static Hostname is set to values other than the localhost.
  3. Pretty — The user sets this name, and it can contain special characters or UTF 8 characters (e.g. ‘Harshit Khandelwal’s PC’ etc).

Pretty Hostname has lesser restrictions when it comes to naming it, but static and transient hostnames can only use the characters accepted by internet domain names. So it would be best if you always name them using the standards of a Fully Qualified Domain name or FQDN.

In most cases, configuring only the static hostname is enough. We’ll focus this guide on changing the static hostname only.

Changing the Hostname

You can use the hostnamectl command to change your Ubuntu 20.04 machine’s hostname in a jiffy.

Syntax:

sudo hostnamectl set-hostname [hostname]

Example:

sudo hostnamectl set-hostname allthingshow

In the above example, we changed the statis hostname of the system to ‘allthingshow’.

hostnamectl does not produce any output on successful execution but returns a failure code when the process gets unsuccessful.

Change Hostname Value in /etc/hosts File too

The static and pretty hostnames are stored in /etc/hosts and /etc/machine-info file, respectively. Let’s navigate to these files and change their values as well.

Open the file in the Nano editor by typing this command:

sudo nano /etc/hosts

The above command will get the following output:

127.0.0.1 localhost
The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

The Hostname is generally mapped to 127.0.0.1 on most Linux based systems. Replace the localhost’s value from the Hostname of your choice in the above file.

Now, press Ctrl + o, and press Enter when File Name to Write: /etc/hosts appears. Now, close the document by pressing ctrl + x.

We are not required to change the /etc/machine-info file, as its value is changed by default from sudo hostnamectl set-hostname "Harshit Khandelwal" --pretty command.

Editing /etc/cloud/cloud.cfg file (If you are running Ubuntu on Cloud instances)

To do this type the following command in your terminal:

sudo nano /etc/cloud/cloud.cfg

Press Ctrl + w, type preserve_hostname and hit enter, and change it from false to true.

This will cause the set+update hostname module not to operate (if true)
preserve_hostname: true

Save the changes by pressing Ctrl + o and close the editor by pressing Ctrl + x.

Verify the changes

To verify the changes type hostnamectl command in your terminal. You will get the results, as shown below:

root@localhost:~# hostnamectl
Static Hostname: allthings.how
Pretty Hostname: Harshit Khandelwal
Icon name: computer-vm
Chassis: vm
Machine ID: a72b6b3938334412977c2bb98acf4447
Boot ID: c9026248d8734be8bfe7f79535c47f67
Virtualization: kvm
Operating System: Ubuntu 20.04.1 LTS
Kernel: Linux 5.4.0-42-generic
Architecture: x86-64

The changes have taken place successfully, now reboot the system by typing sudo systemctl reboot command.

Conclusion

Hence, the Hostname on our remote Ubuntu 20.04 LTS machine has changed. Now when you log in back to your remote server, the screen will look like as shown below.

--

--