News & Updates

The Ultimate Guide to Clear DNS Cache in Linux: Fast & Secure

By Noah Patel 73 Views
how to clear dns cache inlinux
The Ultimate Guide to Clear DNS Cache in Linux: Fast & Secure

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

Because the method to clear the cache depends entirely on which service is managing it, you must first identify the active resolver on your system. The quickest way to do this is by checking the contents of the /etc/resolv.conf file. If the file contains a line pointing to 127.0.0.1 or ::1 , a local daemon is likely handling your requests. You can further investigate which daemon is listening on port 53 by using commands like ss -tulpn
grep ':53' or ps aux
grep -E 'resolved
nscd
dnsmasq' . Common environments include systemd-resolved on modern Ubuntu or Fedora, nscd on older Debian systems, or the resolver integrated directly into NetworkManager.

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:

N

Written by Noah Patel

Noah Patel is a Senior Editor focused on business, technology, and markets. He favors data-backed analysis and plain-language explanations.