Preventing systemd-resolved from caching non-existent domains

If you use a recent version of Ubuntu, all DNS resolution is ultimately handled by a locally running server that is part of systemd. This works fine for the most part.

All hosts on my network get a domain name assigned to them as soon as the DHCP lease is formed. So a common thing I do is to plug in a device, then try and SSH to This works, so long as the device has already received a DHCP lease. If it hasn't, systemd had a very frustrating behavior by default where it would keep the result of the DNS lookup in the cache. So I couldn't try and SSH until the cache entry had expired.

This behavior is easy enough to disable, by editing /etc/systemd/resolved.conf as root. Find or uncomment the line starting with Cache= and then set it to this


This stops systemd from caching an NXDOMAIN response, which means if a domain does not exist then trying to resolve it again makes a query back to the DNS server. You need to restart the systemd component by running sudo systemctl restart systemd-resolved for the updated configuration to take effect.

Copyright Eric Urban 2022, or the respective entity where indicated