Debian – Configure /etc/hosts

debianhostsnetworking

I was trying to get a perl script that contacts a PostgreSQL database to work on a server. This script was mysteriously failing. I then realised that localhost was not in the /etc/hosts file.

The file for this machine (currently running Debian lenny) currently looks like

127.0.0.1       machinename.domain   machinename
xxx.xx.x.xxx    machinename.domain   machinename

The xxx.xx.x.xxx is the IP address. The file for my current home machine, which is a slightly older installation (currently running Debian squeeze) is

127.0.0.1       machinename          localhost
127.0.1.1       machinename.domain   machinename

My home machine sits behind a router and is not directly exposed to the internet. In any case, I'm on DSL and have no static IP address.

I've kept /etc under version control for my machines (using etckeeper) for some time, and I see that for this server, the following change was made by some mastermind (possibly myself) on Dec 17th 2009.

-127.0.0.1      localhost
+127.0.0.1      machinename.domain  machinename

I've wondered before why this file is set up the way this is, but the answer is not obvious. Some questions:

  1. Why 127.0.1.1? This might be a
    Debian-specific bit of history. I
    did a bit of searching on the net,
    and found some vague mutterings about
    Gnome, but little of any substance.

  2. Where in Debian is the template this file is
    set from?

  3. Is there currently considered to be
    a correct/best form for this file?

  4. Is the order of the names in the
    line significant? I hope not.

More generally, what is the explanation of why these two lines are structured the way they are?

For now, I think I'll change the server /etc/hosts to

127.0.0.1       machinename.domain  machinename localhost
xxx.xx.x.xxx    machinename.domain  machinename

Comments?

Best Answer

On my system, I have the following in /var/lib/dpkg/info/netbase.postinst:

create_hosts_file() {
  if [ -e /etc/hosts ]; then return 0; fi

  cat > /etc/hosts <<-EOF
        127.0.0.1       localhost
        ::1             localhost ip6-localhost ip6-loopback
        fe00::0         ip6-localnet
        ff00::0         ip6-mcastprefix
        ff02::1         ip6-allnodes
        ff02::2         ip6-allrouters

EOF

My netbase version is 4.45.

I would expect that the first name on the line will be returned for a reverse lookup of the IP address, otherwise I doubt the order matters.

Related Question