Linux flush DNS cache


In this post, How to View and Flush DNS Cache on Linux. Linux flush DNS cache can solve HTTP errors and protect you from DNS spoofing

DNS Cache on Linux?

Here are a few commonly used DNS resolvers on Linux:

  1. systemd-resolved
  2. dnsmasq
  3. NetworkManager
  4. BIND (Berkeley Internet Name Domain)
  5. Unbound
  6. pdnsd

Why Flush DNS Cache on Linux?

Flushing the DNS cache on Linux can be useful in several scenarios:

  • Flushing the DNS cache ensures that your Linux system fetches the latest DNS information from authoritative DNS servers
  • A Flush DNS cache allows your system to start with a clean cache and retrieve fresh DNS information.
  • Network configuration changes
  • Clearing the DNS cache can help protect your privacy and security.

How to View the Local DNS Cache on Linux

On Linux, the specific method to view the local DNS cache depends on the DNS resolver software being used

View DNS Cache for systemd-resolved

By sending a SIGUSR1 signal to kill the systemd-resolved service

sudo killall -USR1 systemd-resolved

Use the journalctl command and the standard output operator to save the output to a text file

sudo journalctl -u systemd-resolved > /tmp/cache.txt
Linux flush DNS cache

You use the vim command line to open “/tmp/cache.txt” file. search for “CACHE:” by pressing Escape, typing “/CACHE:”, and hitting Enter.

View the Local DNS Cache for nscd

To view the local DNS cache for nscd (Name Service Cache Daemon), you can follow these steps:

sudo strings /var/cache/nscd/hosts | uniq

This command will display various statistics and information related to nscd, including the cache size, cache hits, and cache misses.

sudo nscd --statistics

Use dnsmasq command display the DNS Cache

To display the DNS cache generated by dnsmasq, you can use the following command:

sudo dnsmasq --test --cache

Linux flush DNS cache

To view and flush the DNS cache on Linux, you can follow the steps below:

1. Open a terminal window. You can do this by pressing Ctrl+Alt+T on most Linux distributions.

2. To view the current contents of the DNS cache, use the following command:

sudo systemd-resolve --statistics

This command will display various statistics related to the DNS resolver, including the cache size, cache hits, and cache misses.

3. To flush the DNS cache, you need to restart the DNS resolver service. The method depends on your Linux distribution.

For Ubuntu 16.04 and later, Fedora, and CentOS 7, and later. You can use the following command:

sudo systemctl restart systemd-resolved.service

For Ubuntu 14.04 and earlier, CentOS 6 and earlier. you can use the following command:

sudo /etc/init.d/nscd restart

After executing the appropriate command, the DNS cache will be flushed, and any previously cached DNS entries will be cleared.


Linux flush DNS cache may temporarily affect domain name resolution on your system until the cache is repopulated with fresh DNS information

I hope this will be helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

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