How to Change Hostname on CentOS Linux

A lot of us have heard or read the word hostname before, but the meaning of this term may not be exactly clear if we are not related to the hosting industry or to the Linux community in some way. We may also hear about it if we are taking part in a web development career. If you want to know what a hostname is then please stay with us and keep reading to find out.

If you have a web hosting plan with a hosting company then they may have sent you a hostname before, either to connect to a control panel, to use it in your email configuration, etc. If you are interested in Linux or in a web developer career then you probably have seen the term already, or you will soon. What exactly is a hostname then?


What is a hostname? What is it useful for?

To keep it simple, a hostname is a name for a server, a computer, or any device that can have one. Very often, the hostname is a subdomain of a real domain or a valid FQDN, which is the acronym for Fully Qualified Domain Name.

An easy example of a hostname of a server would be something like “”. We can even set up a hostname for a computer, in fact, this is a very common practice in the Linux community.

Ok, a hostname can be used to give a “name” to a server or a computer, but why are hostnames useful? Or how can we make a hostname useful? Well, in the first place, giving a system a name is useful to identify it of course, but it also has some additional uses in some cases.

For example, if we have a server with the hostname “”, and that hostname is pointing to the server, then we can use the hostname to set up our local email client to point to this server. We can also use it for FTP connections and also for connecting to control panels like cPanel/WHM in our web browser.

As we can see, the hostnames can be very useful when used correctly, so now let’s go ahead with something more practical, and let’s change the hostname in a CentOS environment.

How to Change CentOS Hostname on CentOS 7 and 8

Before changing the hostname we have to keep a few things in mind. Yes, even hostnames have some basic requirements, and those requirements are the following:

  • A hostname can use lower or upper case letters, that is, the letters from a to z in the English alphabet. It is not advised to use other letters. Remember that hostnames are case sensitive, so, for example, will not be the same as (notice the lower/upper case S).
  • Hostnames can also contain numbers, any number from 0 to 9 is fine, and of course, you can also mix those digits to make up bigger numbers.
  • The hostnames can also contain 2 special characters, which are the hyphen (-) and the dot (.), those are the only 2 special characters allowed, you can’t use any others.
  • Finally, hostnames have to start and end with a letter or a number and they also must have an extension of 2 characters minimum and 63 characters maximum.
  • Additionally, you can make it descriptive, though this is only really useful if you have many hostnames related, for example if you have an email server and a web hosting server you can use something like “” for the email server and “” for the web hosting server.

Ok, now that this is clear let’s see the different ways that exist to change or set the hostname of a CentOS server or computer.

Change it manually, by editing the network configuration files

Let’s start with the hardest way to accomplish this task, though it’s not really that hard, is just a bit more difficult than the other ways because we have to run more commands to accomplish it.

First, we have to edit the /etc/sysconfig/network file using our favorite text editor. In this example we are going to use nano, which has a nice interface and is easy to use:

nano /etc/sysconfig/network

Inside this file, we only have to change the HOSTNAME variable to enter the new hostname that we want, so the final result will be something like this:

Now stop editing and save the change, and let’s edit another file, /etc/hosts this time.

nano /etc/hosts

Ignore the line that references the localhost ( and change the hostname in the one that contains your server’s IP, which is usually the second line: localhost localhost.localdomain
111.222.333.444 server100

As you can see, the second line contains (from left to right) the IP in the first place, the full hostname in the second place, and the first part of the hostname in the third place.

Once that is done we just need to restart the network service to make the changes permanent:

/etc/init.d/network restart


service network restart

Set your CentOS hostname using the hostnamectl command

An easier way to change the hostname is to use the hostnamectl command, this is pretty simple:

hostnamectl set-hostname

In this example, we have set “” to be the system’s new hostname. You can check this by just running the hostname command or the hostnamectl command:




To make this permanent we need to reboot the server, so run one of these commands:



shutdown now -rf

Set your CentOS hostname by editing the hostname file

There is a third way to change the hostname. First, start by editing the /etc/hostname file, we will use nano as usual:

nano /etc/hostname

Just type your new hostname, save changes and then just issue a reboot:



shutdown now -rf

And yeah, that’s it.


So today we have learned that a hostname is, to put it simply, a “name” we can assign to a computer or a server to make it easier to identify, though it can also be used as a way to set up remote connections, for example, if we need to set up an email client or an FTP client.

And now we also know that there are a few quick ways to change the hostname of a CentOS system. We can edit the network configuration files, we can use commands like hostnamectl or we can simply edit the hostname file. Among these methods, the second is our favorite, and we suggest using it in a mix with the first one. If you use those two methods together (starting with the first one) then you can be 100% sure that your hostname will be completely changed once your server or computer is back online.

About the Author: Martin Keler

Leave a Reply

Your email address will not be published. Required fields are marked *