dnsmasq and /etc/hosts

Abstract

/etc/hosts.dnsmasq:
192.168.1.100 foohost
(/etc/hosts is left alone)
/etc/dnsmasq.conf:

no-hosts
addn-hosts=/etc/hosts.dnsmasq

Intro

dnsmasq will, by default, read the contents of /etc/hosts and use the information there for looking up those hosts that are configured to have a static IP on the LAN. (Hosts using dnsmasq as a dhcp-server are not specified there).

Problem

However, in Debian, Ubuntu  and others, /etc/hosts seems to contain lines that break this — something like the following (the host name is foohost in this example):

127.0.0.1 foohost localhost.localdomain localhost
127.0.1.1 foohost

A consequence is that if another host on the network for example tries to ping foohost, the DNS lookup performed by dnsmasq on foohost will return 127.0.0.1 or 127.0.1.1, which will not be the address that was intended—the other host will end up pinging itself.

Solution

Now, apparently the above /etc/hosts is broken — for Debian and derivatives it should be rewritten as

127.0.0.1  localhost
127.0.1.1 foohost

— see  [1], [2] and Google.

Further,  if the host has a permanent IP then that IP should be specified instead of 127.0.1.1. And the host running dnsmasq will need to have a static IP, so problem solved:

# Fixed /etc/hosts:
127.0.0.1  localhost
192.168.1.100 foohost

Other solution

I think I prefer to leave /etc/hosts alone though. I suspect various network configuration tools  will edit that file, and may unfix it.  dnsmasq can instead be configured to use another host-file (as suggested by Nils Kassube on ubuntu-users). So, I have added a file /etc/hosts.dnsmasq, specifying the static ip of the host running dnsmasq:

192.168.1.100 foohost

/etc/dnsmasq.conf would then need the entry no-hosts to tell dnsmasq to ignore the usual hosts-file, and the entry addn-hosts=/etc/hosts.dnsmasq which specifies what file to use instead.

(The above mentioned file, /etc/hosts.dnsmasq, could also contain other typical /etc/hosts entries for other hosts on the LAN using static ip, as described in the very nice article on enterprisenetworkingplanet … but why would we do that when all hosts can use configuration-less dhcp instead?)

[1] http://www.debian.org/doc/manuals/reference/ch-gateway.en.html#s-net-dns

[2] https://bugs.launchpad.net/ubuntu/+source/netcfg/+bug/19775/comments/23