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-serverCode language: JavaScript (javascript)

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.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%