Add new comment

The correct contents for /etc/hostname and /etc/hosts on GNU/Linux

This is a short description of what the correct contents of /etc/hosts and /etc/hostname on a GNU/Linux (or another UNIX-like) system is. Here's what to take away from this discussion on LinuxQuestions.

There is actually some conflicting information about /etc/hostname. Debian's documentation says it should be the plain hostname. The linked thread and Red Hat/CentOS docs could be interpreted to say it should be an FQDN. It's not used for DNS at all. My personal bias is to trust more in Debian's judgement and this seems to be in line with how hostname(1) handles the --fqdn parameter. My choice is to have just the plain hostname.

Example hostname file:


/etc/hosts should contain the plain hostname as well as the FQDN for your external IP. If you have multiple IPs for a system, it could be a good idea to have different names associated with them. It's certainly not useful to have the same name defined for each as only the first one will be used.

In addition to this, the loopback line should never contain the name of the machine because this can be a problem for some services. I must add that I once had a problem with the Glassfish application server that I could only solve by adding the hostname there though.

Another rule with /etc/hosts is that even if you have many aliases for a given address, it's not allowed to split the definition to multiple lines pointing to the same IP. This may seem to work but can cause problems. One of these is that gethostbyaddr uses /etc/hosts also for finding a host for an IP and multiple lines for one could result in inconsistent behavior.

Example hosts file: localhost.localdomain localhost host


Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Fill in the blank.