How to Clear DNS Cache on Ubuntu?

Clearing the DNS cache on Ubuntu is a straightforward process that helps resolve connection errors, fixes outdated domain routing, and applies recent DNS changes immediately. Depending on your version of Ubuntu, the system uses different network management services—most commonly systemd-resolved or dnsmasq—to handle DNS caching. This guide provides the exact commands needed to flush the DNS cache across different Ubuntu versions and alternative services.

Flushing DNS Cache with systemd-resolved

Most modern versions of Ubuntu (including Ubuntu 18.04, 20.04, 22.04, and 24.04 LTS) use systemd-resolved by default to manage DNS caching.

To clear this cache, open your terminal (Ctrl + Alt + T) and run the following command:

sudo resolvectl flush-caches

Verifying the Cache Clearance

To confirm that the cache was successfully cleared and to check your current DNS statistics, you can run:

sudo resolvectl statistics

Look for the Current Cache Size field in the output; it should show zero or a significantly reduced number immediately after flushing.

Clearing DNS Cache on Older Ubuntu Versions

If you are running older versions of Ubuntu (such as 16.04 or earlier), the system may rely on the older systemd-resolved syntax. You can clear the cache by restarting the service or using the legacy flush command:

sudo systemd-resolve --flush-caches

Flushing DNS Cache for Alternative Services

Some custom Ubuntu configurations or servers utilize alternative DNS forwarding daemons like Dnsmasq or BIND. If your system uses one of these, use the corresponding command below.

Dnsmasq

Dnsmasq keeps its cache in the system memory. To clear it, you need to restart the service:

sudo systemctl restart dnsmasq

BIND (Named)

If your Ubuntu machine acts as a DNS server running BIND, you can flush its specific cache using the rndc tool:

sudo rndc flush

Clearing Browser-Specific DNS Cache

Sometimes, flushing the operating system’s DNS cache is not enough because web browsers maintain their own internal DNS caches to speed up browsing.