How to Clear DNS Cache on Ubuntu

Filed Under: Ubuntu
Ubuntu Flush Dns Cache

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.

# sudo systemctl status systemd-resolved
Ubuntu Systemd Resolved Service Status
Ubuntu systemd-resolved.service Status

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

# sudo systemd-resolve --statistics
Ubuntu 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.

# sudo systemd-resolve --flush-caches

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.

# sudo systemctl restart systemd-resolved
Ubuntu Restart Systemd Resolved Service
Ubuntu Restart Systemd Resolved Service

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages