How to set the FQDN in CentOS 7?

This tutorial will explain How to set the FQDN in CentOS 7 and RHEL 7 (Red Hat Enterprise Linux 7). The documentation explicitly instructs you to use the fully qualified domain name as the machine’s static hostname. Trying to name a server with a single unqualified name causes a variety of problems with various services, most notably email. The given below is our system information, replace these values as per your system information.

What is FQDN?

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 is also recommended that the static and transient names consists only of 7 bit ASCII lower-case characters, no spaces or dots, and limits itself to the format allowed for DNS domain name labels, even though this is not a strict requirement. Older specifications do not permit the underscore, and so their use is not recommended.

The hostnamectl tool will enforce the following: Static and transient host names to consist of a-z, A-Z, 0-9, “-”, “_” and “.” only, to not begin or end in a dot, and to not have two dots immediately following each other. The size limit of 64 characters is enforced.

Checking the Hostname and FQDN

To show the current Hostname, use the command:

hostname

To show the current FQDN, use the command:

hostname -f

How to set the Hostname and FQDN?

You should type to set Hostname:

hostnamectl set-hostname server.example.com

Now you do not require to restart or logout-login from RHEL 7 and CentOS 7 which is the best thing we have found. Hostname and FQDN are set.

Check the correct hostname/FQDN with printing the hostname and FQDN of your system.

Fixing the problem after reboot

When you run ‘hostname’ command, it displays FQDN when you run ‘hostname -f’ it displays FQDN, so you are defining the correct hostname and FQDN, but after applying a reboot to the server the hostname and FQDN you defined are passing again.

[root@server ~]# hostname
server
[root@server ~]# hostname -f
server.mydomain.com

In this case we recommend you to edit manually config file: /etc/hostname It produces the same effect as a command-line tool, and you may put correct data manually, it should contain the FQDN:

# cat /etc/hostname
server.example.com

Fixing non-saving of Hostname and FQDN

Sometimes changing the hostname and FQDN is not taking effect in this case please run the command below:

systemctl restart systemd-hostnamed

Looking for a custom solution?

Our technicians can provide you with the best custom made solutions on the market, no matter whether you're a small business or large enterprise.

Get in touch