Sep 27, 2022
How to Change Hostname on CentOS 7: 3 Methods Explained Step-by-Step
Watching where packets come from and go to is essential for a healthy, and easily manageable network. There are advanced mechanisms such as IP address tracking, however, we can also easily identify a computer with a universal identifier. That’s where the hostname comes in.
Today we’ll show you how to change a hostname on a CentOS 7 machine, to improve your network!
What Is a Hostname?
Basically, the hostname is a name assigned to a computer within a network. Many network services use the hostname as a computer identifier. On the other hand, the hostnames are made more for the user, than the network, since the latter identifies devices by their IP address. Therefore, the hostname is an identifier designed to make work easier for people, not for the computer.
Although it is a simple concept to learn, many people are often confused by it.
In public networks such as the Internet, the hostname has to be combined with the domain name. This indicates which “mini zone” of the Internet it is on. For example:
In this case, server1 is the hostname of the computer we want to access, and example is the domain. Finally, .com is considered the top-level domain.
These identifiable names are called “Fully Qualified Domain Names” (FQDN) and include the name of the computer and the domain name associated with that computer.
There are no restrictions on placing a Hostname on a private network. However, in the case where the computer has access to the Internet, it has to be combined with a domain.
Linux gives us a lot of server features. Of course, changing the hostname on CentOS 7 machines is possible with only a few steps.
Important! CentOS Linux 8 has reached it’s End of Lifetime (EOL) on 2021-12-31. While CentOS Linux 7 is still supported, it will reach EOL on 2024-06-30. We recommend keeping that in mind when choosing this OS. You can read more about it in their official website.
Change Hostname on CentOS 7 Using Hostname and Hostnamectl Commands
First, we have to connect to our server using SSH or open a terminal from our computer. Here’s a tutorial that will help you out, if you’re having trouble.
After that, we need to be the root user to change the Hostname. To gain this privilege use the superuser command, like this:
When executing this command, we have to type the root password. Without it, we can not continue.
Next, we can see the current hostname by running the following command:
As we can see, when executing the hostname command we can view the hostname of the machine. It is also possible to do it with the following command:
In this case, we will have more information related to the hostname.
If we want to change the hostname, we only have to execute the following command:
hostnamectl set-hostname [thenewhostname]
hostnamectl set-hostname test
After that, we have to run the hostnamectl command again to check if the changes were successful.
Change Hostname on CentOS 7 by Editing /etc/hostname File
There is another way to change the Hostname. We can do this by editing the file /etc/hostname with a text editor such as vi or nano.
As soon as we have the file opened, we will see a single line where the hostname is. We just have to replace it with our new one. As we are using vi, press the i key to enter insert mode. Then, change the hostname. After that, press the Esc key to exit insert mode and then the :wq keys to save the changes and exit.
Now, we have to verify that everything is working. To do this, run the following command:
Static, Transient and Pretty Hostnames
When we establish a Hostname using the command hostnamectl or by editing the file /etc/hostname it becomes our static Hostname – it does not change.
However, we can define a temporary Hostname. This is called a transient hostname. It will only be active until we restart the system. To do it we have to add the –transient option on the hostnamectl command.
hostnamectl set-hostname transienthostname –-transient
Then, we check the changes.
Finally, we have the option to create a Hostname with UTF-8 support, to further improve its presentation. This Hostname is known as Pretty. To make one, we add the –pretty option to the hostnamectl command.
hostnamectl set-hostname newprettyhostname –-pretty
This way we can have a Hostname with UTF-8 support.
Change Hostname Using Hostinger’s Control Panel
If we are using Hostinger VPS, we can further simplify the process by using the Hostinger control panel. There you can change the hostname without using the terminal.
Go to your VPS controls in the control panel, click on Other Settings, and type in your new hostname! To apply the changes, click on the Save changes button.
With CentOS, we have several ways to change the hostname. We can also always just use the Hostinger’s control panel.
Having a hostname helps identify a computer on the network, making management a lot easier!