The DNS cache is a local database of domain names and addresses that helps speed up name resolution. The records in this cache can sometimes be incorrect/outdated, which leads to various DNS-related network errors. The easiest way to fix such issues is to clear the DNS cache.
Flush DNS on Ubuntu 22.04
Ubuntu handles name resolution and caching with the systemd-resolved
service. Rather than directly managing the service, you can use the resolvectl
binary to query the DNS records or perform actions like flushing the cache.
First, check the DNS cache stats with
sudo resolvectl statistics
Clear the cache using the flush-caches parameter.
sudo resolvectl flush-caches
Verify the cache’s state again with
sudo resolvectl statistics
Flush DNS on Older Ubuntu Versions
On Ubuntu versions 20.04 and older, systemd-resolve
could be directly called to clear the cache. Resolvectl is backward compatible with older Ubuntu versions, so the previous method works too. But you can also flush the cache with this alternative method.
Check the current cache statistics with
sudo systemd-resolve --statistics
Use the flush-caches option to clear the cache.
sudo systemd-resolve --flush-caches
Verify that the cache was cleared.
sudo systemd-resolve --statistics
Flush Custom DNS Service Cache
If you’re running a custom DNS service like dnsmasq, nscd, unbound, bind, etc., you’ll need to clear their cache separately as well.
For instance, you can flush the dnsmasq cache by sending a hangup signal (SIGHUP) to the dnsmasq process.
sudo killall -HUP dnsmasq
If you’re running dnsmasq as a service, you can instead use
sudo systemctl restart dnsmasq
This was just one example. As there are numerous third-party DNS services to choose from, we recommend referring to the program-specific documentation for instructions on how to clear the cache.