This quick start guide describes all the possible ways to clear the DNS cache on Linux operating systems. For anyone wondering, the DNS cache (or sometimes referred to as the DNS resolver cache) is a small temporary database maintained by operating systems. It stores information about all websites visited and other domains on the Internet. In other words, the DNS cache contains the latest DNS queries. When you visit the same website multiple times, the OS pulls the DNS records of that website from the local DNS cache database, not from the actual public DNS server. Hence, DNS caching improves website load times and reduces the bandwidth / CPU consumption of the original DNS server.
Why flush or delete the DNS cache?
There are many reasons for flushing the local DNS cache.
For example, you might have configured your website or changed the DNS settings for your domain, or you are having problems connecting to the Internet.
In such cases, if you try to visit the site, your OS will most likely get a cached version of the site from your local DNS cache.
So flushing the local DNS cache will delete all records from the local DNS cache database and force your system to re-populate the DNS cache with new records from the real DNS server and display a fresh copy of the website.
How to flush DNS cache on Linux
By default, DNS caching is not installed and / or enabled at the Linux operating system level.
Instead, applications such as web browsers maintain their own internal cache.
So you can simply clear your browser cache or restart it to fix internet connection issues, fix DNS spoofing issues (like poisoning your DNS cache), and fix outdated content issues.
If your Linux distribution does use any DNS caching services, you need to manually flush them as described below.
There are several ways to clear the DNS cache.
It depends on the Linux distribution and the DNS caching service you are using.
Here I have included instructions for flushing the most commonly used DNS caching services on Linux.
1. DNS caching service Systemd-resolve.
Systemd-resolve is a systemd service that provides a system-level DNS caching service for local applications.
It is part of the systemd package that is installed by default.
This service is enabled by default on most modern Linux operating systems such as Fedora and Ubuntu.
First, let’s check if the systemd-resolved service is active using the command:
$ sudo systemctl is-active systemd-resolved.service
If systemd-resolved service is enabled, the output will display “active”. If it is inactive, the system will display “inactive“.
The systemd-resolved service is active on my system.
Let’s see the statistics of the systemd-resolve caching service using the command:
$ sudo systemd-resolve --statistics
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
As you can see, the above output shows the total of the current cache size, hits, and missed caches.
To clear the systemd-resolve cache, run:
$ sudo systemd-resolve --flush-caches
If successful, you will not see any output.
Now check the systemd-resolve statistics to see if the local DNS cache has been cleared.
$ sudo systemd-resolve --statistics
Now you will see that the current cache size is 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
2. Service of caching DNS Nscd.
Nscd, short for name service cache daemon is a caching daemon that provides a cache for the most common naming service requests.
It is the preferred caching service for most Redhat based systems.
If your Linux system is using the nscd caching service, you can clear the local DNS caching service by simply restarting the nscd service as shown below:
$ sudo systemctl restart nscd.service
$ sudo service nscd restart
$ sudo /etc/init.d/nscd restart
3. Bind / Named DNS caching service.
Berkeley Internet Name Domain (abbreviated as BIND) is the most popular open source DNS server in use today.
If you are using BIND to cache DNS queries, use the following commands to flush / reset the BIND caching service on Linux:
$ sudo systemctl restart named
$ sudo service named reload
$ sudo /etc/init.d/named restart
$ sudo rndc reload
$ sudo rndc exec
The latest version of BID also allows you to clear the DNS cache for a specific domain. The following command clears the DNS cache for the itsecforu.ru domain:
$ sudo rndc flushname itsecforu.ru
You can also clear the DNS cache for the entire LAN and WAN area as shown below:
$ sudo rndc flush lan
$ sudo rndc flush wan
4. DNS caching service Dnsmasq.
Dnsmasq is a lightweight DHCP server and caching DNS server with PXE and TFTP server support.
It is suitable for resource-constrained routers and firewalls to manage small networks.
If you are using dnsmasq as your caching service, you can clear your local DNS caching service as shown below:
$ sudo systemctl restart dnsmasq
Another way to clear the local DNS cache in Ubuntu and other Ubuntu flavors like Linux Mint is to run the following command:
$ sudo /etc/init.d/dns-clean restart