Installing Dnsmasq on Linux: Lightweight DNS Cache and DHCP for Offices and Homelabs

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

In an office with 40–50 workstations, this scenario is all too common: typing an internal domain or calling APIs between lab servers feels sluggish. The reason is that every single machine sends requests out to public DNS servers just to find the route. Dnsmasq solves this issue once and for all. It acts like a phonebook sitting right at your network gateway: whenever a client asks for a familiar domain, it responds instantly without waiting for an answer from the outside world.

Quick Start: Set Up a DNS Cache in 5 Minutes

The following steps will help you quickly resolve DNS query bottlenecks on your LAN.

1. Install Dnsmasq

On Ubuntu or Debian:

sudo apt update
sudo apt install dnsmasq -y

On RHEL, AlmaLinux, or Rocky Linux:

sudo dnf install dnsmasq -y

2. Minimal DNS Cache Configuration

Before editing the configuration file, create a backup copy:

sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
sudo nano /etc/dnsmasq.conf

Paste the following 5 lines into the configuration file (replace 192.168.1.10 with your server’s static IP):

# Listen on loopback and internal LAN interface
interface=lo,eth0
listen-address=127.0.0.1,192.168.1.10

# Reliable upstream DNS servers to the Internet
server=1.1.1.1
server=8.8.8.8

# Number of records cached in RAM
cache-size=1000

3. Resolve Port 53 Conflicts and Test Query Speed

On Ubuntu 20.04/22.04/24.04, the systemd-resolved service occupies port 53 by default. You need to disable its stub listener to free up the port for Dnsmasq:

# Change DNSStubListener to no in systemd-resolved config
sudo sed -i 's/#DNSStubListener=yes/DNSStubListener=no/' /etc/systemd/resolved.conf
sudo systemctl restart systemd-resolved

# Start and enable Dnsmasq
sudo systemctl restart dnsmasq
sudo systemctl enable dnsmasq

Test the resolution latency using the dig tool:

dig itfromzero.com @127.0.0.1

On the first run, the Query time usually takes around 35–60 ms. Run the exact same command a second time, and the result will show Query time: 0 msec. The record is now served directly from the server’s RAM.

How Dnsmasq Works Under the Hood

Dnsmasq consumes only about 5–15 MB of RAM in production. This footprint is significantly lighter than a full-fledged BIND9 deployment, making it an ideal fit for OpenWrt routers, Raspberry Pis, and homelab environments.

Record Lookup Mechanism

Dnsmasq turns the host server’s /etc/hosts file into its primary domain database. The lookup process works as follows:

  1. A client queries the IP address for gitlab.local.
  2. Dnsmasq checks the server’s /etc/hosts file first. If it finds an entry like 192.168.1.50 gitlab.local, it responds immediately.
  3. If not found, it checks its in-memory cache (RAM cache).
  4. If the cache is also empty, Dnsmasq forwards the query upstream to Cloudflare (1.1.1.1) or Google (8.8.8.8). Once it receives the answer, it returns the result to the client and caches it locally.

Seamless DHCP Server Integration

Dnsmasq also acts as an internal IP allocator. Its greatest convenience lies in automatic registration: whenever a workstation requests an IP with a hostname, Dnsmasq automatically maps that hostname into the local DNS. For example, if a machine requests an IP using the hostname nas-backup, other devices on the LAN can simply run ping nas-backup to reach it—no manual configuration entries required.

Enable DHCP Server and Static Leases

Open /etc/dnsmasq.conf again to turn Dnsmasq into a complete LAN network server.

1. Configure Dynamic IP Ranges

# Allocate IP range from .100 to .200, subnet mask /24, lease time 24 hours
dhcp-range=192.168.1.100,192.168.1.200,255.255.255.0,24h

# Push default gateway to clients (typically your main router)
dhcp-option=option:router,192.168.1.1

# Specify this Dnsmasq server as the DNS server for clients
dhcp-option=option:dns-server,192.168.1.10

# Internal domain name for the LAN
domain=lab.local

2. Assign Static IPs by MAC Address

Infrastructure devices like network printers or database servers need strictly persistent IP addresses:

# Syntax: dhcp-host=[MAC],[HOSTNAME],[IP]
dhcp-host=00:11:22:33:44:55,printer.lab.local,192.168.1.20
dhcp-host=b8:27:eb:1a:2b:3c,file-server.lab.local,192.168.1.30

After editing, always verify syntax before restarting the service:

sudo dnsmasq --test
sudo systemctl restart dnsmasq

The output line dnsmasq: syntax check OK confirms that your configuration is valid.

Production Tips & Best Practices

I once used Dnsmasq to handle traffic for an entire 80-person office on an Ubuntu VM with just 1 vCPU and 1 GB RAM. It ran smoothly for months without a hitch, but keep these 4 key points in mind:

  • Tune cache-size sensibly: By default, Dnsmasq only caches 150 records. Increasing this to between 1000 and 2500 records is ideal. Avoid setting it to 50,000 or 100,000. Dnsmasq uses a sequential array lookup in memory, so an excessively large cache will actually degrade lookup performance.
  • Build a DNS Sinkhole for ad blocking: You can block trackers and unwanted ad domains by pointing them directly to an invalid IP using the syntax address=/adservice.google.com/0.0.0.0. All devices on your LAN will stay ad-free without needing browser extensions installed on every machine.
  • Inspect active leases: To see which devices are currently connected, their assigned IPs, and MAC addresses, check the lease database file directly:
    cat /var/lib/misc/dnsmasq.leases
  • Never run two DHCP servers on the same subnet: If you enable DHCP on Dnsmasq, remember to disable the DHCP server on your ISP modem/router. Having two competing DHCP servers on the same broadcast domain will lead to IP conflicts and network outages.
Share: