4

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?

6

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 set-hostname 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
0

To correctly set up hostname and FQDN on CentOS 7, you need to set up /etc/hostname to contain short, node name, like:

# echo server > /etc/hostname

And you need to add domain entry in resolv.conf:

# echo 'domain mydomain.com' >> /etc/resolv.conf

Reboot afterwards.

Note: entry from resolv.conf can be lost after reboot, so to keep it, it may be neccesarry to set up DOMAIN= in your network-scripts, like:

echo 'DOMAIN="mydomain.com"' >> /etc/sysconfig/network-scripts/ifcfg-eth0

Replace eth0 with your appropriate interface, like for example enp3s0.

-1
cat /etc/hostname
server01

cat /etc/hosts

192.168.100.10 server01.test.com server01

Now you can check it.

hostname (would output) server01

hostname -f (would output) server01.test.com

-3

Echo "kernel.hostname = my.com" >> /etc/sysctl.conf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.