Last week, we learned a batch of tips and tricks for Dnsmasq. Today, we’re going more in-depth into configuring DNS and DHCP, including entering DHCP hostnames automatically into DNS, and assigning static IP addresses from DHCP.
You will edit three configuration files on your Dnsmasq server:
/etc/hosts. Just like the olden days when we had nice clean configuration files for everything, instead of messes of scripts and nested configuration files.
Use Dnsmasq’s built-in syntax checker to check for configuration file errors, and run Dnsmasq from the command-line rather than as daemon so you can quickly test configuration changes and log the results. (See last week’s tutorial to learn more about this.)
Taming Network Manager and resolv.conf
Disable Network Manager on your Dnsmasq server, and give its network interfaces static configurations. You also need control of
/etc/resolv.conf, which in these modern times is usually controlled by other processes, such as Network Manager. In these cases
/etc/resolv.conf is a symbolic link to another file such as
/var/run/NetworkManager/resolv.conf. To get around this delete the symlink and then re-create the
/etc/resolv.conf file. Now your changes will not be overwritten.
There are many ways to use Dnsmasq and
/etc/resolv.conf together. My preference is to enter only 127.0.0.1 in
/etc/resolv.conf, and enter all upstream nameservers in
/etc/dnsmasq.conf. You don’t need to touch any client configurations because Dnsmasq will provide all network information to them via DHCP.
This example configuration includes some typical global options, and then defines a single DHCP address range. Replace the italicized values with your own values.
# global options domain-needed bogus-priv no-resolv filterwin2k expand-hosts domain=mydomain.net local=/mydomain.net/ listen-address=127.0.0.1 listen-address=192.168.10.4 # DHCP range dhcp-range=192.168.10.10,192.168.10.50,12h dhcp-lease-max=25
dhcp-range=192.168.10.10,192.168.10.50,12h defines a range of 40 available address leases, with a lease time of 12 hours. This range must not include your Dnsmasq server. You may define the lease time in seconds, minutes, or hours. The default is one hour and the minimum possible is two minutes. If you want infinite lease times then don’t specify a lease time.
dhcp-lease-max=25 defines how many leases can be active at one time. You can have large address pool available and then limit the number of active leases to prevent denial of service problems from hosts going nuts and demanding a lot of DHCP leases.
DHCP Zones and Options
You can define DHCP zones for different subnets, like this example that has an
eth and a
wifi zone, and then give each zone different options. This example shows how to define the zones:
The default route advertised to all clients is the address of your Dnsmasq server. You can configure DHCP to assign each zone a different default route:
How do you know that 3 is the default route option? Run
dnsmasq --help dhcp to see all the IPv4 options.
dnsmasq --help dhcp6 lists the IPv6 options. (See
man 5 dhcp-options for more information on options.) You may also use the option names instead of the numbers, like this example for your NTP server:
Upstream Name Servers
Controlling which upstream name servers your network uses is one of the nicer benefits of running your own name server, instead of being stuck with whatever your ISP wants you to use. This example uses the Google public name servers. You don’t have to use Google; a quick Web search will find a lot of public DNS servers.
Adding DNS hosts to Dnsmasq is almost as easy as falling over. All you do is add them to
/etc/hosts, like this, using your own addresses and hostnames:
127.0.0.1 localhost 192.168.10.2 webserver 192.168.10.3 fileserver 192.168.10.4 dnsmasq 192.168.10.5 timeserver
/etc/hosts, and these hosts are available to your LAN either by hostname or by their fully-qualified domain names. The
expand-hosts option in
/etc/dnsmasq.conf expands the hostnames to the
domain= value, for example webserver.mydomain.net
Set Static Addresses from DHCP
This is my favorite thing. You may assign static IP addresses to your LAN hosts by MAC address, or by hostname. The address must fall in a range you have already configured with
On most Linux distributions it is the default for dhclient to send the hostname. You can confirm this in
dhclient.conf, with the
send host-name option. Do not have any duplicate entries in
Here we are again at the end already. Check out these articles for more Dnsmasq features and howtos:
- Advanced Dnsmasq Tips and Tricks
- DNS Spoofing with Dnsmasq
- Dnsmasq For Easy LAN Name Services
- Dnsmasq home
Learn more about Linux through the free “Introduction to Linux” course from The Linux Foundation and edX.