How to Flush Local DNS Cache on Linux 1

How to Flush Local DNS Cache on Linux

Figuring out how to flush local DNS cache on Linux is essential to clear out security and connectivity issues. We’re here to help you understand all of it.  

DNS is the internet’s phonebook, which allows you to map the domain name to an IP (internet protocol) address. There can be situations where your DNS cache is corrupted, and you would need to flush the local DNS cache.

This article discusses various steps for flushing the local DNS cache. We start with a brief introduction to the DNS cache, the reasons for flushing the local DNS cache, and the multiple approaches to flushing it. 

What is a DNS cache?

The domain name service (DNS) cache is a local store for DNS lookup information on your system. This includes information about recently used websites and internet domains. For instance, whenever you visit a website, the operating system (OS) retrieves this information from your local cache instead of consulting the DNS server on every occasion. This enables faster loading of websites. 

Why should you flush local DNS cache in Linux?

There may be several reasons for flushing the local DNS cache. The following are the possible reasons:

  • Your Linux workstation or server has networking issues
  • The websites on your system may not be loading correctly
  • Website information has been changed, but your browser keeps on loading the old version of the website

We will now discuss the process of flushing your local DNS cache. To perform the steps mentioned in this article, you must have a Linux distribution with a root account or an account with sudo privileges.

How to Flush Local DNS Cache on Linux using systemd-resolved

This is the easiest way to flush local DNS using the systemd-resolved service. Follow the steps below to flush the local DNS cache.

Check if the systemd-resolved service is running

The very first step is to check if the systemd-resolved service is running. Type the following command on your terminal:

$sudo systemctl is-active systemd-resolved


The above will check if the systemd-resolved service is active or not. You should see the following output:

active


Flushing the DNS cache

If everything is fine, we will now flush the local DNS cache.

The basic syntax used here is the system-resolved command followed by the --flush-caches option. Open your terminal and type the following command:

$ sudo systemd-resolve --flush-caches


Checking the status of the DNS cache

After flushing the cache, you can verify the status of the DNS cache by using the -statistics option. This will show you the current cache size in the cache section. For this purpose, type the following command on your terminal:

$ sudo systemd-resolve --statistics

You should see that the current cache size is 0. 

Flushing the local DNS using the resolvectl command

If you are using a Red Hat distribution, the systemd-resolved command may not work. In such a situation, instead of using the systemd-resolved command, you can also use the resolvectl command along with the flushcaches option. Follow the steps below to flush the local DNS cache.

Flush local DNS cache

You can use the resolvectl command to flush the local DNS cache as follows:

$ sudo resolvectl flush-caches


There are other alternate commands that you may try. For instance, the following commands can also be used to flush the local DNS cache:

$sudo systemctl restart nscd.service


Also, you can try the following command:

$sudo systemctl restart named


Note: The systemd-resovled is just a symbolic link to the resolvectl command.

Checking the status of the local DNS cache using resolvectl

After flushing the DNS, you can check the status of the local DNS cache using the following command:

$resolvectl statistics | grep -i cache

Flush the DNS cache using signals

Besides using the systemd-resolved command directly, DNS can be flushed using signals. Follow the steps below to flush the DNS cache using signals.

Sending the signal to the systemd-resolved service

The basic approach is to send the USR2 signal to the systemd-resolved service. This will be the instruction to the systemd to flush DNS entries. For this purpose, type the following command on the Terminal:

$ sudo killall -USR2 systemd-resolved


Checking the status

After running the above command, you can then check if the DNS cache has been flushed properly. For this purpose, you can the USR1 signal to the systemd-resolved service. Type the following command on your terminal:

$ sudo journalctl -r -u systemd-resolved


This command will dump the current state into the systemd journal. 

Flush DNS cache using dnsmasq

Another approach to flush the DNS cache is using the dnsmasq command. Follow the steps below to flush the DNS cache.

Send Signal to dnsmasq

The process is to send a SIGHUP signal to the dnsmasq process along with the killall command as follows:

$ sudo killall -HUP dnsmasq

Checking the statistics

Now to check if the DNS cache entries have been flushed, you can send a signal USR1 to the process similar to the systemd-resolved service. This will print the statistics to the syslog file. Here, you can use the tail command to verify that the DNS cache has been flushed.

In this article, we have discussed the basic steps to flush the local DNS cache. We discussed the basic reasons for flushing the DNS and the various ways to flush the local DNS cache.

If this guide helped you, please share it.

Leave a Reply
Related Posts