The DNSMasq is a service that combines DNS and DHCP in an elegant and easy to configure way. It’s focus is to consume as little memory resources and disk space as possible, therefore it’s highly recommended for simple networks with only a couple of hosts.
With those characteristics, it’s very common to see it being used on Linux distributions aimed at home routers, like the OpenWrt or DD-WRT. For the same reason, it’s also perfect to be run on a Raspberry Pi.
In this post I’ll run through and explain a simple configuration that I have used for my local network.
The DNSMasq is available as a .deb
package for the Raspbian, so
it can be installed through an apt-get:
The main configuration file is on /etc/dnsmasq.conf
. It’s possible to write
the whole configuration directly in it, nonetheless, it’s better to create new
files in the /etc/dnsmasq.d
directory for organization. In order for this to
be possible, it’s necessary to uncomment the last line of the
/etc/dnsmas.conf
file:
From now on, every text file created on /etc/dnsmasq.d
will be used as the
service configuration:
I have used
network.conf
for the filename, but anything else is fine.
The first step is to configure a domain name. It’s defined in the domain
option:
This domain dame will be accessible only locally and through this DNS server.
By default, the DNSMasq will use the nameserver
addresses defined in the
/etc/resolve.conf
as the outside DNS Servers. But there is also the
no-resolv
option that disables that behaviour and then it’s possible to
configure the DNS manually:
I have used the OpenDNS instead of the DNS provided by my ISP. GoogleDNS is also another good option.
For more configurations on the DNS service, consult the DNSMasq manual page.
Following the DNS configuration, we follow on to the DHCP configuration. The
first configuration is the dhcp-range
that defines a range of IP addresses
that are going to be distributed among the clients when joining the network:
In this case, the server will assign addresses from 10.0.0.50
up to
10.0.0.59
, inclusive. It’s important to adjust the range according to how
many devices will be connected to the network simultaneously.
It’s also important to notice the 120m value configured together with the address range. This value is the lease time for this range.
The lease time configuration is something that varies with application. The most usual value is 24h, which is already pretty adequate for most home networks. Shorter periods are usually used for public WiFi networks, like in stores and restaurants, where there is a great rotation of clients accessing the network.
After defining the DHCP range and the lease time, we can define the default gateway that will be advertised to the clients:
There is no direct configuration for the default gateway but it’s possible to
use the dhcp-option
, which directly sets DHCP options that will be
advertised.
The first argument is the option value, according to the RFC 2132
(or this IANA page) directly or the enum option:router
. The
seccond argument is the value that will be advertised. In this case
10.0.0.254
, which is the IP address of the ISP’s modem/routers.
Finally, the dhcp-authoritative
is to indicate that this is the main DHCP
server of the network.
For more options on how to configure the DHCP, consult the DNSMasq manual page.
After writing and saving the configuration file, it’s necessary to restart the dnsmasq service and everything is ready to be used:
Fixed Address Hosts
Since there is a limited IP address range being distributed with limited lease time, it’s inevitable that sooner or later the address advertised for a certain device will be different.
Usually this is not a problem, but in certain situations it’s important to define fixed IP addresses for certain elements. The most common cases in a domestic network are file servers or printers, but they can also be necessary for setting up NAT Port Forwarding.
To create a fixed address host we use the dhcp-host
configuration:
In this case, the 10.0.0.1
is reserved for the device with
AA:BB:CC:DD:EE:FF
MAC address.
Notice that the lease time used is infinite, meaning that the host will never change the IP address. It’s also possible to define limited leases, but it doesn’t make much sense.
Also notice that the addresses are not in the other DHCP range.
Something that is very interesting in the DNSMasq is that by specifying a
hostname for a fixed address device, it will be already included in the DNS
service. Following the example, next time it’s necessary to connect to the
printer from a PC, instead of pointing to 10.0.0.2
, it’s possible to access
printer.raspberry.local
(remember the domain name configured in the
beginning):
References
- DNSMasq Man Page
- Hey Stephen Wood’s English Tutorial (Focused on DNS Caching)
- Raspberry Pi Forum Post