Clearing the DNS cache on a Linux machine is a fundamental troubleshooting step that resolves a surprising number of connectivity issues. When you browse the web, your system stores responses from DNS lookups to speed up future requests. However, when a website changes its IP address or a cached entry becomes corrupted, your machine can continue to reference the old, incorrect data. This guide provides a precise, distribution-agnostic method to purge this stored information and restore proper network resolution.
Understanding the DNS Cache Mechanism
Before initiating a flush, it is essential to understand what the cache actually is and why it exists. Linux does not rely on a single, universal caching service; instead, this functionality is often provided by a stub resolver built directly into the C library, a dedicated caching daemon like nscd or systemd-resolved , or the NetworkManager applet. These components intercept DNS queries, check if a recent answer exists locally, and return that data without contacting the upstream server. While this behavior reduces latency, it can become a liability when network configurations change, making the manual clearance of this storage necessary for accuracy.
Identifying Your Active Resolver
Flushing the Cache for systemd-resolved
If your system uses systemd-resolved , which is standard on distributions utilizing systemd, the process is streamlined and controlled entirely through the command line interface. This service maintains statistics and a cache of DNS records to improve performance. To purge all negative cache entries, valid entries, and DNSSEC records, you must execute a specific flush command. This operation requires root privileges, ensuring that only authorized users can reset the network state.
Execute the Flush Command
To clear the cache for systemd-resolved , open a terminal and run the following instruction:
sudo systemd-resolve --flush-caches
Upon execution, the command will silently return to the prompt if successful. To verify that the operation completed without error and to view the current statistics of the cache, including the number of cached entries, you can run:
sudo systemd-resolve --statistics
This will display counters for current cached entries, cache hits, and misses, confirming that the subsystem is active and responsive.
Flushing the Cache for nscd
Although largely deprecated in favor of systemd-resolved , Name Server Cache Daemon ( nscd ) is still present in some legacy or minimal Linux installations. If your system relies on this daemon for name service caching, the flush process is distinct from the systemd method. You must interact with the init script directly to instruct the daemon to discard its internal storage.
Execute the Flush Command
To clear the cache managed by nscd , use the following command: