Although the system sets the hostname during the installation process, you can change the hostname at any time on Linux.
A hostname, also called a computer name, is a label assigned to a host on a network which 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 proceed with changing the hostname, let’s first check what’s the current hostname on our Linux system.
They are several ways to check the hostname on your Linux system. Here I will going to show you two of the most commonly used methods.
You can find the system hostname in Linux, by running
hostname command without any options:
As you can see from the output above, my Linux system’s hostname is
Another way to get the system hostname in Linux is to run the
hostnamectl command without any options:
hostnamectl displays a few more related details of your system such as type of the system, operating system, kernel version, architecture, etc.
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
For this purpose, we will use the
hostnamectl command with the
set-hostname argument followed by the new hostname, in our case
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:
You can see 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. 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
Save the changes and exit from the
/etc/hosts file. Then reboot the system to apply changes.
The hostname in any Linux based distribution is used to identify a system uniquely in a network. 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 hostname on Linux. As always, feel free to provide your feedback.