dnsmasq

Abstract

The host running dnsmasq:

1) Wherever  Static IP is configured the first “DNS Servers” entry  should specify 127.0.0.1 and the second should specify 192.168.0.254 (or whatever IP the nearest upstream DNS server has).

2) /etc/hosts.dnsmasq:

foohost 192.168.1.100

3)  /etc/dnsmasq.conf

domain-needed
bogus-priv
no-hosts
addn-hosts=/etc/hosts.dnsmasq
expand-hosts
domain=foolan
dhcp-range=192.168.1.50,192.168.1.100,12h
dhcp-host=ahost,192.168.1.70,30m
#dhcp-host=otherhost,192.168.1.71,12h
#dhcp-host=yetanotherhost,192.168.1.72,30m
dhcp-option=option:router,192.168.1.1

Intro

I have a few computers at home (all running Ubuntu Linux 8.10, Intrepid Ibex) forming a small home network. I use dnsmasq to achieve the following:

  • Any host can be plugged in “without configuration”, i.e the dhcp-server functionality within dnsmasq is used.
  • After having been plugged in, hosts are able to ping or connect to each other by host name (thus not only by ip numbers), still without configuration, i.e that dhcp-server will be used together with the DNS functionality that dnsmasq provides.
  • Some of the hosts need a “constant” IP within the LAN (or “static” if you will, though using dhcp). This is needed for port forwarding to these hosts (so that they can be used for bittorrent and as various types of servers). We achieve this by letting dnsmasq manage a list of hostnames and their respective “constant” IP addresses within the LAN. So there is only one place within the whole LAN where that information is maintained, see below. The hosts themselves are still using zero configuration dhcp.
  • dnsmasq also works as a local dns cache for the LAN, which speeds up web browsing.
  • All this is very easy to set up

I tried for a while to set up a WINS server (using samba), but I could never really make that working..

A limitation is that the host running dnsmasq needs to be up pretty all the time (or at least whenever a host on the network needs web access). I have a desktop computer constantly turned on anyway (hosting a few server processes) that dnsmasq can run on.

Another option would be to install it dnsmasq on a router. This would be your only option if there is only Windows computers on your network, because dnsmasq does not run on Windows. I have no experience with this, but see for example Tomato Firmware.

So, how do we set up all this?

The host running dnsmasq has a static ip within the LAN. We want other applications on that same host to use dnsmasq for DNS services, and not some other DNS server on some other host. So, within Network Manager (or wherever that Static IP was configured) the first “DNS Servers” entry  should specify 127.0.0.1. When the connection is enabled, NetworkManager will generate a /etc/resolv.conf containing something like the following:

# Generated by NetworkManager
nameserver 127.0.0.1

By default, dnsmasq will read /etc/resolv.conf and use the hosts specified there (the lines starting with “nameserver”) as “upstream nameservers” to forward non-local DNS-requests to. In this case that file will point back to localhost (127.0.0.1), see above. But that’s ok, dnsmasq will understand to ignore 127.0.0.1 and look at the other lines in /etc/resolv.conf. In my case I will need to add the ip address that my adsl-modem has on the LAN, because the modem in turn is auto-configured by the ISP (dhcp) to know what dns servers to contact further upstream. So, within Network Manager (or wherever that Static IP was configured) the second “DNS Servers” entry  should specify 192.168.0.254. When the connection is enabled, NetworkManager will then generate a /etc/resolv.conf containing something like the following:

# Generated by NetworkManager
nameserver 127.0.0.1
nameserver 192.168.0.254

Further, 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 of the host running dnsmasq)

foohost 192.168.1.100

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

Wrap-up

A suitable /etc/dnsmasq.conf is shown above. (As always, do a backup of the original before experimenting.) Most of the options are well documented elsewhere (for example in the example configuration file and the man-page). Note the dhcp-option=option:router,192.168.1.1 line which tells dnsmasq to tell any dhcp-clients what the gateway of the network is. In my case I’ve specified the address to my router.

Advertisements


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