Sub topic 5
Installing the software and freeing port 53
This chapter assumes you can use a Linux shell, manage services with systemd, and read basic ss output. It also assumes you know that DNS uses port 53 over both UDP and TCP. By the end you will have BIND or Unbound installed. You will also know what is already bound to port 53 and how to clear it without breaking name resolution on the host.
Why port 53 is often busy
A DNS server must bind to port 53 on UDP and TCP. Only one process can listen on a given address and port pair. If another process already holds it, the new daemon fails to start. The usual log line is bind: Address already in use or could not listen on UDP port 53.
Many modern distributions run a local stub resolver. On Ubuntu and Debian derivatives, systemd-resolved listens on 127.0.0.53:53. Applications on the host send queries to that address. The stub forwards them to the upstream servers it learned from DHCP or from netplan or NetworkManager.
This matters for how you pick listen addresses. systemd-resolved holds only 127.0.0.53. A server that listens on 192.0.2.10:53 does not conflict with it. A server that listens on 0.0.0.0:53, which is the default for some daemons, does conflict. The wildcard bind overlaps 127.0.0.53, and the second bind fails. Listening on 127.0.0.1 also works, because it is a different address.
Step 1: See what is listening
Run ss before you install anything. The options mean: -l listening sockets, -n numeric ports, -t TCP, -u UDP, -p show the owning process.
sudo ss -lntup | grep ':53 '
# Debian/Ubuntu
sudo apt install bind9 bind9-utils dnsutils
sudo apt install unbound
# RHEL family
sudo dnf install bind bind-utils
sudo dnf install unboundA typical result on a fresh Ubuntu server looks like this:
udp UNCONN 0 0 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=612,fd=13))
tcp LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=612,fd=14))If the command prints nothing, port 53 is free. Other common owners are dnsmasq (often started by libvirt or NetworkManager), a previous named or unbound, and container runtimes with their own DNS proxies. The users: field tells you which one you have.
Step 2: Install the packages
You do not need both daemons. Pick one according to the job, as the previous chapter described. Install both only if you intend to run them on different addresses or machines.
| Package | Debian/Ubuntu | RHEL family | Contents |
|---|---|---|---|
| BIND server | `bind9` | `bind` | The `named` daemon and `named-checkconf`, `named-checkzone`, `rndc` on some releases |
| BIND tools | `bind9-utils`, `dnsutils` | `bind-utils` | `dig`, `host`, `nslookup`, and on Debian the checkers and `rndc` |
| Unbound | `unbound` | `unbound` | The daemon, `unbound-checkconf`, `unbound-control` |
Package contents shift between releases, so confirm that the tools exist after installing:
command -v named named-checkconf named-checkzone rndc dig
command -v unbound unbound-checkconf unbound-controlThe package manager usually enables and starts the service on Debian and Ubuntu. On the RHEL family it installs the unit but leaves it stopped. In both cases, treat the default configuration as unreviewed. The next chapters replace it with your own.
Service names
Unit names differ by distribution, and this trips people up when they copy commands.
| Daemon | Debian/Ubuntu unit | RHEL family unit |
|---|---|---|
| BIND | `named.service` (alias `bind9.service`) | `named.service` |
| Unbound | `unbound.service` | `unbound.service` |
Configuration paths differ as well. Debian puts BIND files in /etc/bind/ and zone data in /var/cache/bind/. The RHEL family uses /etc/named.conf and /var/named/. Unbound uses /etc/unbound/unbound.conf on both, and Debian includes /etc/unbound/unbound.conf.d/*.conf from it.
Step 3: Free port 53
You have three approaches. Choose the least invasive one that works.
- Bind to specific addresses. Set
listen-onin BIND orinterface:in Unbound to your server's own addresses, not the wildcard. Thesystemd-resolvedstub keeps127.0.0.53, and your daemon takes the others. This changes nothing about the host's own resolution. - Turn off only the stub listener. Keep
systemd-resolvedbut stop it from binding port 53. - Disable the conflicting service. This is the right choice for
dnsmasqor a leftover daemon you do not need.
Turning off the stub listener
Create a drop-in file. This survives package upgrades, and editing /etc/systemd/resolved.conf directly does not always.
sudo mkdir -p /etc/systemd/resolved.conf.d
sudo tee /etc/systemd/resolved.conf.d/10-no-stub.conf >/dev/null <<'EOF'
[Resolve]
DNSStubListener=no
EOF
sudo systemctl restart systemd-resolved
sudo ss -lntup | grep ':53 ' || echo "port 53 is free"Setting DNSStubListener=no has a side effect. /etc/resolv.conf on Ubuntu is a symlink to /run/systemd/resolve/stub-resolv.conf, which names 127.0.0.53. With the stub gone, nothing answers there, and the host cannot resolve names. Repoint the symlink at the file that lists the real upstream servers:
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
readlink -f /etc/resolv.confAlternatively, point resolv.conf at your own new resolver once it works, by setting nameserver 127.0.0.1. Do that only after you have tested the daemon, so a mistake does not leave you unable to resolve the package mirrors.
Disabling another service
For dnsmasq started as a standalone service:
sudo systemctl disable --now dnsmasqSome dnsmasq instances are started by libvirt or NetworkManager, not by systemd units of their own. Disabling the unit does nothing in those cases. NetworkManager's setting dns=dnsmasq in /etc/NetworkManager/NetworkManager.conf is one example, and libvirt's default network is another. Check the users: field in ss again after each change.
Step 4: Start the service and confirm the bind
Validate the configuration before you start or restart. A syntax error in a restart can leave you with no DNS at all.
sudo named-checkconf
sudo unbound-checkconf
sudo systemctl enable --now named
sudo systemctl enable --now unbound
sudo systemctl status named unbound --no-pager
sudo ss -lntup | grep ':53 'Running both daemons with default settings will fail for whichever starts second, because both try to listen on the same addresses. Use distinct addresses for each, or run only one.
If a unit fails, read the journal:
sudo journalctl -u named -u unbound -n 50 --no-pagerFirewall and SELinux notes
Opening the port is separate from binding it. DNS needs both protocols. UDP carries most queries. TCP carries large responses, zone transfers, and any answer that was truncated.
# firewalld (RHEL family)
sudo firewall-cmd --permanent --add-service=dns
sudo firewall-cmd --reload
# ufw (Ubuntu)
sudo ufw allow 53/udp
sudo ufw allow 53/tcpOpen the port only on the interfaces and networks that should reach the server. A resolver exposed to the whole internet becomes an open resolver, a topic the hardening chapter covers. On the RHEL family, SELinux is enforcing by default and the packages ship with correct policy for the default paths. If you put zone files in a custom directory, they need the named_zone_t label, or named cannot read them.
Common failures
| Symptom | Likely cause | Check |
|---|---|---|
| `Address already in use` | Wildcard bind overlaps `127.0.0.53` or another daemon | `ss -lntup` |
| Host cannot resolve after change | `resolv.conf` still points at the removed stub | `readlink -f /etc/resolv.conf` |
| Service active but remote clients time out | Firewall closed on UDP or TCP 53 | `ss` shows the listener, then test from the client with `dig` |
| `named` cannot read a zone file | Wrong owner, mode, or SELinux label | `journalctl -u named` |
Exercise
Your server has the address 192.0.2.10 and runs systemd-resolved with its default stub. Unbound with interface: 0.0.0.0 fails to start with an "address already in use" error. Name two different fixes, and say what each one leaves unchanged or changes on the host.
Show answer
First fix: change Unbound to interface: 127.0.0.1 and interface: 192.0.2.10 so it avoids 127.0.0.53. This leaves systemd-resolved and /etc/resolv.conf untouched. Second fix: set DNSStubListener=no in a drop-in under /etc/systemd/resolved.conf.d/ and restart systemd-resolved. This frees the loopback address but breaks the default /etc/resolv.conf, so you must repoint it to /run/systemd/resolve/resolv.conf or to your own resolver.
Exercise
Which command shows the process that owns port 53, and why does it need sudo?
Show answer
sudo ss -lntup | grep ':53 '. Without root, ss -p cannot show process details for sockets owned by other users, so the users field is empty.