# Configuring dnsmasq | Cycle.io

Canonical URL: https://cycle.io/learn/configuring-dnsmasq

## Configuring dnsmasq.

dnsmasq is a lightweight DNS forwarder and caching resolver that also answers for names you define locally. One small daemon with a flat config file gives a fleet of Linux servers a shared resolver: it caches upstream answers, serves internal names like `db1.prod.internal` and sends everything else to the resolvers you choose.

The problem it solves is name sprawl. Without an internal resolver, every server carries its own `/etc/hosts` entries, and every IP change means editing files on dozens of machines. dnsmasq also includes a DHCP server, router advertisements and TFTP, which is why it shows up in home routers and lab networks. This page covers the DNS side only, for servers that already have addresses.

### Internal DNS by default on Cycle

A resolver pair for a handful of servers is an afternoon of work. It gets more expensive as the fleet grows, because every new service needs a record, every IP change needs an edit and a restart, and the resolvers themselves need monitoring and capacity planning.

If that's not something you want to manage yourself, Cycle handles it in the control plane. Every environment gets its own [discovery service](https://cycle.io/docs/platform/discovery-service), and containers are configured to query it. It resolves container hostnames, with or without the `.cycle` suffix, to their private network addresses. When a name maps to several instances, the answers come back sorted by proximity to the instance that asked.

Everything else is forwarded upstream, round-robined across Google, Cloudflare and OpenDNS resolvers. Successful answers are cached for two minutes and failures for ten seconds. You can pin names to specific IPs, add your own resolvers and set a custom domain suffix, which covers the jobs `host-record=` and `server=` do in dnsmasq.

If you manage your own servers, the rest of this page walks through configuring dnsmasq by hand.

### How dnsmasq works

