Cannot setup hostname and FQDN Centos 7

John Doe asked:

I’m now running CentOS-7.0-1406 and looks like i can’t setup hostname properly. As far as i know, you need to setup hostname using hostnamectl set-hostname command and write FQDN in /etc/hosts.
I have a centos machine and i want to set it’s hostname to “server” and FQDN to “server.mydomain.com”. I run hostnamectl command and edit /etc/hosts file:

[root@server ~]# hostnamectl set-hostname server
[root@server ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

{inet_IP_here} server.mydomain.com server 

At first everything looks fine, console displays hostname when i run hostname and it displays FQDN when i run hostname -f:

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

BUT after i reboot machine and run the same commands again, it starts to display FQDN as hostname:

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

I must say that it’s a VPS server and i have no such problem when i do it on a local virtual machine. Also there is no any settings in VPS control panel which look like hostname. What reason might cause such problems?

My answer:


The Red Hat 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.

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.

You should be doing:

hostnamectl server.example.com

You can also manually edit /etc/hostname for the same effect; again, it should contain the FQDN.

# cat /etc/hostname
server.example.com

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.