The DNS cache is a temporary database on an operating system that stores DNS lookups. And first what is DNS? DNS is Domain Name Service responsible for resolving websites names into their respective IP addresses.
DNS lookup shows information about visited websites and internet domains. But how do DNS lookups work? When you visit a website on numerous occasions, your operating system retrieves the information for the website from the locally stored cache rather than using the slower DNS public records. This helps to fasten the load time.
There are times when a DNS cache becomes corrupted. When this happens DNS cache needs to be flushed on your operating system.
To ensure that the DNS is flushed, the operating system should be using systemd and the user is running Sudo privileges.
The first thing we should do is ensure that systemd-resolved is running. To do that open your terminal and type the following command on your server.
$ sudo systemctl is-active systemd-resolved
active
From here we need to check the statistics for the DNS cache. Use the following command:
$ sudo systemd-resolve --statistics
DNSSEC supported by current servers: no
Transactions
Current Transactions: 0
Total Transactions: 4913
Cache
Current Cache Size: 45
Cache Hits: 481
Cache Misses: 366
DNSSEC Verdicts
Secure: 0
Insecure: 0
Bogus: 0
Indeterminate: 0
Now you can see that the current cache size is 45. We need to reset that to 0 by using the following command:
$ sudo systemd-resolve --flush-caches
Once the command has been initiated run the statistics command again to check
$ sudo systemd-resolve --statistics
Transactions
Current Transactions: 0
Total Transactions: 4916
Cache
Current Cache Size: 0
Cache Hits: 482
Cache Misses: 368
DNSSEC Verdicts
Secure: 0
Insecure: 0
Bogus: 0
Indeterminate: 0
Conclusion
In this article, we have learned how to flush DNS cache from Ubuntu server 20.04. One way or another you are faced with the challenge of your website not loading properly and you can.t figure out the problem. Flush the DNS cache and everything will be fine.