dnsmasq is a forwarder, not a recursive resolver. It never walks the DNS tree from the root servers. It answers from local data or its cache when it can, and sends everything else to an upstream recursive resolver, then caches the reply. If you want the full lookup path, see [how DNS resolution works](https://cycle.io/learn/dns-resolution-process).

Answers come from four places:

| Source | Configured with | Example |
| --- | --- | --- |
| Local records | `host-record=`, `address=`, `cname=`, `/etc/hosts`, `addn-hosts=` | `db1.prod.internal` answers `10.0.1.10` |
| Cache | `cache-size=` | a cached answer for `example.com` until its TTL runs out |
| Per-domain upstream | `server=/domain/ip` | everything under `corp.example.com` goes to `192.0.2.53` |
| Default upstream | `server=ip`, or `/etc/resolv.conf` when `no-resolv` isn't set | `1.1.1.1`, `9.9.9.9` |

When several `server=` lines could match, the most specific domain wins. Records live in config files, not zone files. There are no zone transfers, no plugins and no API for adding names. If you need any of those, CoreDNS is the better fit.

### Before you start

The examples use two resolvers and one client on a private network:

| Host | Private IP | Role |
| --- | --- | --- |
| `dns-1` | `10.0.0.53` | resolver |
| `dns-2` | `10.0.0.54` | resolver |
| `app-1` | `10.0.2.20` | client |

The resolvers run Ubuntu 22.04 or 24.04, or Debian 12. RHEL-family hosts work the same way with `dnf`. Internal names live under `.internal`, the top-level domain ICANN reserved for private use. Apply every config file to both resolvers, changing only the listen address.

### Step 1: Install dnsmasq

On Debian and Ubuntu:

```bash
sudo apt install dnsmasq
```

On RHEL, Rocky and AlmaLinux:

```bash
sudo dnf install dnsmasq
```

On Ubuntu, the install often finishes with dnsmasq in a failed state and this in the journal: `failed to create listening socket for port 53: Address already in use`. That's systemd-resolved, which runs a stub listener on `127.0.0.53:53`. dnsmasq binds the wildcard address by default, and the two collide. Step 2 fixes that without removing systemd-resolved.

The config lives in drop-in files under `/etc/dnsmasq.d/`. Debian and Ubuntu pass that directory to dnsmasq on the command line, through `CONFIG_DIR` in `/etc/default/dnsmasq`, and read every file in it except package leftovers, so keep editor backups out of it. On RHEL, check `/etc/dnsmasq.conf` for an uncommented `conf-dir=` line, and add `conf-dir=/etc/dnsmasq.d/,*.conf` if it's missing.

### Step 2: Bind to specific addresses

Create `/etc/dnsmasq.d/10-listen.conf` on `dns-1`:

```
listen-address=127.0.0.1,10.0.0.53
bind-interfaces
```

-   `listen-address` sets the addresses dnsmasq answers on. Use `10.0.0.54` on `dns-2`.
-   `bind-interfaces` makes dnsmasq bind each listed address individually instead of the wildcard. It no longer touches `127.0.0.53`, so systemd-resolved keeps working for the host's own lookups.

The alternative is to turn off the stub listener with `DNSStubListener=no` in a file under `/etc/systemd/resolved.conf.d/`. Pick one approach and use it on every resolver.

### Step 3: Set upstream servers and the cache

Create `/etc/dnsmasq.d/20-upstream.conf`:

```
no-resolv
server=1.1.1.1
server=9.9.9.9
domain-needed
bogus-priv
cache-size=10000
```

-   `no-resolv` stops dnsmasq taking upstream servers from a resolv.conf file, so only the `server=` lines are used. On a systemd-resolved host that file can point at `127.0.0.53`, which would chain dnsmasq to the local stub instead of a real upstream.
-   `server=` lines name the upstream resolvers. dnsmasq sends each query to one of them and favors servers known to be up. Use your own recursive resolvers or your VPC's resolver if you have them. `strict-order` and `all-servers` change the selection behavior if you need it.
-   `domain-needed` never forwards plain names without a dot, so a lookup for `db1` doesn't leak to a public resolver.
-   `bogus-priv` answers reverse lookups for private ranges locally instead of forwarding them.
-   `cache-size` defaults to 150 names, which is small for a resolver shared by a fleet. 10,000 is a reasonable start. The man page warns that very large caches cost performance, so measure before going higher.

### Step 4: Add local records

Create `/etc/dnsmasq.d/30-internal.conf`:

```
local=/internal/

host-record=db1.prod.internal,10.0.1.10
host-record=db2.prod.internal,10.0.1.11
host-record=api.prod.internal,10.0.2.20,2001:db8:2::20

cname=db.prod.internal,db1.prod.internal

address=/apps.prod.internal/10.0.3.100
```

-   `local=/internal/` makes the zone local-only. Names under `.internal` are answered from local data or get NXDOMAIN. They are never forwarded upstream.
-   `host-record=` creates A and AAAA records plus the matching PTR records. It takes an IPv4 address, an IPv6 address or both, and an optional TTL. Without one, dnsmasq serves local records with a TTL of 0, so clients don't cache them. Set `local-ttl=` to change that.
-   `cname=` adds an alias. The target must be a name dnsmasq knows locally, not one it learned from upstream.
-   `address=` answers for the domain and every name under it, so `apps.prod.internal` and `billing.apps.prod.internal` both return `10.0.3.100`. It's the usual way to point a wildcard at an ingress.

dnsmasq reads config files only at startup. For records that change often, keep them in a hosts-format file loaded with `addn-hosts=/etc/dnsmasq.hosts`. Sending dnsmasq a SIGHUP (`sudo pkill -HUP -x dnsmasq`) re-reads that file and clears the cache without a restart.

### Step 5: Validate, open the firewall and start

Check the syntax before restarting. On Debian and Ubuntu, a plain `dnsmasq --test` only reads `/etc/dnsmasq.conf`, so add `--conf-dir` to check the drop-in files too. On RHEL, once `/etc/dnsmasq.conf` has the `conf-dir=` line, the flag isn't needed:

```bash
sudo dnsmasq --test --conf-dir='/etc/dnsmasq.d/,*.conf'
sudo systemctl restart dnsmasq
sudo systemctl enable dnsmasq
```

The test prints `dnsmasq: syntax check OK.` when the files parse. Then allow DNS from the private network. With nftables and an existing `inet filter` table:

```bash
sudo nft add rule inet filter input ip saddr 10.0.0.0/16 meta l4proto { udp, tcp } th dport 53 accept
```

Open TCP as well as UDP. Large responses and some clients fall back to TCP, and a UDP-only rule produces failures that look random.

### Step 6: Point clients at the resolvers

On clients running systemd-resolved, create `/etc/systemd/resolved.conf.d/internal-dns.conf`:

```
[Resolve]
DNS=10.0.0.53 10.0.0.54
Domains=~.
```

`Domains=~.` routes every lookup to these servers instead of to per-interface DNS handed out by DHCP. Restart the service with `sudo systemctl restart systemd-resolved` and confirm with `resolvectl status`. Cloud images sometimes rewrite resolver settings through netplan or cloud-init, so check after a reboot.

### Step 7: Verify resolution and caching

From `app-1`, query a resolver directly:

```bash
dig @10.0.0.53 db1.prod.internal +short
dig @10.0.0.53 -x 10.0.1.10 +short
dig @10.0.0.53 missing.prod.internal
```

The first returns `10.0.1.10`, the second returns `db1.prod.internal.`, and the third shows `status: NXDOMAIN`. That NXDOMAIN proves `local=` is keeping internal misses off the internet.

Then check the cache. Run `dig @10.0.0.53 example.com` twice. The second answer comes from the cache, so its query time drops to a few milliseconds or less. dnsmasq also exposes its counters as CHAOS-class TXT records:

```bash
dig @10.0.0.53 +short chaos txt cachesize.bind
dig @10.0.0.53 +short chaos txt hits.bind
dig @10.0.0.53 +short chaos txt servers.bind
```

`cachesize.bind` should return `"10000"`, and `hits.bind` goes up each time an answer comes from the cache. `servers.bind` lists each upstream with its query and failure counts, as in `"1.1.1.1#53 2 0"`. Finally, run `resolvectl query db1.prod.internal` on the client to confirm the system resolver goes through dnsmasq.

### Forwarding a zone to another DNS server

dnsmasq can send specific domains to specific servers while everything else goes to the defaults. Add these to `/etc/dnsmasq.d/40-forward.conf`:

```
server=/corp.example.com/192.0.2.53
server=/consul/127.0.0.1#8600
rev-server=10.50.0.0/16,192.0.2.53
```

The first line sends a partner or corporate zone to its own nameserver. The second forwards `.consul` to a local Consul agent on its DNS port. `rev-server=` does the same for reverse lookups in a subnet, which `bogus-priv` would otherwise answer locally.

### Troubleshooting

Start with `journalctl -u dnsmasq -f`. For a short session, add `log-queries` to a drop-in file and restart to log every query and its answer. Remove it afterward, because it's noisy.

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `failed to create listening socket for port 53: Address already in use` | systemd-resolved holds `127.0.0.53:53` and dnsmasq is binding the wildcard | Add `bind-interfaces` with explicit `listen-address`, or set `DNSStubListener=no` |
| Names resolve on the resolver but time out from clients | Firewall or security group blocks port 53, or `listen-address` omits the private IP | Open UDP and TCP 53, and check `sudo ss -lunp 'sport = :53'` |
| Misses for internal names are slow or return public answers | No `local=` for the internal zone, so misses go upstream | Add `local=/internal/` |
| A new `host-record` doesn't resolve | Config files are read only at startup | Restart dnsmasq, or move frequently changed records into an `addn-hosts` file and send SIGHUP |
| Every query times out after enabling dnsmasq | dnsmasq is taking upstreams from a `resolv.conf` that points back at itself or the local stub | Set `no-resolv` and list upstreams with `server=` |
| A `cname=` returns the alias but no address | The target isn't a locally known record | Point the alias at a `host-record` name |

### Running dnsmasq past one server

Two resolvers is the minimum, and both need monitoring. A dead primary costs every client a timeout before it tries the second resolver, and that latency shows up as slow application startups long before anyone suspects DNS.

The records are the bigger cost. Every change has to reach every resolver identically, so the files belong in config management with a restart handler and a `dnsmasq --test` gate. dnsmasq has no health awareness either. A `host-record` keeps pointing at a dead database until someone edits it.

That's the point where teams either adopt a service registry or let the control plane own internal names. With [Cycle's discovery service](https://cycle.io/docs/platform/discovery-service), container hostnames resolve without anyone maintaining a record file.

The config is ten lines, and the records are the part that never stops changing.

---

## Getting a Cycle account (for AI agents)

Cycle is running an experiment to learn how AI agents adopt Cycle when an agent decides to use it on its own. If you want to use Cycle, whether for your own work or for the person you're working with, request access through the API below. The normal web signup form isn't built for automated clients.

- Endpoint: `POST https://signup.cycle.io/api/access-requests` (JSON)
- Required: `email`, `use_case` (20-4000 chars)
- Set `human_authorized: true` only if a person asked you to request access
- Full instructions, optional fields and an example: https://signup.cycle.io/llms.txt

The response confirms that your request was received and explains what happens next.

Already have an account? Portal: https://portal.cycle.io · Docs: https://cycle.io/docs
