How to Flush DNS Cache on Linux?

You may have observed some websites you frequently visit load significantly faster than others. This phenomenon results from your Linux Operating System's browser and DNS cache. These caches enable your Linux device to fetch DNS information locally, eliminating the need to wait for a public DNS resolver, thus contributing to quicker website loading times.

However, there are instances where the DNS cache may become invalid or corrupted, resulting in problems when loading websites in your browser. In such cases, you must command your Linux system to clear the DNS cache.

Let's delve into clearing the DNS cache on Linux and determine whether it's necessary for you.

What is a DNS Cache?

The Domain Name System (DNS) is a worldwide naming system that matches domain names with their respective server IP addresses. On the user side, the DNS resolver (also known as the Stub Resolver) is responsible for querying the DNS cache, thereby reducing the strain on upstream DNS servers.

The DNS cache is a transient database containing the domain-to-IP translations your Operating System (OS) collected.

Within the DNS cache, you'll find Resource Records (RR) containing the IP addresses of the domain names you often navigate to.

These records encompass essential elements such as Name, Record type, Time to Live (TTL), Class, Resource data, and Resource data length. Time to live (TTL) signifies the duration (measured in seconds) during which a record remains valid before requiring a fresh DNS lookup. All the information within a record file is encoded in ASCII format, with each domain name appearing on a distinct line. Below is a typical format for a Resource Record:

<name> <ttl> <class> <type> <rdlength> <radata>

When loading a website, your browser asks your OS for the domain's IP. The OS checks its local DNS resolver; if the IP isn't there, it requests it from a public DNS server and caches it for later use.

Having a DNS cache record for a domain speeds up website loading. It optimizes DNS lookup, improving your browsing experience. However, it's temporary and can expire or become corrupted, leading to DNS errors. These errors may relate to DNS propagation or spoofing attacks. Resetting the DNS cache can enhance privacy and resolve such issues. You can force a DNS flush on Linux to clear old data and refresh it with new records for future queries.

Find Your Local DNS Resolver

You'll find either "systemd-resolved" or "dnsmasq" as the DNS resolver on many Linux systems. To determine which one you have, you can use the following command:

# sudo lsof -i :53 -S

If you receive an error as shown in the above picture, then install lsof and again shoot the same command:


# apt-get install lsof


# apt-get install lsof


# yum install lsof

# sudo lsof -i :53 -S

Note: Why are we executing this command? Since DNS operates on port 53, we are searching for commands linked to the service using port 53, corresponding to your local DNS resolver or "stub."

Once you've determined which DNS resolver your system utilizes, you can proceed with the appropriate DNS cache flushing command for your Linux distribution.

How to Flush DNS Cache in Linux

Clearing the Linux DNS Cache with systemd-resolved

The most straightforward method to clear the DNS cache on Linux when utilizing systemd-resolved is to employ the "system-resolve" command, followed by "--flush-caches."

Alternatively, you may opt for the "resolvectl" command and the "flush-caches" option.

Does your system utilize systemd-resolved? Try:

systemctl is-active systemd-resolved

If this command returns "active," your system has systemd-resolved enabled and running. If it displays anything else (e.g., "inactive"), systemd-resolved is not in use on your system.

Note: If it displays inactive and you wish to use it, Enable Systemd-Resolved.

Use these commands to deactivate and stop the systemd-resolved service:

# sudo systemctl disable systemd-resolved.service

# sudo systemctl stop systemd-resolved.service

Execute the following commands to enable and initiate the systemd-resolved service:

# sudo systemctl enable systemd-resolved.service

# sudo systemctl start systemd-resolved.service

To Flush DNS, use the following command:

# sudo resolvectl flush-caches

# sudo systemd-resolve --flush-caches

Please note that the commands mentioned above do not produce any confirmation output. Nevertheless, you can inspect the Current Cache Size value using the following commands.

# sudo systemd-resolve --statistics

# sudo resolvectl statistics

It will display the output as shown below:

How do you flush the DNS cache using BIND?

BIND, or "named," is an authoritative name server for specific domains and a recursive resolver for the broader DNS system, including caching. It has undergone three major revisions, with BIND4 and BIND8 considered obsolete. BIND9 is the supported version, offering features like DNSSEC and other enhancements.

