Share your love
Install and Configure Dnsmasq on Ubuntu 22.04: Best Setup

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 that 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. Now follow the guide steps below on the Orcacore website to complete the Dnsmasq config and setup on Ubuntu 22.
Table of Contents
Steps To Install and Configure Dnsmasq on Ubuntu 22.04
To complete the Dnsmasq config on Ubuntu 22, 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.

1. 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
2. Create a new resolv.conf file
Now you need to create a new resolv.conf
file by using the command below:
echo nameserver 8.8.8.8 | sudo tee /etc/resolv.conf
3. 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
4. Dnsmasq Config on Ubuntu 22.04
The main configuration file for Dnsmasq is /etc/dnsmasq.conf
. You can configure Dnsmasq by modifying the Dnsmasq config.
sudo vi /etc/dnsmasq.conf
You can modify your Dnsmasq config 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.
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=example.com
# Set Listen address
listen-address=127.0.0.1 # 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
5. 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:
10.1.3.4 ex.domain.com
10.1.4.4 er.domain.com
192.168.10.2 ch.domain.com
192.168.4.3 hello.world
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
nameserver 127.0.0.1
nameserver 8.8.8.8
Save and close the file.
Now you can use the dig command to test your Dnsmasq functionality:
$ dig A ex.domain.com
; <<>> 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
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;. IN A
;; ANSWER SECTION:
. 0 IN A 10.1.4.4
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Tue Aug 21 10:35:41 UTC 2018
;; MSG SIZE rcvd: 64
6. Configure Dnsmasq as a 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:
dhcp-range=192.168.3.25,192.168.3.50,24h
dhcp-option=option:router,192.168.3.1
dhcp-option=option:ntp-server,192.168.3.5
dhcp-option=option:dns-server,192.168.3.5
dhcp-option=option:netmask,255.255.255.0
Restart Dnsmasq and configure clients to obtain an IP address from this server.
sudo systemctl restart dnsmasq
Conclusion
Dnsmasq is a lightweight and versatile tool that helps manage networks. It simplifies and speeds up networking for small networks. At this point, you have learned to Install and Configure Dnsmasq on Ubuntu 22.04.
Hope you enjoy it. You may also interested in these articles:
Open-source DNS server with GUI
PowerDNS integration with Virtualizor
PowerDNS-admin ubuntu 22.04 Setup