How to Change the Hostname on Ubuntu

Ubuntu Hostname

In a local area network (LAN) environment, computer systems must communicate with each other based on their IP addresses. Learning and retaining these IP addresses, and passing them on when needed, is a tricky business. To avoid such problems, users rename the hostname of their system. The simpler hostnames allow all computer users to coordinate easily without exchanging large IP addresses. This whole scenario has a lot to do with URLs and DNS server addresses, because users don't even know long addresses and just use the URLs in their search engine.

In this tutorial, I will show you two methods to change the hostname of an Ubuntu system using the command line and the graphical user interface. Users can choose either method to update and share the names once they have finalized them. The instructions in this guide are compatible with Ubuntu 20.04 and Ubuntu 22.04.

Prerequisites

  • Recommended OS: Ubuntu
  • User account: A user account with sudo rights

How to change the hostname

We will discuss the methods to change the hostname through the command line using hostnamectl command and the GUI of Ubuntu system.

Method # 01

Change the hostname on the shell with the hostnamectl command

To change the hostname permanently, open up the terminal window of the Ubuntu system using the Ctl+Alt+T shortcut or go to ApplicationsTerminal. Once opened, check the current hostname of the device. To display the current hostname, type the hostnamectl command in your terminal.

$ hostnamectl

hostnamectl command

An output containing details of the system like the hostname, and other hardware specifications will appear. In this method, we desire to update the Static hostname field. The pre-existing hostname in our example is ubuntu-VirtualBox.

View hostname

Users can also check the existing hostname by entering the following command in the terminal window:

$ cat /etc/hostname

Get hostname from /etc/hostname file

The existing hostname will be displayed in the output.

Display hostname

To change the currently set hostname, type the following hostnamectl command:

$ sudo hostnamectl set-hostname newNameHere

Set new hostname

You can change the hostname, by replacing it with the newNameHere in the previous command. We are setting the new hostname as ubuntu in the example to get it replaced with the existing name i.e., ubuntu-VirtualBox. In order to make such changes, users need to have sudo rights.

Here the system might ask user for the password. Enter the password in the required field and then press the <enter> key to proceed with the process of changing the hostname

enter password

Once the name is updated, you can verify the updates using:

$ hostnamectl

Run hostname command

The output will appear showing the updated Static hostname.

New hostname has been set

This way you can easily change the hostname via the command line within the Ubuntu system. The changes will be reflected on opening up a new terminal window. Users do not need to restart their computer systems.

Method # 02

Set new Hostname through the GUI

To change the hostname temporarily, open up the Settings of the system. Go to the Settings from the Applications list. Once done, the Settings window will open up.

Network settings

Scroll down to the About section from the left side navbar. The About section is under the Date & Time option as shown in the figure below:

Device name

Here you will see the Device Name field on the right side. The pre-defined name is set as ubuntu-VirtualBox as the hostname. To update it by click on the Device Name field. The Rename Device modal will open up, provide the updated name, and then select the Rename button. In our case, the new hostname is ubuntu.

Rename device

Please note that this is not the permanent hostname. Users can update and rename the hostname based on their choice.

How to verify the updated hostname?

To verify the updated hostname, open up the new terminal window in your computer system. The hostname will be updated as shown in the figure below:

Verify hostname change

Conclusion

In this tutorial, we discussed two methods to change the hostname of the Ubuntu 20.04 system. The first method allows users to update the hostname permanently through the command line and the second method helps them to update the hostname through GUI. Both methods are pretty straight forward and can be followed easily. Users can use either of the method based on their requirements.