In this article, we will go through the process of changing the hostname in Linux systems. Before we get to the step-by-step process, let’s quickly got through what exactly is a hostname.
What is a hostname?
A hostname is a label assigned to a machine (PC, server, printer, etc.) to identify the devices on the network. Therefore each device on the network should have a unique hostname. Usually, the hostname is set during the installation, and it’s relatively easy to change it to something different.
This is not something that many people care about, and usually, when getting a VPS, the data centre will automatically set it. If you manage multiple servers (like me) or don’t like the current hostname on your system, then this is for you.
Personally, I need to see the relevant information on the SSH screens by different hostnames. This will give the relevant information swiftly on what system I am logged in.
The hostname can be a simple string containing letters, numbers, dots and hyphens. Whereas if the machine is connected to the internet, it is recommended to use a fully qualified domain name (FQDN) as the system hostname.
How to change the hostname: temporary
Changing the hostname is not complicated at all; you need a single command.
- To display the current hostname on your Linux system, type in:
- To output the FQDN of the system:
- To change the hostname of your system:
As you see here, the “NEW_NAME” was replaced with the actual hostname that was needed. However, to perform this action, you will need to be the root user or have root privileges (check the above picture).
You will most probably need to exit the current shell to see the changes in your shell prompt.
That’s all and good, but this is a temporary solution and will be reverted to the original upon the next reboot. To make the hostname permanent, you will have to change some configuration files.
How to change hostname permanently in Linux
New versions of different Linux distributions like CentOS, Debian, Ubuntu, Fedora etc. comes with systemd. This is a system and service manager which provides the hostnamectl command which will be used in this tutorial to manage the hostnames.
Note: To run this command, you will need to be a root user or have sudo privileges.
- Run the command:
sudo hostnamectl set-hostname NEW_NAME
Here set the NEW_NAME as the preferred name of your system.
- Verify if the hostname was successfully changed.
In the output, you will see the new system hostname and additional kernel information.
To learn more about Linux commands, we made a beginners guide (check it out).
This method will edit the system file containing the hostnames and then run that specific file to activate the changes.
Note: To continue this process, you will need to be a root user or have sudo privileges. This tutorial will use the nano text editor; however, you can use any other text editor.
- Enter the new hostname:
sudo hostname NEW_NAME
- Enter the following command and update the hostname.
sudo nano /etc/hostname
- Now, as the final step, update the lines that read your old hostname:
sudo nano /etc/hosts
- In the appeared window, check here, as shown in the picture below:
- Now change the name from the old name to the new one:
- Save and close the file!
- Check for the changes.
Congratulations! You made it to the end and successfully changed the hostname of your Linux system. The above commands were done and tested on Ubuntu 20.04. However, the commands are similar for all other Linux distributions.
Be careful when changing the hostname permanently using the 1Gbits VPS servers as it might make some connection errors when using the SSH client.
In some cases, depending on the Linux distribution, the commands might change slightly. If you will come across any problems, let us know in the comments below.