Sunday, April 06, 2014

Dnsmasq: DNS & DHCP for a small network

For a small network like a home network, dnsmasq is an appropriate local DNS and DHCP server. The DNS is dynamic in the sense, that if a computer is assigned an IP by the DHCP server, it will show up in the DNS, as well.

NAT network and the virtual machines

For a first experience, I started with VirtualBox. Created a NAT network, vb.nat (192.168.4.0/24). For the installation of the virtual machines, DHCP is enabled in this network. Installed two virtual machines, dnsmasq.vb.nat (ubuntu-12.04.3-server-amd64) and client.vb.nat (ubuntu-13.10-desktop-amd64). For dnsmasq.vb.nat, the "SSH server" packages were chosen at installation.

To install VirtualBox guest additions, some packages are needed. Ubuntu server does not automount the CD and does not autostart the installation. The following commands do the installation:
frigo@dnsmasq:~$ sudo apt-get install build-essential dkms
frigo@dnsmasq:~$ sudo mount /dev/sr0 /mnt
frigo@dnsmasq:~$ sudo /mnt/VBoxLinuxAdditions.run
frigo@dnsmasq:~$ sudo eject
The VirtualBox guest additions are easy to install to client.vb.nat, using the graphical interface of the desktop Ubuntu.

Preparation: hosts and interfaces

After installation, the /etc/hosts file contains the name of the server with the 127.0.0.1 IP address that leads to a lot of trouble, so fix it as the first step:
frigo@dnsmasq:~$ sudo vi /etc/hosts
127.0.0.1       localhost
192.168.4.15    dnsmasq.vb.nat  dnsmasq
The second step of preparation is to set a fixed IP address for the DNS and DHCP server.

frigo@dnsmasq:~$ sudo vi /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
        address 192.168.4.15
        netmask 255.255.255.0
        network 192.168.4.0
        broadcast 192.168.4.255
        gateway 192.168.4.1
        dns-nameservers 8.8.8.8 # Google Public DNS
        dns-search vb.nat
Testing: 
frigo@dnsmasq:~$ sudo service networking restart

Install and configure dnsmasq

The dnsmasq package is easy to install. The default configuration file contains 500+ lines of documentation and examples in the form of comments. The installation and the options that worked for me:
frigo@dnsmasq:~$ sudo apt-get install dnsmasq
frigo@dnsmasq:~$ sudo vi /etc/dnsmasq.conf

# Never forward plain names (without a dot or domain part)
domain-needed
# Never forward addresses in the non-routed address spaces.
bogus-priv
# Listen on eth0 (this option may be superfluous)
interface=eth0
# Set the domain for dnsmasq.
domain=vb.nat
# Enable the integrated DHCP server
dhcp-range=192.168.4.50,192.168.4.150,12h
# Override the default route supplied by dnsmasq
dhcp-option=3,192.168.4.1

Finished. Disable DHCP in VirtualBox.

Testing - use Alt-F1 and Alt-F2 to have two text consoles or use terminal windows in the graphical interface of client.vb.nat to ssh into dnsmasq:
frigo@dnsmasq:~$ tail -f /var/log/syslog
frigo@dnsmasq:~$ sudo service dnsmasq restart
You can restart client.vb.nat and see the DHCP handshake in dnsmasq syslog. Some simple tests:
frigo@client:~$ nslookup dnsmasq.vb.nat
Server:        127.0.1.1
Address:    127.0.1.1#53

Name:    dnsmasq.vb.nat
Address: 192.168.4.15

frigo@client:~$ nslookup client.vb.nat
Address: 192.168.4.147

frigo@client:~$ nslookup ubuntu.com
Address: 91.189.94.156

Useful information

This blog entry was inspired by the three articles about configuring isc-dhcp-server and bind9 at raerek.blogspot.hu (in Hungarian):
  1. http://raerek.blogspot.hu/2012/03/dhcp-szerver-ubuntu-1204-en.html
  2. http://raerek.blogspot.hu/2012/04/ddns-ubuntu-1204-en-elso-resz.html
  3. http://raerek.blogspot.hu/2012/04/ddns-ubuntu-1204-en-masodik-resz.html
 The most important sources of information used for my dnsmasq setup:
  1. https://wiki.debian.org/HowTo/dnsmasq 
  2. https://help.ubuntu.com/community/Dnsmasq
  3. http://xmodulo.com/2012/10/how-to-set-up-dhcp-server-using-dnsmasq.html
  4. http://edoceo.com/howto/dnsmasq

No comments: