How to Change Hostname in Linux

How to Change Hostname in Linux

Although the system sets the hostname during the installation process, you can change it at any moment in Linux.

If you forget to customize the machine name during the installation of the operating system, you can do it later. Here’s how!

A hostname, also called a computer name, is a label assigned to a host on a network that distinguishes one device from another on a specific network. It can be a simple string containing alphanumeric characters, dots, and hyphens.

Display Hostname in Linux

Before changing the hostname, let’s first check the current hostname on our Linux system.

They are several ways to check the hostname on your Linux system. I will show you two of the most commonly used methods.

You can find the system hostname in Linux by running the hostname command without any options

hostname
Hostname command on Linux

As you can see from the output above, my Linux system’s hostname is ubuntu.

Another way to get the system hostname in Linux is to run the hostnamectl command without any options:

hostnamectl
Hostnamectl command on Linux

Unlike the hostname command, hostnamectl displays a few more related details of your system, such as type of the system, operating system, kernel version, architecture, etc.

Related: What Linux Version Am I Running? Here’s How to Find Out

Now let’s move on to changing the hostname of our Linux system.

Change Hostname in Linux

We can change the system hostname of our Linux system to any other desired hostname. For example, we want to change the hostname of the current system from ubuntu to web-server.

For this purpose, we will use the hostnamectl command with the set-hostname argument followed by the new hostname, in our case, web-server:

sudo hostnamectl set-hostname web-server

Of course, don’t forget to replace web-server with the name you’d like to use.

Keep in mind that hostnamectl set-hostname does not produce any output. Therefore, you can use the hostnamectl command without any options to verify that hostname has been changed:

hostnamectl
Change Hostname in Linux with the hostnamectl Command

The newly provided hostname is changed successfully on our Linux system.

While you changed the hostname through the hostnamectl command, you are not required to reboot the system to see the effects of the hostname change.

However, to change your hostname permanently, you’ll also need to edit your /etc/hosts file. Then, with the editor of your choice, for example, Nano, open the file and replace all the occurrences of your old hostname with the new hostname.

sudo nano /etc/hosts
Changing the /etc/hosts file
Changing the /etc/hosts file

Save the changes and exit from the /etc/hosts file. Then reboot the system to apply changes.

Conclusion

The hostname in any Linux-based distribution identifies a system uniquely in a network. Therefore, one of the essential tasks for a system administrator is to know how to change the hostname.

I hope this article helped you to change the hostname on Linux. As always, feel free to provide your feedback.

Leave a Reply