Flush DNS Cache on Linux
Flush the DNS Cache
Depending on the age of your Linux distribution, you can use one of two commands to clear the DNS cache using systemd-resolved.
The Modern Command
For newer distributions (like Ubuntu 18.04 and later, or recent Raspbian versions), the standard tool for interacting with systemd-resolved is resolvectl. To flush the cache, run:
sudo resolvectl flush-caches
The Older Command
On older distributions that use systemd but predate the transition to resolvectl, you will use the systemd-resolve command instead:
sudo systemd-resolve --flush-caches
Neither command will produce an output message if successful.
How to Verify It Worked
If you want to confirm that the cache was actually flushed, you can check the statistics before and after running the flush command. Run this command:
sudo systemd-resolve --statistics
Look for the "Current Cache Size" field; after a successful flush, this number will drop to 0.