How to Change Hostname on Linux

How to Change Hostname on Linux

Understanding how to change hostname on Linux is vital to streamlined network connectivity. And in this article, we will discuss how you can go about it. 

In the web world, computers are identified with a hierarchical numbering scheme called Internet Protocol (IP) addressing. However, it is not easier to remember the computers based on their IP addresses. Hence, an alternate hostname is used to identify the names mapped to their IP addresses. 

These hostnames are used to identify hosts on the network in almost all operating systems, like Linux, Windows, and Mac OS. Generally, a network engineer specifies the host’s name during network configuration and system installation. We will discuss various ways to change the hostname of a system.

What is a hostname?

A hostname is a unique name in the network that is used to identify the host in the network. Each network interface in your system, such as your network card and the modem, should have a hostname. If a machine is connected to the internet, a fully qualified domain name (FQDN) is used that comprises a hostname and the domain name. 

Tip: The hostname is alphanumeric characters, but dots and hyphens are also allowed.

Typically, the hostname is set during the installation of the system. However, often you may want to change the current network settings, such as to make the host part of a new network. So, you may want to follow the name convention of the new network. In a cloud environment, the hostname is dynamically assigned to the nodes. 

Why are IP addresses not suitable for recognizing hosts by an average user?

The devices in the network communicate with each other using the hierarchical IP addressing scheme. However, it is not suitable for average users for several reasons. First, they are challenging to recognize and remember for a non-specialist. In addition, the IP addresses can change at times or be assigned dynamically. 

For consistency, the hostnames are considered for referring to the hosts by average users.

How to Change Hostname on Linux

Now, we will discuss the various ways of changing the hostname. You should have a Linux operating system with superuser rights to run the commands in the following sections. Only the root or a user with sudo privileges can change the hostname. Following are the four ways to change the hostname:

  1. hostnamectl command
  2. hostname command
  3. Editing /etc/ hostname
  4. GUI based methods

Finding the name of your host in Linux

As the first step, we will discuss how we can find the host’s name we are currently working on. The name of the current host can be determined using the following command. On your Linux system, open the Terminal and type the following command:


Once done, the name of your host will be displayed. There is another command that can be used to find the name of your host. Type the following command on your Terminal:


The following information will be displayed on the Terminal: hostname, machine ID, operating system, kernel, and architecture.

Changing your hostname using hostnamectl command

The command hostnamectl can be used to manage a hostname. The command accepts a parameter that can be used to change the hostname. The hostname can be altered using the following command:

$hostnamectl abacus

The above command changes the name of the host to the abacus.

You can also use the hostnamectl command with the option set-hostname to change the name of the host in the following way:

$hostnamectl set-hostname abacus

Once the command above is successfully executed, no output will be displayed. However, this command returns the status as code 0 or a non-zero value depending on the command’s success or failure. You can then verify that the hostname has been changed with the hostnamectl command again as follows:


You should see the name of the host changed to the abacus.

Changing the name of the host using the hostname command

Before the hostnamectl command existed, and the hostname command changed a host’s name. Similar to hostnamectl, this command also accepts the new host’s name as a parameter. The following command changes the name of the host to abacus:

$hostname abacus

The two methods that we discussed can only be used to change the hostname temporarily. Once the computer is restarted, the hostname is reverted to the original name. Hence, these two methods are used when you only want to change the hostname temporarily. Now, we will discuss how we can permanently change the hostname.

Changing the name of the host via /etc/hostname file

In Linux, the hostname is stored in a file /etc/hostname. This file is a plain text file, and you can edit it in your favorite editor. For instance, the following command opens the /etc/hostname file using the nano editor:

$nano /etc/hostname

The file will then be opened in the editor. From there, provide the new name for the host, save the file, and replace all occurrences of the current computer name with the abacus. And finally, reboot the computer after saving the file.

Changing the hostname via GUI-based methods

Various Linux shells provide graphical interfaces to make changes to the hostname. The steps to be followed for changing the hostname depend on the shell, such as GNOME, KDE, etc.

In this guide, we discussed what hostnames are and why they are used in place of IP addresses for referring to systems in Linux. We discussed how we could find the host’s name in Linux and various ways of changing the hostname. 

We have discussed four approaches in total. Through using the hostname and hostnamectl commands, via editing /etc/hostname file, and using a graphical user interface. More details about these commands can be seen in the manual.

If this guide helped you, please share it.

Leave a Reply
Related Posts