We aren't going to go into how the whole DNS protocol works. However, it is important to know that all the name and IP address mappings are cached locally. This is done for speed and efficiency. So the next time you go to putorius.net, the DNS resolver can read it's local cache instead of doing a lookup over the internet. Sometimes it is necessary to flush the DNS cache of your system. Maybe the IP address of the system you are trying to reach has changed? If so, you need to flush your local DNS cache or you will not be able to reach it.
In this Linux quick tip we will teach you how to easily flush local DNS cache to ensure you are getting current information.
Flush Local DNS Cache Using resolvectl
Most modern Linux systems like Ubuntu, Red Hat, Debian, Fedora, and Arch use systemd. Systemd-resolved is the systemd daemon that runs the local DNS resolver. It also provides tools to interact with the daemon from the command line. Two of the most widely used tools are resolvectl and systemd-resolve. Typically systemd-resolve is just a symbolic link to resolvectl. I personally prefer resolvectl because of the easier syntax and the similarity to other systemd tools.
First, let's check the cache on our system. We do this by using the resolvectl command followed by the statistics option. Here we use grep to show us only the cache stats.
[[email protected] ~]$ resolvectl statistics | grep -i cache Cache Current Cache Size: 572 Cache Hits: 10767 Cache Misses: 11147
To flush the local DNS cache use resolvectl followed by the flush-caches option.
[[email protected] ~]$ resolvectl flush-caches
Now we can check the statistics again and see the current cache size is zero (empty).
That's it! You just flushed the local DNS cache on your Linux system.
Here is an example doing the same operations using systemd-resolve.
As you can see the syntax is a little different and it's more keystrokes. I prefer resolvectl for this reason and because it more closely resembles other tools provided for systemd.
Links and Resources
This site uses Akismet to reduce spam. Learn how your comment data is processed.