Fix port 53 Address already in use (systemd-resolved, dnsmasq)
Port 53 Address already in use from dnsmasq or Pi-hole usually means the systemd-resolved stub holds 127.0.0.53. Bind a specific address or disable the stub.
dnsmasq: failed to create listening socket for port 53: Address already in useUbuntu (17.10 and later) runs systemd-resolved by default, and its stub resolver listens on 127.0.0.53:53 and 127.0.0.54:53. A new DNS server that tries to bind every address (0.0.0.0:53) collides with those two and fails. Bind the new server to specific addresses, or turn off just the systemd-resolved stub and repoint /etc/resolv.conf. You don't need to stop or remove systemd-resolved entirely.
[1] Find out what holds port 53. This is the UDP list in an Ubuntu 24.04 container where only systemd-resolved is running:
$ ss -lnup 'sport = :53'State Recv-Q Send-Q Local Address:Port Peer Address:PortProcessUNCONN 0 0 127.0.0.54:53 0.0.0.0:* UNCONN 0 0 127.0.0.53%lo:53 0.0.0.0:* TCP (ss -lntp) shows the same two addresses in LISTEN. The Process column was empty in the container test; on a real server, sudo ss -lnup 'sport = :53' shows the process name. If the addresses are 127.0.0.53 and 127.0.0.54, it's systemd-resolved. If you see another name (dnsmasq, named, pihole-FTL, docker-proxy), that program is already running — deal with that service instead. An address of 0.0.0.0:53 or [::]:53 means that program holds every address.
[2] See why they collide. In the same state, binding every address fails immediately:
$ python3 -c 'import socket;s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM);s.bind(("0.0.0.0",53))'Traceback (most recent call last): File "<string>", line 1, in <module>OSError: [Errno 98] Address already in use0.0.0.0 means "every IPv4 address on this machine", so a single socket on 127.0.0.53 is enough to block it. dnsmasq started with no options tries the same thing and printed the error at the top. For the same reason, publishing a port without an address, as in docker run -p 53:53/udp, tries to take the host's 0.0.0.0:53 and fails.
[3] Bind the new DNS server to specific addresses. When the addresses don't overlap with the stub, both run side by side. For dnsmasq, set bind-interfaces and listen-address:
bind-interfaceslisten-address=127.0.0.1Here dnsmasq runs with the same settings given on the command line, next to systemd-resolved. It started without errors and answered queries:
$ ss -lnup 'sport = :53'State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess UNCONN 0 0 127.0.0.1:53 0.0.0.0:* users:(("dnsmasq",pid=3208,fd=4))UNCONN 0 0 127.0.0.54:53 0.0.0.0:* UNCONN 0 0 127.0.0.53%lo:53 0.0.0.0:* $ dig @127.0.0.1 example.com A +short172.66.147.243104.20.23.154For a server that answers other devices on the network, use the LAN address instead of 127.0.0.1 (for example listen-address=192.0.2.10). In BIND that's listen-on { 192.0.2.10; };; in Docker, publish with the address: -p 192.0.2.10:53:53/udp -p 192.0.2.10:53:53/tcp. This approach leaves systemd-resolved untouched, so there is nothing to roll back.
[4] If the server really must listen on every address (Pi-hole published without an address, for instance), turn off only the stub. This is the sequence from the Pi-hole docs; per the systemd docs, DNSStubListener=no disables the stub on both 127.0.0.53 and 127.0.0.54.
$ sudo mkdir -p /etc/systemd/resolved.conf.d$ printf '[Resolve]\nDNSStubListener=no\n' | sudo tee /etc/systemd/resolved.conf.d/no-stub.conf$ sudo sh -c 'rm -f /etc/resolv.conf && ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf'$ sudo systemctl restart systemd-resolvedDon't skip the ln -s line. On Ubuntu, /etc/resolv.conf is a link to stub-resolv.conf, which lists the stub address 127.0.0.53, and the Pi-hole docs note that disabling the stub doesn't change that nameserver setting, so it keeps pointing at the stub. /run/systemd/resolve/resolv.conf lists the upstream DNS servers directly. In the container, restarting systemd-resolved with this setting emptied the port 53 list, and dnsmasq with no options came up on every address:
UNCONN 0 0 0.0.0.0:53 0.0.0.0:* users:(("dnsmasq",pid=3210,fd=4))UNCONN 0 0 [::]:53 [::]:* users:(("dnsmasq",pid=3210,fd=6))In this test, systemd 255 turned stub-resolv.conf itself into a link to resolv.conf once the stub was off. There's no documentation saying every version does this, so change the /etc/resolv.conf link as the docs say.
[5] After the fix, check two things: the new server is on port 53 (sudo ss -lnup 'sport = :53'), and this machine still resolves names (getent hosts example.com). If the second fails with Temporary failure in name resolution, recheck the link step in [4]; Temporary failure in name resolution covers how to narrow it down. If a web port collides instead of 53, see nginx bind() address already in use.
Check it with the dig output explainer tool ↗ — calculated in your browser.