Change Ubuntu Hostname

What is a computer name (hostname)?

Your computer name, in technical terms, is also referred to as the hostname of your computer system. A hostname is how other computers recognize your computer over a local network. Like on the Internet, we have URLs instead of hostnames. These URLs contain regular words like google.com that we can easily understand instead of remembering the numeric IP address of a server.

We can give easy computer name/hostname for our systems so that other computers can easily identify it over a local network. So instead of remembering your IP address, other people can access local web pages and other authorized data on your system through your hostname.

In this article, we will give a few simple ways to change your computer name through the graphical user interface and the command line.

The commands and procedures mentioned in this article have been run on a Ubuntu 18.04 LTS system.

How to change the hostname?

Method 1: Through the GUI

Through the UI, you can change your computer’s device name. It can be called a “pretty hostname” as it is not the permanent or static hostname of your computer. Nevertheless, you can change the device name as follows:

Open your system settings either by clicking the downward arrow located at the top-right corner of your Ubuntu screen and then clicking the settings icon from the following view:

Ubuntu Settings

OR

Open the Settings utility through the system Dash as follows:

Search for settings utility

The Settings utility will by default open in the Wi-Fi view as follows:

Wi-Fi View

Move to the Details view by clicking the Details tab from the left pane. You will be able to view the Device name in the About view as follows:

Details tab

The device name will change as soon as you enter a new name in the Device name textbox.

Please note that this is not your computer’s permanent hostname. Please read further in this article to view how you can change your computer’s permanent hostname.

Method 2: Manually through the hostname and hosts file

You can view the hostname of your computer by entering the following command in your Terminal:

(Click the Ctrl+Alt+T shortcut to open the Terminal application)

$ hostname

Get current hostname

One way to change the hostname is through the following command:

$ sudo hostname new-hostname

Example:

$ sudo hostname Linux-system

Set new hostname with hostname command

The drawbackof this method is that the hostname will revert to the original when you restart your system.

The proper way to change the hostname is by changing it in two configuration files named the hostname and hosts file located in the /etc/ folder.

You can open these files through any of your favorite text editors. We are opening this file in the nano editor as follows:

$ sudo nano /etc/hostname

Edit the /etc/hostname file

The only text in this file lists the hostname of your computer. Simply change the text to a new hostname and then exit and save the file by clicking Ctrl+X, and then y and hit enter.

Then open the hosts file as follows:

$ sudo nano /etc/hostname

In this file, the hostname is listed against the IP: 127.0.1.1

Edit /etc/hosts file

Change this hostname to a new hostname and then exit and save the file by clicking Ctrl+X, and then y and hit enter.

Now when you restart the system, your hostname will change to a static new hostname.

Method 3: Through the hostnamectl command

The smartest way to change your hostname is through the hostnamectl command that is a part of the Systemd utility. If Systemd is not already installed on your system, you can install it through the following command as root:

$ sudo apt install systemd

You can check the version number of the Systemd utility by running the following command:

$ systemd --version

This command will give you the version number of the utility and also ensure that it is indeed installed on your system

Now that the Systemd utility is installed on your system, you can run the following command in order to view detailed information about your system, including the hostname:

$ hostnamectl

Output of hostnamectl command

In this output, the Static hostname lists the permanent hostname of your machine. The Pretty hostname lists the Device name you have set up through the UI in the Settings utility. The hostnamectl lists the Pretty hostname(device name) only if it is different from the static hostname.

In order to change your computer’s hostname through the hostnamectl command, use the following syntax:

$ hostnamectl set-hostname “new-hostname”

Example:

$ hostnamectl set-hostname Linux-system

Set new hostname with hostnamectl command

Now when you see, the hostname through the hostnamectl command, it will show the static hostname as the new hostname you have set. The system has also changed the device name to the hostname you specified through the set-hostname command.

You can verify through the UI that your device name will also be the same as your static hostname. Open the Settings utility and move to the Details tab to view your device name.

New hostname shows up in the GUI as well

The plus point of the Hostnamectl command is that you do not need to restart your computer in order to permanently change the hostname.

Conclusion

Through this tutorial, you learned to change the device name and computer name(hostname) of your system. Now you can change your computer’s hostname either temporarily or permanently through the Ubuntu command line. All you need to do is change a few configuration files or simply use the hostnamectl command to do so. Now you can have a customized computer name through which other computers over the local area will identify you.

 

How to Change your Ubuntu Computer Name (Hostname)
Avatar

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn