How to Change Hostname in Linux

To differentiate devices, data packets use IP addresses; however, IP addresses may not be easy for humans to recognize. Luckily, in Linux, the hostname is used in places like the terminal prompt to identify the device being used. In this guide, we will show you how to change the hostname of a device in Linux.

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.

$ hostname

$ hostnamectl

Change hostname using hostname

The hostname command can be used to change the hostname of the system by using the following command structure.

$ sudo hostname <new_hostname>

Finally, verify the change.

$ hostname

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.

$ sudo hostnamectl set-hostname <new_hostname>

Then, verify the change.

$ hostname

Next, we have to manually update the host file. To do so, open the file using a text editor.

$ sudo vim /etc/hosts

Change the old hostname to the new hostname.

$ 127.0.1.1 localhost

$ 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.

$ /etc/init.d/hostname restart

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.

$ sudo vim /etc/hostname

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.

$ hostname

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!



from Linux Hint https://ift.tt/36VtCZ9

Post a Comment

0 Comments