How to Install and Configure Dnsmasq on Ubuntu 18.04 LTS

This guide will help you to install and configure Dnsmasq DNS Server on Ubuntu 18.04 LTS (Bionic Beaver). For those new to Dnsmasq, Dnsmasq is a simple, lightweight, easy to use and manage DNS server with support for Lua scripting, IPv6, DNSSEC, network booting for PXE, BOOTP, and TFTP. It has a small footprint hence suitable for resource-constrained routers and firewalls.

Dnsmasq has been designed to provide DNS, and optionally DHCP/TFTP services for a small to mid-size network environments. When it receives DNS queries, it will either answer them from its local cache or forwards them to a different recursive DNS server, which can be BIND or any other DNS server.

A setup for PowerDNS Authoritative DNS server is available on our blog:

Dnsmasq is available on the apt repository, easy installation can be done by running:

sudo apt-get install dnsmasq

The main configuration file for Dnsmasq is /etc/dnsmasq.conf. Configure Dnsmasq by modifying this file.

sudo vim /etc/dnsmasq.conf

Here is minimal configuration

# Listen on this specific port instead of the standard DNS port
# (53). Setting this to zero completely disables DNS function,
# leaving only DHCP and/or TFTP.
port=53
# Never forward plain names (without a dot or domain part)
domain-needed
# Never forward addresses in the non-routed address spaces.
bogus-priv
# By default, dnsmasq will send queries to any of the upstream
# servers it knows about and tries to favour servers to are known
# to be up. Uncommenting this forces dnsmasq to try each query
# with each server strictly in the order they appear in
# /etc/resolv.conf
strict-order
# Set this (and domain: see below) if you want to have a domain
# automatically added to simple names in a hosts-file.
expand-hosts
# Set the domain for dnsmasq. this is optional, but if it is set, it
# does the following things.
# 1) Allows DHCP hosts to have fully qualified domain names, as long
# as the domain part matches this setting.
# 2) Sets the "domain" DHCP option thereby potentially setting the
# domain of all systems configured by DHCP
# 3) Provides the domain part for "expand-hosts"
#domain=thekelleys.org.uk
domain=mypridomain.com
# Set Liste address
listen-address=127.0.0.1 # Set to Server IP for network responses

If you want to enable DNSSEC validation and caching, uncomment

#dnssec

Make any other changes you see relevant and restart dnsmasq when done:

sudo systemctl restart dnsmasq

Step 2: Adding DNS records to Dnsmasq

Add DNS records in the file./etc/hosts. Dnsmasq will reply to queries from clients using these records.

Step 3: Testing Dnsmasq DNS functionality

To verify that Dnsmasq responds to the records we added, point DNS server of your servers to Dnsmasq server. Edit /etc/network/interfaces for persistent configuration, or the file /etc/netplan/ on Ubuntu 18.04 servers.

Restart dnsmasq and configure clients to obtain an IP address from this server.

sudo systemctl restart dnsmasq

Conclusion

Dnsmasq is an easy to configure DNS cache which can speed up internet browsing and the resolving of domain records on your systems. You can also enjoy its DHCP subsystem which is easy to configure and use for a small network.