Skip to content

dnsmasq for small networks — DNS, DHCP, and TFTP in one daemon

Configuring dnsmasq on Ubuntu as the all-in-one resolver, forwarder, DHCP server, and PXE/TFTP for a small network — with the operational caveats that matter.

A small network — a lab, a branch office, a homelab — needs DNS, DHCP, and sometimes PXE boot. Running three separate daemons is overkill for fewer than a thousand hosts. dnsmasq does all three in a single 300KB binary, with a config file most operators can hold in their head. This article is the install and configuration walk-through for a small network role: DNS forwarder with local hostnames, DHCP with reservations, optional TFTP for PXE. The deliverable is a single host that gives every device on the network DNS and an address.

How to verify

Before installing, check what is already serving DNS or DHCP on this network. Two DHCP servers on the same broadcast domain is bad news.

ss -lunp | grep -E ':(53|67|69)\b'
dpkg -l | grep -E 'dnsmasq|isc-dhcp-server|kea'
systemctl status systemd-resolved
sudo tcpdump -ni any port 67 -c 5         # see if any DHCP traffic is around

If systemd-resolved is holding port 53 and you want dnsmasq on the host’s main address, disable the resolved stub listener.

What’s happening

dnsmasq is a multi-protocol “small network” daemon. By default it does:

  • DNS forwarder. Reads /etc/resolv.conf (or a configured upstream), forwards queries that do not match local config, caches answers. Can also serve local A records from /etc/hosts and from explicit address= lines.
  • DHCP server. Hands out leases to any interface configured with dhcp-range. Supports static reservations by MAC, DNS-DHCP integration (the DHCP-assigned hostname becomes resolvable), and IPv6 RA + DHCPv6.
  • TFTP server. For PXE/iPXE bootstrapping — point an embedded TFTP at a bootloader and netboot a fleet.

The config lives in /etc/dnsmasq.conf and /etc/dnsmasq.d/*.conf (drop-ins). The daemon is small, fast, and read-once on start — there is no runtime API, just SIGHUP to reload.

The right way to think about dnsmasq is “one binary that knows DNS and DHCP are usually deployed together on small networks, and saves you the integration work.” For more than a few hundred clients, or for an HA pair, switch to Kea + a dedicated authoritative DNS — dnsmasq does not cluster.

The procedure

  1. Install and stop systemd-resolved from competing. Ubuntu ships dnsmasq in the main archive:

    sudo apt update
    sudo apt install -y dnsmasq
    sudo systemctl disable --now systemd-resolved
    sudo rm /etc/resolv.conf
    echo "nameserver 1.1.1.1" | sudo tee /etc/resolv.conf

    Putting an explicit upstream in /etc/resolv.conf is what dnsmasq reads as “where to forward queries it does not know.”

  2. Configure DNS forwarder + local names. Drop a file in /etc/dnsmasq.d/:

    # /etc/dnsmasq.d/dns.conf
    interface=eth1
    bind-interfaces
    listen-address=127.0.0.1,10.0.5.1
    no-resolv
    server=1.1.1.1
    server=9.9.9.9
    domain=lab.example.com
    local=/lab.example.com/
    expand-hosts
    cache-size=10000
    neg-ttl=60

    no-resolv plus explicit server= lines decouples dnsmasq from /etc/resolv.conf — useful when the host uses a different resolver for its own DNS lookups than it serves to clients.

  3. Configure DHCP. Another drop-in:

    # /etc/dnsmasq.d/dhcp.conf
    dhcp-range=10.0.5.100,10.0.5.250,12h
    dhcp-option=3,10.0.5.1                # default gateway
    dhcp-option=6,10.0.5.1                # DNS server (this host)
    dhcp-option=15,lab.example.com        # domain name
    dhcp-authoritative
    log-dhcp
    
    # static reservation by MAC
    dhcp-host=aa:bb:cc:dd:ee:01,printer,10.0.5.10,infinite
    dhcp-host=aa:bb:cc:dd:ee:02,nas,10.0.5.20,infinite

    dhcp-authoritative tells dnsmasq it is the only DHCP server on this segment, which lets it NACK invalid renews quickly. Only set it if true.

  4. Add TFTP (optional). For PXE/iPXE:

    # /etc/dnsmasq.d/tftp.conf
    enable-tftp
    tftp-root=/srv/tftp
    dhcp-boot=ipxe.efi
    dhcp-match=set:efi64,option:client-arch,7
    dhcp-boot=tag:efi64,ipxe.efi
    dhcp-boot=tag:!efi64,undionly.kpxe

    Drop the boot files in /srv/tftp/. dnsmasq returns the right filename based on the client architecture.

  5. Reload and verify. dnsmasq parses on start — there is no separate checker:

    sudo systemctl restart dnsmasq
    sudo journalctl -u dnsmasq -n 30 --no-pager
    dig @10.0.5.1 printer.lab.example.com +short
    sudo cat /var/lib/misc/dnsmasq.leases
  6. Confirm DHCP from a client. From a Linux client on the same segment:

    sudo dhclient -v eth0
    ip addr show eth0
    cat /etc/resolv.conf            # should now point at 10.0.5.1

Common pitfalls

  • bind-interfaces vs default behaviour. Without it, dnsmasq binds to wildcard 0.0.0.0:53 even though it accepts only on listed interfaces. This shows up as port-53 conflicts with another daemon. Always use bind-interfaces.
  • dhcp-authoritative on a network with another DHCP server. dnsmasq will NACK leases the other server gave out — clients flap. Confirm you are the only DHCP server before setting it.
  • No HA story. Two dnsmasq instances on the same network with overlapping dhcp-range will hand out conflicting leases. For real HA, switch to Kea with HA hooks or ISC DHCP failover.
  • Cache TTL on stale records. dnsmasq caches according to upstream TTL. After an upstream zone change, run sudo killall -HUP dnsmasq to flush.
  • DHCPv6 is separate. dhcp-range= with an IPv6 prefix activates DHCPv6, but you also need enable-ra for router advertisements. Forgetting RA leaves clients without a default route.

In the engagements we run, dnsmasq is the right answer for branch offices, lab environments, and the management network of a larger platform — anywhere the DNS/DHCP integration is small enough to fit in one head and HA is not a hard requirement. We codify the config, monitor the lease file growth, and roll out updates via the same configuration management as the rest of the fleet. See our managed operations service for the broader operating model.