Install and Configure Dnsmasq on Ubuntu 22.04

In this guide, you will learn to Install and Configure Dnsmasq on Ubuntu 22.04.

Dnsmasq is a lightweight DNS, TFTP, and DHCP server. It is intended to provide coupled DNS and DHCP service to a LAN.

Dnsmasq accepts DNS queries and either answers them from a small, local, cache or forwards them to a real, recursive, DNS server. It loads the contents of /etc/hosts so that local hostnames which do not appear in the global DNS can be resolved and also answers DNS queries for DHCP-configured hosts.

The Dnsmasq DHCP server supports static address assignments, multiple networks, DHCP-relay, and RFC3011 subnet specifiers. It automatically sends a sensible default set of DHCP options and can be configured to send any desired options, including vendor-encapsulated options. It includes a secure, read-only, TFTP server to allow net/PXE boot of DHCP hosts and supports BOOTP.

Dnsmasq supports IPv6 for DNS, but not DHCP.

Steps To Install and Configure Dnsmasq on Ubuntu 22.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Ubuntu 22.04.

Disable resolved service on Ubuntu 22.04

At this point, you need to disable systemd-resolve which binds to port 53 which will conflict with the Dnsmasq port. To do these, run the following commands:

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

Then, use the following command to remove the symlinked resolv.conf file:

sudo unlink /etc/resolv.conf

Create a new resolv.conf file

Now you need to create a new resolv.conf file by using the command below:

echo nameserver | sudo tee /etc/resolv.conf

Installing Dnsmasq on Ubuntu 22.04

At this point, update your local package index and use the following command to install the Dnsmasq:

# sudo apt update
# sudo apt install dnsmasq -y

Configuring Dnsmasq on Ubuntu 22.04

The main configuration file for Dnsmasq is /etc/dnsmasq.conf. You can configure Dnsmasq by modifying this file.

sudo vi /etc/dnsmasq.conf

You can modify your file as shown below:

# 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

When you are done, save and close the file.

To apply the changes, restart Dnsmasq:

sudo systemctl restart dnsmasq

Add DNS records to Dnsmasq

At this point, you can add DNS records to the file /etc/hosts. Dnsmasq will reply to queries from clients using these records.

Open the file by using your favorite text editor, here we use the vi editor:

sudo vi /etc/hosts

For example:

When you are done, save and close the file.

Restart Dnsmasq on Ubuntu 22.04:

sudo systemctl restart dnsmasq

To verify that Dnsmasq responds to the records you have added, point the DNS server of your servers to the 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 the runtime file /etc/resolv.conf

sudo vi /etc/resolv.conf

Save and close the file.

Now you can use the dig command to test your Dnsmasq functionality:

$ 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

.	0	IN	A

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

Configure Dnsmasq as DHCP Server

You can use Dnsmasq on Ubuntu 22.04 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 a different DNS server)
  • Network Subnet mask
  • DHCP Addresses range
  • NTP server

For example:


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

sudo systemctl restart dnsmasq


At this point, you have learned to Install and Configure Dnsmasq on Ubuntu 22.04.

Hope you enjoy it. You may be interested in these articles:

Install Visual Studio Code on Ubuntu 22.04

Install Cloudron on Ubuntu 20.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!