How to change hostname on Ubuntu

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to change the hostname of your Ubuntu machine.

Hostname is the name that your machine uses to talk to other computer. Hostname is a unique name that you can use to identify your device amongst other devices.

Changing hostname can be done via terminal or GUI. In this tutorial, we'll be changing the hostname of our Ubuntu machine, by using only the terminal.

Change Hostname on Ubuntu

Hostname can be easily changed by using the hostname command, directly from the terminal. All you have to do is use the hostname command, and give your machine a hostname you like.

The syntax of changing your machine's hostname is:

sudo hostname name-of-host

So let's say you want to name your machine owlhowto then to do do, you need to run the command below.

sudo hostname owlhowto

Next, authenticate by entering sudo password, and press enter to proceed changing hostname.

💡
Note: Once you change the hostname, you need to close your terminal window and open it again to see the new hostname.

However this is not the only way to change the hostname on Ubuntu. You can also change the hostname by manually editing the hostname file.

This file is located at /etc/hostname, and to edit this file you can use any text editor.

For example, to change the hostname by editing the hostname file via nano text editor, run the command below.

sudo nano /etc/hostname

Next, enter the hostname, and then save changes by pressing the CTRL + X, next confirm by pressing Y, and finally press enter.

Check Hostname

To verify that the hostname was changed, check the current hostname by using the hostname command.

hostname

Alternatively, you can also view the current hostname, by reading the content of the hostname file located at /etc/hostname

cat /etc/hostname

And it should show you the current hostname that your machine is using. If for some reason you still see the old hostname, log out and log in again on your machine.

Conclusion

In this tutorial, you learned how to change the hostname on Ubuntu.

The hostname command works on any Ubuntu distro, and Ubuntu based distros, which means you can use the same command to change the hostname on your machine.