Ubuntu Linux Change Hostname

You can change the hostname of your PC running on Linux kernel.

In this tutorial, we will learn how to change the hostname in your Linux machine. We shall use PC running Ubuntu 20.04LTS with Linux 5.4.0-33-generic.

Steps to Change Hostname

Following is a step by step process to change hostname of your Ubuntu Linux PC.

Step 1: Open Terminal by using Ctrl+Alt+T shortcut key or from Applications.

ADVERTISEMENT
Ubuntu Change Hostname

The Terminal title and the prompt, reads sony@ubuntu-desktop. The hostname is ubuntu-desktop. You can also check this using hostname command as shown below.

Step 2: Let us change this hostname. In this tutorial, we will change it to ubuntu-pc from ubuntu-desktop. There are two files that we should modify. Of those, the first one is /etc/hostname. We shall open this file using nano editor as administrator. Run the following command.

$ sudo nano /etc/hostname
Ubuntu Change Hostname

Enter the password if prompted, as shown in the following screenshot. You may not see the password echoed to the screen. Just type your password and click Enter key.

Ubuntu Change Hostname

You will see the file opened in edit mode as shown below. Now we have to change the text, ubuntu-desktop to ubuntu-pc.

Ubuntu Change Hostname

Step 4: Change the hostname to your desired name.

Ubuntu Change Hostname

Type Ctrl+X from keyboard. You will get a prompt if you would like to save the changes.

Ubuntu Change Hostname

Type y or Y and click Enter.

Ubuntu Change Hostname

Click Enter again. We have successfully changed the hostname in /etc/hostname.

Step 5: Now we have to change the hostname in our second file /etc/hosts.

Ubuntu Change Hostname

Open the file using nano editor with root access (sudo). Run the following command in Terminal.

$ sudo nano /etc/hosts
Ubuntu Change Hostname

Change the hostname present in the second line. From ubuntu-desktop to ubuntu-pc. Make sure that you change the hostname in /etc/hostname and /etc/hosts to a same new hostname.

Ubuntu Change Hostname

Change the hostname and type Ctrl+X from keyboard.

Ubuntu Change Hostname

Click y or Y from keyboard and press Enter.

Ubuntu Change Hostname

Press Enter key again. The hostname is changed in /etc/hosts.

Step 6: The final step is to reboot the PC for the changes to take effect. Run the following command in Terminal, to reboot your PC, or you may reboot using GUI.

Ubuntu Change Hostname

Once the reboot is finished you should see that the hostname is changed successfully.

Conclusion

In this Linux Tutorial, we learned how to change the hostname of Linux Ubuntu PC.