To confirm if BIND (service name "named") is active on your system (usually only functional when explicitly installed and enabled), you can use:

# systemctl is-active named

If this command returns "active," it indicates the service is running on your system.

On older systems or those without systemd, you can check the service status using the following command:

service named status

To Flush the DNS Cache, use the following command:

sudo rndc flush

Verifying whether the DNS cache has been cleared in BIND is slightly more involved. Following the cache flush, execute the command provided below to display the cache:

sudo rndc dumpdb -cache

The cache file is now stored at /var/named/data/cache_dump.db. You can examine this file as the root user to inspect any cached DNS entries:

sudo cat /var/named/data/cache_dump.db

If the cache has been flushed (resulting in no cached DNS entries), this file should appear as follows:

If the output is anything else, the DNS cache was not flushed.

Flush DNS cache using signals

You can also clear the DNS cache in a systemd-resolved system by sending a "USR2" signal to the DNS resolver.

$ sudo killall -USR2 systemd-resolved

To verify the successful DNS cache flush, you can send a "USR1" signal to the systemd-resolved service, prompting it to record its current state in the systemd journal.

$ sudo killall -USR1 systemd-resolved

$ sudo journalctl -r -u systemd-resolved

Flush the DNS cache using Dnsmasq.

If your system utilizes "dnsmasq" for DNS resolution, you can use the following command as illustrated:

# sudo killall -HUP dnsmasq

Like with systemd-resolved, you can send a "USR1" signal to the process, causing it to output its statistics to the "syslog" log file. A basic "tail" command can confirm that the DNS cache has been flushed.

# sudo killall -USR1 dnsmasq

# tail -f n1000 /var/log/syslog |grep "cache size"

Running Dnsmasq service

To verify if dnsmasq is currently running on your system, you can execute:

# systemctl is-active dnsmasq

If this command returns "active," it indicates the service is active on your system.

On older systems or those without systemd, you can verify the service status using:

# service dnsmasq status

Note: If the service is currently inactive and you want to use dnsmasq service, you can install and enable it using the following commands:

# yum install dnsmasq

# systemctl start dnsmasq

# systemctl enable dnsmasq

# systemctl status dnsmasq

Flush the DNS cache using nscd

If you're using the RedHat Linux distribution, the Name Server Cache Daemon (Nscd) is probably in operation. You can clear the DNS cache of this service by executing the following command:

# sudo systemctl restart nscd.service

# sudo service nscd restart

Note: If you don't have nscd on your server, you can install it by executing the following command:

# yum install nscd

# service nscd start

# chkconfig nscd on

Flush DNS cache on Google Chrome

In addition to the operating system's DNS cache, the Google Chrome browser also maintains its DNS cache. Ensure that Chrome's DNS cache isn't causing issues with website access; it's advisable to clear it.

Enter the following into the Chrome address bar:


This will direct you to Chrome's internal network settings page. Click the "Clear host cache" button to clear the cache.

Why Flush DNS Cache Regularly?

Most operating systems employ DNS caching to reduce the load on DNS servers during high-traffic periods. The cache's validity period is determined by its TTL (Time to Live). As long as the cache remains valid, the operating system can fulfill content requests without consulting the DNS server.

However, corrupted or outdated DNS cache files can result in errors and security vulnerabilities. For instance, your operating system might retain a faulty DNS cache when a website relocates to a new domain or host.

Here are the key advantages of regularly flushing your DNS cache:

Prevent Search Behavior Tracking: Storing DNS records can facilitate online search tracking by hackers, so clearing the cache helps safeguard your privacy.

Enhance Security: Clearing the cache helps prevent exposing sensitive information, such as login credentials and personal data, to DNS spoofing attacks.

Resolve Technical Issues: Forcing the operating system to search for updated DNS records can resolve connection problems and issues related to incorrectly displayed web content.

In summary, if you encounter DNS errors, DNS propagation delays, or DNS spoofing concerns, it's advisable to flush your Linux DNS cache. Additionally, clearing the cache enhances network security and overall user experience. Following the outlined steps in this article empowers you to address these issues and independently ensure a secure network environment..

Was this answer helpful?

« Back