dnsmasq and /etc/hostsPosted: February 24, 2009
(/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):
127.0.0.1 foohost localhost.localdomain localhost
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.
Now, apparently the above /etc/hosts is broken — for Debian and derivatives it should be rewritten as
— see ,  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:
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:
/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?)