Hostname in Linux
Devices communicate with each other over a network by using IP addresses. The IP address assigned to a particular device may change depending based on the situation. Therefore, it is important to use a memorable hostname that does not change based on the situation.
Several methods can be used to change the hostname in Linux, and these methods can be divided into two categories: command-line interface (CLI) methods and graphical user interface (GUI) methods.
CLI methods
To change the hostname of a device in Linux, we recommend using CLI tools. These methods are universal and will work on almost any Linux distro. Furthermore, there is also no need to install any additional packages.
Check hostname
First, check the hostname of the current system as follows.
Change hostname using hostname
The hostname command can be used to change the hostname of the system by using the following command structure.
Finally, verify the change.
Note that the hostname in only temporarily changed using this method. If the system is rebooted, then it will revert back to the original hostname. This method should be used only when you do not intend to make a permanent hostname change.
Change hostname using hostnamectl
The hostnamectl tool is controls the hostname of the system. This tool can perform a wide range of actions; for example, it can be used to show the current hostname, to change the hostname, to configure the environment description, and to manipulate the system LOCATION string.
To use hostnamectl to change the hostname, run the following command.
Then, verify the change.
Next, we have to manually update the host file. To do so, open the file using a text editor.
Change the old hostname to the new hostname.
$ 127.0.1.1 <new_hostname>
If you are using an older Linux distro, then after making the change, you may need to run the following command.
Change hostname (for init system)
A dedicated file holds the hostname of the system. We can manually update the file to change the hostname. To do so, open the file using a text editor.
The image below shows the old hostname.
Edit the content to update the hostname.
Save the file and close the editor. For the changes to take effect, reboot the system. After the system reboots, verify that the action was successful as follows.
GUI Methods
In this section, we will use GNOME to change the hostname of a device. However, you can use YaST to perform hostname changes in openSUSE.
Change hostname in GNOME
GNOME is a powerful desktop environment. If you use GNOME, then you can change the hostname without running any commands by accessing the “Settings” app.
From the left panel, scroll to the bottom and select “About”. In this example, the hostname of the system is in the “Device name” field.
Click “Device name” to open a prompt for changing the hostname.
Enter the new hostname and click “Rename” to save the change.
Final thoughts
In this tutorial, we showed you several simple methods for changing the hostname of your system. Make sure to use a proper hostname to help to identify the system.
If you are using CentOS/RHEL or a similar distro, then changing the hostname may be more difficult. Check out how to change hostname permanent on CentOS 7.
Happy computing!