Include domain name or not in /etc/hosts?
Putting the domain name in /etc/hosts
is optional, and you can run a system without any ill effect at all.
The only downside of leaving it out is that the system's fully qualified hostname won't show up properly. For example, hostname -f
.
The way detection of the fully qualified host name works:
- It first gets the hostname, or 'shortname'. This is the output of
uname -n
orhostname
. - It then gets the IP address for that hostname by consulting
/etc/hosts
(or whatever you have in/etc/resolv.conf
, and falling back to the latter sources if not found in/etc/hosts
). - Once it has the IP it then does a reverse lookup by again consulting
/etc/hosts
. - Once it has a record in
/etc/hosts
, the first entry is used as the fully qualified hostname.
In a nutshell, if you want fully qualified hostname to work, you should do either:
127.0.0.1 fully.qualified.hostname hostname localhost.localdomain localhost
or
127.0.0.1 localhost.localdomain localhost
1.2.3.4 fully.qualified.hostname hostname
As long as your host agrees with your domain name, specifying it or not in /etc/hosts
won't change anything. Another practice is to have it specified as the domain
parameter in /etc/resolv.conf
. Not specifying it could simplify your life if, one day, your network administrator changes it. Specifying it doesn't change anything, as far as I know.
You should definitely leave 127.0.0.1 localhost
or 127.0.0.1 localhost localhost.localdomain
in /etc/hosts
. Some applications could start having a really strange behaviour is localhost
binds to anything other than your loopback address because this is a really, really, unexpected setting.
You have to specify fully qualified domain name in the first position after IP address.
As we read in man 5 hosts
:
For each host a single line should be present with the following information:
IP_address canonical_hostname [aliases...]
If not following the rule, then some software may break. For example, Puppet configuration management system may start changing your hostname back and forth on each run just because you did not specify domain part or because you put alias without domain on the line before FQDN.