dnsmasq offers a lightweight, functional and integrated DHCP and DNS service. Using it on OpenWRT brings up and embedded, flexible DNS service, with a very small footprint, for small or home offices.
dnsmasq acts as a caching DNS server and DHCP server. It reserves a DNS domain, called the local DNS domain and usually being
.lan, for local name resolution. When queried for an A RR inside the local DNS domain, dnsmasq looks at file
/etc/hosts for a match. If one is found, its corresponding IP is returned as the query result. When queried for a PTR RR, it looks into file
/etc/hosts for a match by IP and, if one is found, its correspoding hostname, qualified with the local DNS domain, is returned. Thus,
/etc/hosts behaves much like a DNS master zone file.
Also, if the DHCP server funcionality of dnsmasq is enabled, when a query under the local DNS domain fails (no record is found in
/etc/hosts), it will try to resolve the query from the DHCP lease database.
The DHCP lease database is usually stored at
/tmp/dhcp.leases. Its format is pretty simple: it’s a text file, where each line represents an active DHCP lease. Each line is made up of five fields:
- Time of lease expiration
In epoch time (seconds since 1970). States when the lease will expire. Most DHCP clients will try to renew the lease when it reaches 80% of its valid lifetime.
- Client MAC address
The MAC address corresponding to the client to which the lease belongs.
- Leased IP address
A valid IP address, taken from the DHCP pool, which is actually and currently leased to the client whose MAC address is listed in the previous field.
- Client hostname
If known, holds the unqualified host name of the client machine. Else, an asterisk is stored here.
- Client ID
Simon Kelley defines it as:
The client-ID is used as the computer’s unique-ID in preference to the MAC address, if it’s available. Some DHCP clients provide it, and some don’t. The ones that do normally derive it from the MAC address unless explicity configured, but it could be something like a serial number, which would protect a computer from losing its identify if the network interface were replaced.
If not know, an asterisk is stored here.
A sample DHCP database lease:
# cat /tmp/dhcp.leases 1147729862 00:16:3e:3b:56:f1 192.168.0.11 rhel * 1147725355 00:0c:29:09:3d:58 192.168.0.10 rhel-devel *
In this case, there are two active DHCP leases, one for client
rhel, another one for
OpenWRT uses a rc.d script stored at
which, for a squashfs firmware is a symbolic link to
/rom/etc/init.d/S50dnsmasq. This rc.d script tries to configure the dnsmasq daemon using NVRAM variables, which helps a lot when reflashing. However, I have found more convenient to the use the traditional
/etc/dnsmasq.conf file instead.
Replacing the OpenWRT rc.d script with a custom one, in order to leverage
dnsmasq.conf, is as simple as removing
/etc/init.d/S50dnsmasq and invoking the dnsmasq daemon directly:
rm -f /etc/init.d/S50dnsmasq cat > /etc/init.d/S50dnsmasq < < EOF #/bin/sh /usr/sbin/dnsmasq EOF
Here is a sample of a
/etc/dnsmasq.conf file I use on my Linksys WRT54G router running OpenWRT White Russian RC5:
# filter what we send upstream domain-needed bogus-priv filterwin2k localise-queries # allow /etc/hosts and dhcp lookups via *.lan local=/lan/ domain=lan expand-hosts # enable dhcp (start,end,netmask,leasetime) dhcp-authoritative dhcp-range=10.0.0.10,10.0.0.100,255.255.255.128,12h dhcp-leasefile=/tmp/dhcp.leases # use /etc/ethers for static hosts; same format as --dhcp-host # [hwaddr] [ipaddr] read-ethers # other useful options: # default route(s): dhcp-option=3,192.168.1.1,192.168.1.2 # dns server(s): dhcp-option=6,192.168.1.1,192.168.1.2 dhcp-option=3,10.0.0.126 dhcp-option=6,10.0.0.122