Sunday, October 21, 2012

Dnsmasq as a local DNS and DHCP server

Lately it is fairly common to have quite a few devices connected in local network, be it wireless or wired: computers, tablets, smartphones, media players, gaming consoles... wouldn't it be nice to call them by name instead of IP?
Also some internet providers rent you a router to access their services, which may have few configuration options; in this case if it gives you by default  unreliable DNS servers and no way to change them you could get a lot of undesired errors when contacting internet servers.
Finally maybe you have an old or low power computer you can leave on 24/7...

Dnsmasq to the rescue: three birds with one stone!

You could use a low spec (and hopefully low power drain) computer, like a Raspberry PI or similar (or my faithful Dockstar, see http://archlinuxarm.org/platforms/armv5/seagate-dockstar), to solve both issues using dnsmasq; this machine from now on will be called server.
All major distributions have a package for it, so I won't go into specifics on how to install it or have it run at startup and go straight to configuration.

Problem 1: home computer by names, not IP

That's the easiest part, just edit /etc/hosts on server:
...
192.168.1.1 router router.local livebox livebox.local
192.168.1.100 eeepc eeepc.local
...
and restart dnsmasq.
If you plan not to use dnsmasq as DHCP server, configure all other devices to use server as DNS server, using your usual one  as secondary server; otherwise, take a look at point 2 for a better alternative.

Problem 2: replace broken router DHCP

If you go further and configure dnsmasq to also be a DHCP server you won't have to configure the other devices one by one; let's see how. First remember to give the server a static address; it won't know it otherwise!
Let's edit /etc/dnsmasq.conf:
domain=local
dhcp-range=192.168.1.100,192.168.1.200
dhcp-host=00:11:22:33:44:55,192.168.1.100,eeepc
dhcp-option=3,192.168.1.1
dhcp-option=6,192.168.1.101,8.8.8.8,8.8.4.4,192.168.1.1
this way your eeepc will always get the same IP address and will be given the name "eeepc"; the address before the IP is the mac address. You also configure it to give addresses in the range 192.168.1.100 - 192.168.1.200 and to use the router (192.168.1.1) as gateway (option 3) and to use a list of DNS (option 6 - 192.168.1.101,8.8.8.8,8.8.4.4,192.168.1.1), which will be used in order to resolve host names.
In the fairly common case of a device (like a netbook or notebook) which has bot ethernet and wifi and you want to have it always pick the same IP:
dhcp-host=00:11:22:33:44:55,66:77:88:99:AA:BB,192.168.1.100,eeepc
where the two mac addresses are for ethernet and wifi (the order doesn't matter in the common case of the two interfaces never being used together). 
No further configuration is needed on the clients, just set them to dhcp if they're not and you're done; you will have to renew the DHCP lease, this varies depending on the device.
Restart dnsmasq and turn router's DHCP off and you will have the comfort of dhcp and the full freedom of configuring it the way you likeit. 

This is just a simple example, check dnsmasq doc out to know about its many options and as always: enjoy :-) 

2 comments:

Webmaster said...

I'm curious about the 192.168.1.101 in the example:

dhcp-option=6,192.168.1.101,8.8.8.8,8.8.4.4,192.168.1.1

Is that a secondary DNS in your example?

Riccardo Cossu said...

@Webmaster
It's just to be sure that the server which hosts the local names is always called first; it is actually the one where DNSMasq is running