How to Change Hostname on Ubuntu 20.04?

ubuntu change hostname
Share

A hostname is a name given to a device connected to a network. This is a unique identifier that network administrators assign to devices during setup or maintenance. 

In Ubuntu 20.04, users generally assign a hostname to the machine during OS setup. However, as this article will discuss, there are several ways of changing the hostname in Ubuntu 20.04, both at the command line and through the GUI. 

But first, let’s discuss the importance of hostnames.

What Is A Hostname?

A machine connected to the internet is assigned a hostname, a unique name that identifies it on the internet or network. However, it is important to note that the devices connected to a network don’t need hostnames to identify or communicate. Instead, they use IP addresses to exchange information. 

A hostname can have two components – a hostname and a domain name. These parts are separated by a period. The total allowable length of the hostname is 255 characters. While you can name the devices anything you want, you need to follow specific guidelines and ensure that the hostnames describe the machine (and its location within the network) for quick identification. 

Why Would You Want To Change Hostname On Ubuntu 20.04? 

No two computers can have the same hostname on a network; otherwise, a conflict would affect both devices and the network.  

Many IT departments have a device naming convention that aligns with asset management policies. In addition, several automated asset management platforms use custom hostnames for management. 

Security is an important reason why network administrators change hostnames. They often opt for hostnames that aren’t easy to guess to thwart network intrusions. 

Now that you have a good idea of what hostnames are, it’s time to dive into the process of changing hostnames on Ubuntu 20.04 machines. 

Prerequisites

When changing hostnames on Ubuntu 20.04 machines, you need the following:

An admin account or a user with Sudo privileges.

Access to command line tools and a GUI for changing hostnames through your preferred method.

Find the Current Hostname in Ubuntu 20.04

Before changing the hostname of the Ubuntu machines, you need to know the current hostname of the machine.  

For this, use the following command in the terminal to display the hostname: 

# hostname

hostname

If you need more details, use the hostnamectl command that displays additional information about the machine. 

# hostnamectl

hostnamectl

Now that you know how to find out the hostname, let’s go into the details of changing the hostname in Ubuntu 20.04.

Change the Hostname Temporarily on Ubuntu 20.04

Ubuntu 20.04 allows you to set a temporary hostname for a machine on the network. Note that this change will remain effective until the device is rebooted. 

To change the hostname temporarily, use the following command:

# hostname <new hostname>

Since the command doesn’t provide any output, you need to verify that the command has successfully changed the hostname. For this, we’ll use the hostname command to get the latest hostname

#hostname

hostname2

Change The Hostname Without Rebooting The Server

There Are times when you don’t want to reboot the machine when changing the hostname. This often happens when you are working with servers. To change the hostname without rebooting the machine, use the hostnamectl command. 

#hostnamectl set-hostname <new-hostname>

Remember to replace <new-hostname> with the new hostname.  Since you won’t get any status updates, use the hostname command to verify the change. 

# hostname

hostnamectl2

Change the Hostname by Rebooting the Server

The easiest way of changing the hostname permanently is to change the entries in the system files. The hostname records are stored in two files –  /etc/hostnames and /etc/hosts

We can easily change the system’s hostname by editing these files. The only caveat here is that the changes will take effect once the system has been rebooted. 

We’ll now go into the details of the two options.

Change the Hostname By Editing the /etc/hostname file

/etc/hostname contains the current hostname of the system. By editing this file, you can easily assign a new hostname to the system. 

Open the file in your preferred file editor. We’ll use Vim for this demonstration. 

Start with the following command in the terminal:

# vi /etc/hostname

Find the hostname of the system and replace it with the new hostname. Next, save and exit.

edit_hostname

Change the Hostname By Editing the /etc/hosts file

The /etc/hosts file contains a record of all the hosts on the network. Usually, this is a simple text file that you can edit with your text editor. Like the previous demonstration, we’ll use Vim for editing this file. 

# vi /etc/hosts

You need to find the current hostname and replace it with the new hostname. Finally, save and exit the editor. 

etc_hosts

Important: To apply the changes, you need to reboot the device. For this, use the following command;

# sudo systemctl reboot 

Optional: If you’re running Ubuntu 20.04 as a cloud instance (mainly through the cloud-init package), you need to edit the /etc/cloud/cloud.cfg file in a file editor. Search for the parameter preserve_hostname and set the value to TRUE. 

Change Hostname on Ubuntu 20.04 Using GUI

Ubuntu supports all popular GUI options, and many admins prefer changing the hostname on Ubuntu 20.04 systems through GUI. The process is pretty simple and involves the following steps.

Step 1: Go To Settings

To start the process, go to Settings and locate the About section.

Go To Settings

Step 2: Find the Device Name

Find the Device Name in the About section. Usually, it’s located at the top of the screen. 

device_name

Step 3: Change the Device Name

Click the device name, and a new dialogue box opens up. You can now enter a new name as the hostname of the system. Click the green Rename button to finalize the configuration. Note that this change is permanent and goes into effect immediately.

Change the Device Name

Conclusion

This article presents three methods of changing the hostname on an Ubuntu 20.04 machine: using the command line tools, editing the host files, and through the GUI. All the methods are simple and straightforward, so you can choose whichever one fits your use case best.