How to Setup the Hostname and FQDN on CentOS / RHEL
Posted onSetting up your hostname and Fully Qualified Domain Name (FQDN) can be a bit of a challenge. A lot of tutorials on the Internet are outdated and refer us to all kinds of different files. In this tutorial, I’ll show you how to permanently set up your hostname and FQDN on CentOS so that the following commands work as expected:
hostname
This should return the hostname.
hostname -f
This should return the fully qualified domain name.
hostname -d
This should return the domain name.
Quick Explanation of Terms
Just so that we’re clear on 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:
newhostname.dummyserver.com
It means that the hostname is “newhostname”, the domain is “dummyserver.com” and the FQDN is “newhostname.dummyserver.com”. Note that the hostname is just a convention. There’s nothing stopping us from having the hostname be the same as the FQDN itself (more on this later).
We want to set up CentOS so that these values are returned by the regular commands:
- hostname
- hostname -f
- hostname -d
Setting up the Hostname
We can get our current hostname simply by typing the command:
hostname
On a fresh server like mine, the three commands return the following:
You can see that I have neither a domain name set up, nor a FQDN. The value for the hostname is stored in a file called:
/etc/hostname
Here are the contents of that file as expected:
Temporary Hostname Change
We can temporarily change the hostname (transient hostname) by using the command:
hostname [newname]
This will change the hostname to “newname”, but the changes won’t persist after a reboot. Neither does it allow us to change the domain name, nor get the FQDN. Here’s a screenshot of what it looks like after running the command:
This transient hostname doesn’t persist after a reboot as shown here:
We’re back to where we started. So how do we make this change permanent?
Permanently Changing the Hostname
To ensure that our hostname remains changed, we use the following command:
hostnamectl set-hostname [newhostname]
When we run this command, it makes the change directly in /etc/hostname. You can see that the value has changed in the file:
We also see that it persists after a reboot:
Now we’ll see how to set the domain name and FQDN.
RedHat/RHEL Recommendations Regarding Hostnames and FQDN
It turns out that RedHat wants to do away with the practice of having a hostname that’s separate from the FQDN. In their documentation, here’s what it has to say:
A host name can be a free-form 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:
newhostname.dummyserver.com
instead of
newhostname
If you want to go ahead with their recommendations, you can simply set the hostname as the FQDN and be done with it. Keep in mind though, that the commands “hostname -d” and “hostname -f” won’t work as expected. If on the other hand, you want a separate domain name and FQDN, read on!
Setting up the Domain Name via the /etc/hosts File
Now that you’ve set up the hostname via the /etc/hostname file, we need to use the following file to set the FQDN:
/etc/hosts
Open this up using an editor like “vi” and append the following line to the bottom:
[IP Address] [your FQDN] [your hostname]
For a local test server like mine, here’s my /etc/hosts entry:
Save your changes. Now when you run the command “hostname -d”, the system will parse the FQDN and return the domain name as expected. Here’s the output of “hostname -f” and “hostname -d” after making the above changes in /etc/hosts:
And we’ve done it! These changes will persist after boot. We’ve managed to change our hostname, set up our FQDN and identified our domain name in CentOS/RHEL.
Are you completely sure that you should do this on /etc/hosts and not /etc/hostname?
node1.domain.topdomain in /etc/hostname would give you:
hostname node1.domain.topdomain
hostname -f node1.domain.topdomain
hostname -d domain.topdomain
/etc/hosts is for name resolution
“/etc/hostname” contains only the server hostname.
By putting the ip address and hostname into “/etc/hosts”, you ensure that local connections will use 127.0.0.1 instead of the network ip.