To flush the DNS cache on an Ubuntu server, the method depends on which DNS resolver your system is using. Here’s how to do it for the most common setups:
🧼 For Ubuntu 22.04+ (Using systemd-resolved)
Ubuntu now uses systemd-resolved for DNS caching. To flush the cache:
sudo resolvectl flush-caches
You can verify the cache was cleared by checking statistics:
resolvectl statistics
Look for Cache Current Size: 0 to confirm the flush.
🧼 For Ubuntu 20.04 and Earlier (Using systemd-resolve)
If your system still uses the older command:
sudo systemd-resolve --flush-caches
This is functionally similar and works on many 20.04 setups.
🧼 If You’re Using dnsmasq
Some Ubuntu setups (especially custom or lightweight ones) use dnsmasq:
sudo systemctl restart dnsmasq
This restarts the service and clears its cache.
🧼 If You’re Using bind9
For servers running BIND:
sudo rndc flush
Or to flush a specific zone:
sudo rndc flushname example.com
🧠Pro Tip
If you’re troubleshooting DNS issues, you might also want to clear your local resolver cache and browser cache, or test with:
dig example.com
to bypass local caching and query directly.