Sunday, February 28, 2010

For home DNS & DHCP, use dnsmasq

I find that I have many devices in my home requiring IP addresses and names; laptops, netbooks, phones, nas, music player ... the list goes on.

While my current DSL modem/router will run DHCP and give each device on the LAN an IP address, if I want to refer to a device by name (e.g. 192.168.0.24 aka "nero") I have to add names and IP addresses to the hosts file on my workstation ... and (yawn) on every other computer I also want to see that device by name.

Adding to my woes, the DHCP server in the router is a bit free and easy with IP addresses, so if I restart a device it can get a different IP address which means that IP addresses move from device to device over time, which in turn means the names in my hosts files are all messed up. Darn ... and potentially harmful.

Of course all I really want to do it to write down once that *this* box should be given *this* IP address and made available with *this* name

... and the small utility dnsmasq lets me do just that. The following line says that the device with the MAC address 00:1b:62:01:e4:d5 is always to be given the IP address 192.168.0.24 and is always to be known as nero:

dhcp-host=00:1b:62:01:e4:d5,192.168.0.24,nero

This one line configures both a DHCP server and a DNS server (light-weight implementations internal to dnsmasq), so any machine on the LAN can now use the name "nero" in place of the IP address even if the IP address did change, which would only happen if the configuration line above were changed.

For the low-down on dnsmask see the project home page and the articles here and here.

A very brief how-to (assuming a Debian-like server) would go something like ...

1. Identify a machine on your LAN which runs 24/7 (I use a FitPC running Ubuntu) and install dnsmasq on it

2. Decide on a domain for your LAN, e.g. home.localdomain

3. Identify the devices on your network which will have static IP addresses. I have two, the DSL modem/router and the FitPC running dnsmasq. For these devices make entries in the /etc/hosts file of the machine running dnsmasq, e.g.:

192.168.0.1 gateway gateway.home.localdomain
192.168.0.100 server server.home.localdomain

4. Now work on the dnsmasq config file (/etc/dnsmasq.conf). First take a copy of the default file because it has lots of useful information in there. Then edit the file with details of your network and devices. You can see an example based on what I did for my home network below, but do refer to the docs and the original config file to understand the detail.

5. Stop any other DHCP server you may have running (e.g. the one in the DSL modem/router)

6. On the machine running dnsmasq: sudo /etc/init.d/dnsmasq restart

7. Restart the network interfaces on your devices (or just reboot the computer, phone or whatever)

8. Test that everything is as you expect. e.g. does ping nero work? Does ping nero.home.localdomain work?

9. Done

The example file:

domain-needed
bogus-priv
expand-hosts
domain=home.localdomain
dhcp-range=192.168.1.100,192.168.1.150,1h

dhcp-host=00:1b:62:01:e4:d5,192.168.0.24,nero
dhcp-host=00:1c:f0:06:25:ca,192.168.0.25,see
dhcp-host=00:23:76:cd:a7:9a,192.168.0.26,rome
dhcp-host=00:14:a6:2d:a7:9a,192.168.0.27,burn

dhcp-option=option:router,192.168.0.1
dhcp-option=option:domain-name,home.localdomain