Are you experiencing technical issues that are believed to be caused by DNS caching on your Linux system? In this blog post, we explain what is DNS, DNS caching, how to flush system-level cache from the command line, and how to flush your web browser DNS cache.
What Is DNS?
DNS is an internet protocol that stands for domain name system and could be compared to an address. Computers don’t directly use domains like namehero.com, instead they they use IP addresses. This is where DNS resolution comes into play, it can look up an IP address from a domain name. The result is that the device can make a connection to the destination IP addresses and make a request.
What Is A DNS Cache?
You can think of DNS caching as keeping a local address book. Instead of calling someone to get their address every time, you can look it up on your own. Then you only need to reach out to ask if the information is still correct.
DNS cache entries are only considered valid for some time. The domain administrator sets the maximum time. That means it could be shorter than depending on how the cache is configured.
Why Might I Need To Flush DNS Cache?
The most common reason for flushing your DNS cache is when related to a technical issue. This is mainly due to the cache being cleared out as TTLs expire.
System-Level DNS Cache
On Linux systems, DNS caching isn’t part of Linux directly which is a bit unique compared to other operating systems. Instead, DNS caching is added by additional software, generally, the choice to add DNS caching is either made by the Linux Distribution or the end user directly.
How To Check If A Local DNS Cache Is Being Used.
To check for a local cache the most universal way is to check for services listening on port 53. The following command is one way to do that:
sudo lsof -i :53 +c 0
This command will list anything listening on the port DNS uses without truncation.
For example, this is what it could look like
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd-resolve 285598 systemd-resolve 21u IPv4 3462282 0t0 UDP _localdnsstub:domain
systemd-resolve 285598 systemd-resolve 22u IPv4 3462283 0t0 TCP _localdnsstub:domain (LISTEN)
systemd-resolve 285598 systemd-resolve 23u IPv4 3462284 0t0 UDP _localdnsproxy:domain
systemd-resolve 285598 systemd-resolve 24u IPv4 3462285 0t0 TCP _localdnsproxy:domain (LISTEN)
As you can see in my case systemd-resolve is being used.
A Service Listening On Port 53 Does Not Directly Indicate DNS Caching
Be aware that just having a service listening in port 53, does not directly mean that caching is configured.
This is because some services that do caching, are more focused on being a DNS server and vice versa.
The most common services you will see being used for DNS caching are listed below:
- systemd resolved
- nscd
- dnsmasq
Flush Systemd DNS Cache
Before clearing the DNS cache in systemd-resolved I would recommend looking at the stats so you can compare after the fact. That command is:
sudo systemd-resolve --statistics
Next to clear the cache use the following command:
sudo systemd-resolve --flush-caches
Now rerun the first command to confirm DNS was successfully flushed.
sudo systemd-resolve --statistics
Below is only the cache section of the output, showing the cache at 0 after being flushed.
Cache
Current Cache Size: 0
Cache Hits: 963
Cache Misses: 2688
Flush NSCD DNS Cache
To flush the DNS cache with NSCD you just need to restart the service. For most systems that will be through systemctl as shown below.
systemctl restart nscd.service
Flush Dnsmasq Cache
To flush the dnsmasq service’s, cache it is also done by issuing a restart.
systemctl restart dnsmasq
Application-Level DNS Cache
Be aware that some applications have a separate DNS cache, such as web browsers. Below are the steps on how to clear your browser’s cache, specifically Firefox and Google Chrome.
Firefox: Clear DNS Cache
- Enter about:networking#dns in the address bar.
- Click the Clear DNS Cache button
- Optional: Refresh the page to confirm Firefox’s DNS Cache was cleared
Chrome: Clear DNS Cache
- Enter chrome://net-internals/#dns in the address bar.
- Click the Clear Host Cache button
Note: for other Chromium-based browsers you should be able to replace Chrome with the name of the browser.
Conclusion
In this post, we started by going over the basics like what is DNS, what a DNS cache is, and why you might need to flush a DNS cache. From there we break down that there are both system and even application-level DNS caches. Finally, we discuss how to clear both types of DNS cache.
Additional Considerations
If you have cleared all local DNS caches and did not solve the problem be aware DNS caching is not limited to running on a local device. DNS caching can also be run at a network or DNS provider level. If this is the cause it may be solved by speaking with your network administrator or switching the Public DNS provider being used.
Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.
Leave a Reply