Jump to content

How to Change Hostname in Linux


Linux Hint

Recommended Posts

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

host1.png

$ hostnamectl

host2.png

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>

host3.png

Finally, verify the change.

$ hostname

host4.png

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>

host5.png

Then, verify the change.

$ hostname

host6.png

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>

host7.png

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.

host8.png

Edit the content to update the hostname.

host9.png

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

host10.png

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.

host11.png

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.

host12.png

Click “Device name” to open a prompt for changing the hostname.

host13.png

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!

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...