Linux flush DNS cache

Introduction

In this blog post, we’ll cover how to view and flush the DNS cache on Linux. Linux flush DNS cache can help resolve HTTP errors and safeguard against DNS spoofing attacks. Follow along to learn the steps for managing your DNS cache effectively.

DNS Cache on Linux?

DNS Cache on Linux refers to the stored records of DNS lookups that the system keeps locally. These records contain information about previously resolved domain names and their corresponding IP addresses. By caching this information, Linux can speed up subsequent DNS queries, reducing the time required to resolve domain names.

Flushing the DNS cache on Linux clears this stored information, forcing the system to perform new DNS lookups for subsequent queries. This can be useful for troubleshooting DNS-related issues or ensuring that the system retrieves the most up-to-date information from DNS servers.

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

To view the local DNS cache on Linux, the method varies depending on the DNS resolver software in use.

  • Viewing the local DNS cache on Linux varies based on the DNS resolver software.
  • For systemd-resolved users, employ systemd-resolve –statistics to check cache usage.
  • Alternatively, dnsmasq users can use dnsmasq –test or sudo rndc dumpdb -cache.
  • NetworkManager users can check cache status with nmcli dev show | grep DNS.
  • Familiarity with these methods aids in monitoring and troubleshooting DNS resolution for optimal system performance.

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 provides a comprehensive overview of nscd command, presenting statistics such as cache size, cache hits, and cache misses, offering valuable insights into the performance and operation of the Name Service Cache Daemon.

sudo nscd --statistics

Use dnsmasq command display the DNS Cache

To display the DNS cache generated by dnsmasq command , 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.

Conclusion

Linux flush DNS cache can temporarily disrupt domain name resolution on your system, as it clears existing DNS data. However, this process ensures that your system fetches updated DNS information, enhancing accuracy and security in the long run. 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 DevopsRoles.com 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.