dnsmasq and /etc/hosts


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



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).


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): foohost localhost.localdomain localhost 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 or, which will not be the address that was intended—the other host will end up pinging itself.


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

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

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

# Fixed /etc/hosts:  localhost 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: 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