Ubuntu

How to Change Hostname on Ubuntu 20.04

You might have just installed Ubuntu, or you just want to change your hostname due to any reason, then you are exactly at the right place, and this post will guide you on how to change the hostname on Ubuntu 20.04 without restarting your machine. But before getting started, let’s understand the hostname first.

What is Hostname?

The hostname indicates the device name over the network. Since it identifies the device name on the network, so it is not a better practice to have the same name over the same network. Normally, it is set during the Operating system’s installation process, but we can also change it to avoid name duplication.

In Ubuntu 20.04, the hostnames are stored in the “/etc/hostname” file, and only the root user or the user having the sudo privileges can alter these hostnames. So let’s start with the very first command for showing the current hostname in the terminal.

Show Present Hostname

The present hostname can be printed out on the terminal by typing the terminal’s following simple command.

$ hostnamectl

You can see in the screenshot above that the hostname of my Ubuntu system is ubuntuBox. Now let’s see how to change it.

Change the Hostname

We can change the hostname on Ubuntu 20.04 LTS system using two different methods.

Method 1:

The hostname can easily be changed by providing the new hostname to the hostnamectl command with sudo privileges by using the following syntax.

$ sudo hostnamectl set-hostname ubuntuHost

After running the command for changing the hostname, you can verify the new hostname by again typing the “hostnamectl” command.

$ hostnamectl

You can witness in the screenshot that the hostname is updated successfully.

Method 2:

We can also change the hostname on Ubuntu by editing the “/etc/hostname” file by opening it in any text editor like nano. To open the “/etc/hostname” file in the nano editor, type the command given below.

$ sudo nano /etc/hostname

And now replace the old hostname with the new hostname of your choice.

After changing the hostname, save the file and exit using the shortcut keys(CTRL+S) and (CTRL+X).

Now again, run the command “hostnamectl” to verify the newly assigned hostname.

$ hostnamectl

You can see the newly provided hostname is changed successfully on Ubuntu 20.04.

If you close and reopen the terminal or open a new tab in the terminal, you can also see the changed hostname highlighted in the attached screenshot.

You have successfully changed the hostname, but you need to edit the “/etc/hosts” file and provide the new hostname there as well because the hostnames are mapped in this file.

Change the mapped hostname

$ sudo nano /etc/hosts

Replace the old hostname with the new one you have just set for the device in the “/etc/hostname” file.

Once it is altered as well, Save it and exit from the nano text editor using the shortcut keys (CTRL+S) and (CTRL+X).

Now, you have successfully changed your hostname.

Conclusion

This article shows how to change the Ubuntu 20.04 LTS system’s hostname without even restarting your system.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.