Install and Configure Dnsmasq on Ubuntu 22.04|20.04|18.04

Posted on 19 views

This guide will help you to install and configure Dnsmasq DNS Server on Ubuntu 22.04|20.04|18.04 LTS. 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 Subsystems

Dnsmasq has three main subsystems, namely:

  • DNS subsystem: Provides caching of A, AAAA, CNAME and PTR, also DNSKEY and DS records.
  • DHCP subsystem: Provide support for DHCPv4, DHCPv6, BOTP and PXE. You can use both static and dynamic DHCP leases, built in read-only TFTP server to support netboot.
  • Router Advertisement subsystem: provides basic autoconfiguration for IPv6 host

Step 1: Install Dnsmasq on Ubuntu 22.04|20.04|18.04

Ubuntu 18.04+ comes with systemd-resolve which you need to disable since it binds to port 53 which will conflict with Dnsmasq port.

Run the following commands to disable the resolved service:

sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved

Also, remove the symlinked resolv.conf file

$ ls -lh /etc/resolv.conf 
lrwxrwxrwx 1 root root 39 Aug  8 15:52 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

$ sudo unlink /etc/resolv.conf

Then create new resolv.conf file.

echo nameserver | sudo tee /etc/resolv.conf

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

sudo apt update
sudo apt 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.
# Never forward plain names (without a dot or domain part)
# Never forward addresses in the non-routed address spaces.
# 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
# Set this (and domain: see below) if you want to have a domain
# automatically added to simple names in a hosts-file.
# 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"

# Set Listen address
listen-address= # Set to Server IP for network responses

If you want to enable DNSSEC validation and caching, uncomment

$ sudo 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.

$ sudo vim /etc/hosts

You need to restart dnsmasq service after adding the records.

sudo systemctl restart dnsmasq

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 servers.

Since this is a test, I’ll modify runtime file /etc/resolv.conf

$ sudo vim /etc/resolv.conf

Test using dig:

$ dig A

; <<>> DiG 9.11.3-1ubuntu1.1-Ubuntu <<>> A
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 43392
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

; EDNS: version: 0, flags:; udp: 4096
;		IN	A


;; Query time: 0 msec
;; WHEN: Tue Aug 21 10:35:41 UTC 2018
;; MSG SIZE  rcvd: 64

Here is another example:

$ dig A +noall +answer

; <<>> DiG 9.11.3-1ubuntu1.1-Ubuntu <<>> A +noall +answer
;; global options: +cmd 0 IN A

You can confirm that we’re getting responses as configured.

Configure Dnsmasq as DHCP Server (Optional)

You can use Dnsmasq to assign IP addresses to clients, either static or dynamic.

Edit the file a /etc/dnsmasq.conf and provide DHCP options. You need to provide:

  • Default gateway IP address
  • DNS server IP address (Probably Dnsmasq or different DNS server)
  • Network Subnet mask
  • DHCP Addresses range
  • NTP server

See below example


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

sudo systemctl restart dnsmasq


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.


Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.