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.
Table of Contents
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
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
We can run the following command to check the DNS Cache statistics.
# sudo systemd-resolve --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.
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
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.