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.
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:
- Start and end IP address
- Lease time
- Router address
- NTP server
- DNS server
- Netmask
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. Setting up Dnsmasq on Ubuntu 22.04 can significantly improve local network management.
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
Install Visual Studio Code on Ubuntu 22.04
Install Cloudron on Ubuntu 20.04
Alternative Solutions for Local DNS and DHCP on Ubuntu 22.04
While Dnsmasq is a fantastic and lightweight solution, other approaches exist for achieving similar results. Here are two alternative methods for setting up local DNS and DHCP services on Ubuntu 22.04.
1. Using systemd-networkd and systemd-resolved
Instead of disabling systemd-resolved
(as suggested in the original guide), you can configure it to act as a caching DNS server and integrate it with systemd-networkd
for DHCP server functionality. This approach leverages systemd’s built-in capabilities, providing a more integrated and potentially simpler solution, especially if you’re already using systemd-networkd for network configuration.
Explanation:
- systemd-resolved: Acts as a local DNS resolver, caching DNS queries and forwarding them to upstream DNS servers. You can configure it to read from
/etc/hosts
for local hostname resolution. - systemd-networkd: A network configuration daemon that can manage network interfaces and provide DHCP server functionality.
Configuration Steps:
-
Configure systemd-resolved:
Edit
/etc/systemd/resolved.conf
and ensure the following settings:[Resolve] DNS=8.8.8.8 8.8.4.4 Domains=~. Cache=yes
DNS
: Specifies the upstream DNS servers. You can use Google’s public DNS servers (8.8.8.8 and 8.8.4.4) or your preferred DNS providers.Domains=~.
: Specifies that systemd-resolved should handle all domains.Cache=yes
: Enables DNS caching.
-
Configure systemd-networkd for DHCP Server:
Create a network configuration file for your interface (e.g.,
/etc/systemd/network/20-lan.network
for an interface namedlan
). Replacelan
with your actual interface name.[Match] Name=lan [Network] Address=192.168.1.1/24 DHCPServer=yes [DHCPServer] PoolOffset=100 PoolSize=50 DefaultLeaseTimeSec=3600 MaxLeaseTimeSec=86400
Name
: Specifies the network interface this configuration applies to.Address
: Sets the static IP address and subnet for the interface acting as the DHCP server (e.g., 192.168.1.1/24).DHCPServer=yes
: Enables the DHCP server on this interface.PoolOffset
: The starting IP address for the DHCP range (192.168.1.100 in this example).PoolSize
: The number of IP addresses available in the DHCP range.DefaultLeaseTimeSec
: The default lease time in seconds.MaxLeaseTimeSec
: The maximum lease time in seconds.
-
Enable and Start systemd-networkd and systemd-resolved:
sudo systemctl enable systemd-networkd sudo systemctl start systemd-networkd sudo systemctl enable systemd-resolved sudo systemctl start systemd-resolved
-
Add Local Host Entries to /etc/hosts:
As with Dnsmasq, add your local hostname mappings to
/etc/hosts
.
Advantages:
- Integrated with systemd: Leverages existing systemd infrastructure.
- Potentially simpler configuration: Avoids the need to manage a separate Dnsmasq configuration file.
Disadvantages:
- Steeper learning curve: Requires understanding of systemd-networkd and systemd-resolved concepts.
- Less flexible than Dnsmasq: Might not offer all the advanced features of Dnsmasq.
2. Using a Full-Fledged DNS Server: Bind9
For larger or more complex network environments, a full-fledged DNS server like Bind9 might be a better option. While Bind9 is significantly more complex to configure than Dnsmasq, it provides much greater flexibility and scalability.
Explanation:
- Bind9 (Berkeley Internet Name Domain): A widely used, powerful, and highly configurable DNS server.
Configuration Steps (Simplified Overview):
-
Install Bind9:
sudo apt update sudo apt install bind9 bind9utils bind9-doc
-
Configure Forwarders:
Edit
/etc/bind/named.conf.options
and configure forwarders to upstream DNS servers:options { directory "/var/cache/bind"; forwarders { 8.8.8.8; 8.8.4.4; }; dnssec-validation auto; listen-on { any; }; listen-on-v6 { any; }; };
-
Create a Local Zone File:
Create a zone file for your local domain (e.g.,
/etc/bind/db.example.com
):; ; BIND data file for example.com ; $TTL 86400 @ IN SOA ns1.example.com. admin.example.com. ( 2023102701 ; Serial 604800 ; Refresh 86400 ; Retry 2419200 ; Expire 604800 ) ; Negative Cache TTL ; @ IN NS ns1.example.com. @ IN A 192.168.1.1 ns1 IN A 192.168.1.1 host1 IN A 192.168.1.10 host2 IN A 192.168.1.11
-
Configure the Named Configuration File:
Edit
/etc/bind/named.conf.local
to include your local zone:zone "example.com" { type master; file "/etc/bind/db.example.com"; };
-
Restart Bind9:
sudo systemctl restart bind9
Advantages:
- Highly configurable: Provides extensive control over DNS settings.
- Scalable: Suitable for larger and more complex networks.
- Standard DNS server: Widely supported and understood.
Disadvantages:
- Complex configuration: Requires significant knowledge of DNS and Bind9 configuration.
- Overkill for simple networks: More complex than necessary for basic local DNS needs.
These alternative solutions offer different approaches to achieving local DNS and DHCP functionality on Ubuntu 22.04. The best choice depends on your specific needs and technical expertise. For simple networks, Dnsmasq remains an excellent option. For more integrated systemd environments, systemd-networkd
and systemd-resolved
may be preferable. For complex environments needing advanced DNS features, Bind9 is a powerful, though more complex, solution. Remember to thoroughly test any configuration changes before deploying them to a production environment. The process to Install and Configure Dnsmasq on Ubuntu 22.04 is significantly simpler than the two alternatives mentioned above.