How to Setup the Hostname and FQDN on CentOS / RHEL

Configuring your host name and fully qualified domain name (FQDN) can be challenging. Many tutorials on the Internet are outdated and refer us to all kinds of different files. In this tutorial, I will show you how to permanently configure your hostname and FQDN on CentOS so that the following commands work as expected: This should return the hostname

. This should return the fully qualified domain name.

This should return

the domain name.

Quick explanation of terms

Just to make us clear about what we’re talking about, the FQDN (fully qualified

domain

name) looks like a subdomain address.

The hostname is the first part, followed by the domain name and a “.com” or “.net” or whatever. So, if we have the string:

it means that the hostname is “newhostname”, the domain is “dummyserver.com”, and the FQDN is “newhostname.dummyserver.com”. Note that the hostname is only a convention. There’s nothing stopping us from having the hostname the same as the FQDN itself (more on that later).

We

want to configure CentOS so that these values are returned by the regular

commands: hostname hostname -f hostname

  1. d

Hostname configuration

We can get our current hostname by simply typing the command

:

On a new server like mine, all three commands return the following:

centos hostname

You can see that I have neither a domain name configured nor an FQDN. The host name value is stored in a file called

:

Here is the contents of that file as expected

:

output of etc hostname

Temporary hostname

change

We can temporarily change the hostname (transient hostname) using the command:

This will change the host name to “newname”, but the changes will not persist after a reboot. It also does not allow us to change the domain name, or obtain the FQDN. Here’s a screenshot of what it looks like after running the command

: temporary change

This transient hostname does not persist after a reboot as shown here:

<img src="https://webhostinghero.org/wp-content/uploads/2017/07/after-reboot-hostname-reverted.jpg" alt=

” />

We return to where we started. So how do we make this change permanent?

Permanently

change the hostname

To make sure our hostname remains changed

, we use the following command:

When we run this command, it makes the change directly in /etc/hostname. You can see that the value has changed in the file

: hostname file changed

We also see that it persists after a reboot:

<img src

=”https://webhostinghero.org/wp-content/uploads/2017/07/hostname-changes-are-permanent.jpg”

alt=”” />

Now we will see how to configure the domain name

and FQDN.

RedHat/RHEL recommendations regarding hostnames and FQDNs

It turns out that RedHat wants to eliminate the practice of having a host name separate from the FQDN. In your documentation, here’s what you have to say:

A hostname can be a freeform string up to 64 characters in length. However, Red Hat recommends that both static and transient names match the fully qualified domain name (FQDN) used for the machine in DNS, such as host.example.com.

It means that RedHat wants

your hostname to be something like:

instead of If you want to go ahead with your recommendations, you can simply set the hostname as FQDN and be done with it. However, note that the “hostname -d” and “hostname -f” commands will not work as expected. If, on the other hand, you want a separate domain name and FQDN, read on!

Configuring the

domain

name through the /etc/hosts file

Now that you have configured the hostname through the /etc/hostname file, we need to use the following file to set the FQDN

:

Open this using an editor like “vi” and add the following line at the end:

For a local test server like mine, Here is my entry /etc/hosts:

modify etc hosts for domain name

Save your changes. Now, when you run the “hostname -d” command, the system will parse the FQDN and return the domain name as expected. Here is the result of “hostname -f” and “hostname -d” after making the above changes in /etc/hosts:

Set hostname and FQDN on CentOS

And we’ve done it! These changes will persist after boot. We have managed to change our hostname, configure our FQDN and identify our domain name on CentOS/RHEL.

Contact US