Home DNS How To Clear Or Flush DNS Cache In Linux

How To Clear Or Flush DNS Cache In Linux

By sk
Published: Updated: 46K views

This brief guide explains all possible ways to clear or flush DNS cache in Linux operating systems. For those wondering, DNS cache (or sometimes called a DNS resolver cache) is a small temporary database maintained by the operating systems. It stores the information about all visited websites and other Internet domains. In other words, the DNS cache contains recent DNS lookups. When you visit the same website multiple times, the OS retrieves the DNS records of that website from the local DNS cache database instead of the actual public DNS server. Hence DNS caching improves the website loading time and reduces the origin DNS server's bandwidth/CPU consumption.

Why should you clear or flush DNS cache?

There could be many reasons to clear the local DNS cache. For instance, you might have customized your website or changed the DNS settings for your domain or you are facing some Internet connectivity issues. In such cases, if you attempt to visit the site, your OS will likely to get the cached version of the site from the local DNS cache. So clearing the local DNS cache will delete all entries from the local DNS cache database and force your system to repopulate the DNS cache with new entries from the actual DNS server and display the fresh copy of the website.

Clear Or Flush DNS Cache In Linux

By default, the DNS caching is not installed and/or enabled at the operating system level in Linux. Instead, applications like web browsers maintain their own internal cache. So you can simply clear your browser's cache or restart them to fix the Internet-connectivity issues, resolve DNS spoofing (e.g. DNS cache poisoning) issues and resolve stale content issues. If your Linux distribution does use any DNS caching services, you need to manually clear them as described below. There are multiple ways to clear DNS cache. It varies depends upon the Linux distribution and DNS caching service you are using. Here I have included instructions to clear the most commonly used DNS caching services in Linux.

1. Clear Systemd-resolve DNS caching service

Systemd-resolve is a systemd service that provides system-level DNS caching service for local applications. It is part of the systemd package which is installed by default. Systemd-resolved service is enabled by default in most modern Linux operating systems such as Fedora and Ubuntu.  First, let us check if systemd-resolved service is active or not using command:

$ sudo systemctl is-active systemd-resolved.service

If systemd-resolved service is enabled, you will see "active" in the output. If it is not active, it will print "inactive".

Check if systemd-resolved service is active
Check if systemd-resolved service is active

As you see in the above output, systemd-resolved service is active in my system.

Let us view the statistics of systemd-resolve caching service with command:

$ sudo systemd-resolve --statistics

Sample output:

DNSSEC supported by current servers: no

Transactions              
Current Transactions: 0   
  Total Transactions: 3117
                          
Cache                     
  Current Cache Size: 58  
          Cache Hits: 1248
        Cache Misses: 1891
                          
DNSSEC Verdicts           
              Secure: 0   
            Insecure: 0   
               Bogus: 0   
       Indeterminate: 0

systemd-resolve statistics
systemd-resolve statistics

As you see, the above output lists the total no of current cache size, hits and missed caches.

To flush the systemd-resolve cache, run:

$ sudo systemd-resolve --flush-caches

On successful execution, you will not see any output.

Now, check the systemd-resolve statistics to verify if the local DNS cache is cleared or not.

$ sudo systemd-resolve --statistics

You will now see current cache size as zero.

DNSSEC supported by current servers: no

Transactions              
Current Transactions: 0   
  Total Transactions: 3155
                          
Cache                     
  Current Cache Size: 0   
          Cache Hits: 1255
        Cache Misses: 1922
                          
DNSSEC Verdicts           
              Secure: 0   
            Insecure: 0   
               Bogus: 0   
       Indeterminate: 0

Display systemd-resolve statistics
Display systemd-resolve statistics

2. Clear Nscd DNS caching service

Nscd, short for name service cache daemon, is a caching daemon that provides a cache for the most common name service requests. It is the preferred caching service for most Redhat-based systems. If your Linux system is using nscd caching service, you can clear or flush the local DNS caching service by simply restarting the nscd service like below:

$ sudo systemctl restart nscd.service

Or,

$ sudo service nscd restart

Or,

$ sudo /etc/init.d/nscd restart

3. Clear Bind/Named DNS caching service

Berkeley Internet Name Domain (shortly BIND) is the most popular open source DNS server in use today. If you are using BIND for caching DNS queries, use the following commands to clear/flush BIND caching service in Linux:

$ sudo systemctl restart named

Or,

$ sudo service named reload

Or,

$ sudo /etc/init.d/named restart

Or,

$ sudo rndc reload

Or,

$ sudo rndc exec

The latest BID version allows you to clear DNS cache for a specific domain as well. The following command clears the DNS cache for ostechnix.com domain:

$ sudo rndc flushname ostechnix.com

You can also clear DNS cache for entire LAN and WAN zones like below:

$ sudo rndc flush lan

$ sudo rndc flush wan

4. Clear Dnsmasq DNS caching service

Dnsmasq is a lightweight DHCP and caching DNS server with support for PXE and TFTP server. It is suitable for resource constrained routers and firewalls to manage small networks.

If you are using dnsmasq as caching service, you can flush the local DNS caching service like below:

$ sudo systemctl restart dnsmasq

Another way to clear local DNS cache in Ubuntu and other Ubuntu variants such as Linux Mint is to run the following command:

$ sudo /etc/init.d/dns-clean restart

Hope this helps.

Related read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More