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

Advertisements

5 Comments on “dnsmasq and /etc/hosts”

  1. […] dnsmasq is configured to ignore the usual /etc/hosts and use a custom hosts-file instead, /etc/hosts.dnsmasq,  containing something like the following (”foohost” is the name […]

  2. Wonderfull, I have been struggling to set up BIND9, I have it set up, but I want simple ability to add an entry like Windows7Personal and have it point to my computers IP without having to set up Windows7Personal.domain.local etc etc! I currently use DNSMasq on my Router, but I have been meaning to move this function to a Server for a while now.

  3. This is my first time go to see at here and i am truly impressed to read all at alone place.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s