How to setup a host file, correctly.. in Linux.

What is a host file?

A host file or /etc/hosts in Linux, it’s a locally administrated text file which is used by the operating system to translate host names into IP addresses. Keep in mind that the hosts file has a bigger priority then DNS by default, consequently the hosts are first resolved from here. The behavior can be changed via /etc/nsswitch.conf.


cat /etc/hosts myserver myrouter

In this example we have defined two hosts.
1) myserver with the IP address and the FQDN is
2) myrouter with the IP address, without a domain name, this is not a FQDN.

Just use the simple ping command, as you see in the example from below, the host is successfully translated.

ping myserver -c 1
PING myserver ( 56(84) bytes of data.
64 bytes from myserver ( icmp_seq=1 ttl=64 time=0.045 ms

What is FQDN?

FQDN is the abbreviation for Fully Qualified Domain Name, a domain name which must include at least a second-level domain and a top-level domain.
In our example
– host name is myserver
– second level-domain is example
– top-level domain is .com.

So.. the correct entry of a FQDN would be:
ip_address hostname.domain hostname

Local system’s host name

Some apps or services like Kerberos requires that the local system’s host name to be FQDN.
/etc/hostname file stores the system’s host name, if is modified the change will be effective only after reboot.

cat /etc/hostname

Check if local system’s host name is FQDN

You need to make sure that the local host name has a valid FQDN entry in /etc/hosts.

cat /etc/hosts | grep `hostname` myserver

You can also check with the hostname command.

Display the host name


Display the domain name

hostname -d

Display the FQDN

hostname -f

Leave a Reply

Your email address will not be published.