Wednesday, 4 June 2014

Using a Raspberry PI as DNS and DHCP tool

This morning I set up my RPI to run DNS for my network. This means I can resolve internal machines (the PI itself and the iMac downstairs) and make use of internal DNS caching, save going out to Virgin for every DNS lookup.

It's actually trivially easy to do. If you're already using udhcpd you may want to switch, as you can do it all in one config file, but you don't have to.

Firstly apt-get the package:

apt-get install dnsmasq

You then need to edit the following lines in /etc/dnsmasq.conf. Look for the following settings, and change them for your own settings:

server

This should be the address of a good external DNS, so either your ISP one, or I've used Google's DNS'

server=8.8.8.8
server=8.8.4.4

domain

This should be the name of your internal network. As it happens mine is softfox.net. This enables you to be able to resolve addresses on your network, so in my case pi.softfox.net

domain=softfox.net

dhcp-range

If you're going to use dnsmasq for DHCP you need to edit this, but you don't have to. If you are using a different DHCP (for instance, udhcpd) you'll need to update the network DNS settings it hands out to your PI.

dhcp-range=192.168.0.100,192.168.0.199,12h

This takes the form of range start, range end, lease time. There are other options you can do for multiple ranges on different networks and all kinds of things

dhcp-host

I've not used this, but this is how you hand out a specific IP to a MAC address - could be useful. I actually have my fixed things on a fixed IP so didn't need it.

dhcp-option

Important one this - dnsmasq assumes that it's running on the router, but in my case my router is a virgin box on 192.168.0.1, so you have to set this up to point to that box.

dhcp-option=option:router,192.168.0.1

Again there are ton of things you can do here, but I have a fairly simple setup. There are examples of setting up WINS names and so on, but I have an all mac network, so don't worry about such things. You can also set up bootp and tftp for network boots from this - again, I have no need of such things.

cache-size

This sets the number of DNS address dnmasq will cache, and I would advise you to set it to it's maximum setting. It's way quicker to get the address from your own network than go out to your ISP, so I've set mine to the maximum, 10000

cache-size=10000

Stephen Wood has done a really cool post about this with more information, and some scripts to help you profile the improvements here so thanks, Steve for that, and getting me up and running so quickly.

No comments:

Post a Comment