Flushing the DNS cache on a Linux machine is a fundamental troubleshooting step that resolves many connectivity issues. When you browse the web, your system stores the IP addresses of visited domains in a local cache to speed up future requests. However, when a website changes its server or IP address, this cached data can become stale, causing errors or preventing access to the updated location. By clearing this stored information, you force your resolver to query authoritative nameservers for the latest records, effectively resetting your network path to the correct destination.
Understanding the DNS Cache Mechanism
The DNS cache is not a single file but a system managed by various services depending on your distribution and configuration. Modern Linux systems often rely on systemd-resolved , NetworkManager , or dedicated caching daemons like nscd or dnsmasq . The operating system maintains this cache in memory to reduce latency and decrease the number of queries sent to upstream servers. While this process is efficient, it requires manual intervention when network configurations change or when experiencing DNS hijacking and poisoning attacks.
Checking Your Current Resolver
Before attempting to flush the cache, you must identify which service is managing DNS resolution on your system. Using the systemd-resolve or resolvectl command provides detailed status information about the cache and the current nameservers in use. This diagnostic step ensures you are interacting with the correct subsystem, preventing frustration if the standard flush command does not apply to your specific setup.
Methods to Flush DNS Resolution
The method you use depends entirely on the init system and DNS service running on your machine. For systems utilizing systemd-resolved , the process is straightforward and requires specific elevated privileges. Alternatively, systems relying on older init scripts or external daemons require distinct terminal commands. Below is a summary of the most common approaches based on the active service.
Using resolvectl for Verification
After executing the flush command, you can verify that the cache statistics have reset using resolvectl statistics . This command displays packet counts and cache hit ratios, confirming that the previous data has been cleared. A successful flush will typically show a spike in outgoing queries immediately following the operation as the system rebuilds its cache with fresh data.