How to Clear DNS Cache on Ubuntu With Examples

Whenever we try to access a website, a DNS name resolution query is executed to resolve its IP address. The operating systems cache this data to avoid reaching out to name servers every time.

Why to Clear DNS Cache?

Sometimes a website IP address is changed when it’s moved to another server. In that case, if we hit the cached IP address, the website won’t work.

DNS entries have the “Time To Live (TTL)” value associated with it, which tells the Operating System name resolution service when to invalidate the DNS Cache.

But, if you want to reach out to the new IP address before TTL expires, the only solution is to flush the DNS cache.

Ubuntu Service for DNS Name Resolution

The systemd-resolved.service is used to run the DNS queries and maintain the DNS Cache.

We can use the following command to check its status.

We can run the following command to check the DNS Cache statistics.

ubuntu-dns-cache-statistics

There is a slight difference in the service and command name. The command name doesn’t have the last character “d”.

How to Clear DNS Cache on Ubuntu?

We can run the following command to clear the DNS cache on Ubuntu.

Then, check the statistics again to confirm that the “Current Cache Size” is 0.

ubuntu-dns-flush-cache

Ubuntu Dns Flush Cache

Restarting systemd-resolved service to clear all DNS Cache Statistics

The DNS flush command doesn’t clear the cache hits and misses statistics. If you want to clear all the Cache stats, then you have to restart the systemd-resolved service.

ubuntu-restart-systemd-resolved-service-1024x763

Conclusion

It’s very easy to flush cache on Ubuntu. The command to flush the DNS can be run by the root user or a user with sudo privileges.

By admin

Leave a Reply

%d bloggers like this: