Why Flush DNS Cache on Linux
Your Linux system caches DNS lookups to speed up repeated requests. This is good for performance, but bad when you change DNS servers or a website moves to a new IP address. Stale cache entries cause connection errors, showing you old IP addresses that no longer work.
Common scenarios where flushing DNS helps: after switching to a new DNS provider, after migrating a website to a new server, when a site you visit starts giving DNS errors, or after configuring VPN or proxy settings. If you change DNS on Linux and sites still load slowly, stale cache is probably the reason.
Different Linux distributions use different DNS caching services. Ubuntu uses systemd-resolved. Debian might use systemd-resolved or dnsmasq. Arch uses systemd-resolved by default. You need to know which one your system uses before you flush.
Flush systemd-resolved Cache
systemd-resolved is the default DNS resolver on Ubuntu (18.04+), Fedora, Debian, Arch Linux, and most modern distributions. It handles DNS caching, DNSSEC, and DNS over TLS.
Command:
sudo resolvectl flush-caches
That is it. One command, and the cache is gone. Older systems might use sudo systemd-resolve --flush-caches, but resolvectl is the modern replacement.
To check the cache status before and after flushing:
resolvectl statistics
Look for "Current Cache Size" in the output. It should drop to zero after flushing. The command also shows cache hits, misses, and DNSSEC statistics, which are useful for debugging.
Flush dnsmasq Cache
dnsmasq is a lightweight DNS forwarder often used in network environments. It is common on Debian and Ubuntu systems that run NetworkManager, and on many routers running OpenWrt or DD-WRT.
Command:
sudo systemctl restart dnsmasq
Restarting the service clears all cached entries. You can also send a SIGHUP signal to clear the cache without a full restart:
sudo killall -HUP dnsmasq
If dnsmasq is running as a daemon and you do not have systemd, use the /etc/init.d/dnsmasq restart script instead.
Flush nscd Cache
nscd (Name Service Cache Daemon) is older but still used on some systems, especially enterprise Linux distributions and older Debian/Ubuntu versions.
Command:
sudo systemctl restart nscd
Or on systems without systemd:
sudo /etc/init.d/nscd restart
nscd caches more than just DNS. It also caches passwd, group, and hosts information. The restart clears all of those caches. If you want to clear only the DNS hosts cache without restarting, you can send a SIGINT signal, but restarting is simpler and guaranteed to work.
Flush BIND Cache
If you are running a BIND DNS server on your Linux machine, flush the cache with:
Command:
sudo rndc flush
rndc is the remote name daemon control utility. It flushes all cached records. Use rndc status to check the cache statistics before and after.
Flush Browser DNS Cache
Your browser might have its own DNS cache. Chrome and Chromium-based browsers use a built-in DNS cache that is separate from the system cache. To flush it:
Chrome/Chromium: Visit chrome://net-internals/#dns and click "Clear host cache".
Firefox: Firefox uses the system DNS by default. Restart the browser or go to about:networking#dns and click "Clear DNS Cache".
Brave/Edge/Opera: Same as Chrome. Visit the net-internals page and clear the cache.
You can also just restart your browser. That clears all caches, including DNS, TLS sessions, and HTTP cache.
Verify the Cache Was Cleared
After flushing, verify with these commands:
resolvectl statistics — Check "Current Cache Size" (systemd-resolved).
dig example.com — A fresh lookup that shows the response time. A high response time on the first lookup after flush is normal because the cache is empty.
ping example.com — If the IP address matches the new DNS server's answer, the cache is working correctly.
Frequently Asked Questions
How do I know which DNS caching service my Linux uses?
Run sudo lsof -i :53 -S to see which process is listening on port 53. That is your DNS resolver. Alternatively, check if systemd-resolved is active with systemctl is-active systemd-resolved.
Do I need to flush DNS after changing DNS servers on Linux?
Yes. If you do not flush the cache, your system might keep using cached results from the old DNS server for the duration of the TTL. Flushing ensures all new lookups use the new server.
How often does Linux clear DNS cache automatically?
DNS entries expire based on their TTL values set by the domain owner. Most TTLs range from 60 seconds to 24 hours. The cache is cleared automatically when entries expire, but manual flushing is faster.
Does flushing DNS cache affect system performance?
Only temporarily. The first few DNS lookups after a flush will be slower because the cache is empty. Performance returns to normal once frequently accessed domains are re-cached.