You can flush the DNS cache on a systemd-based Linux computer with the “resolvectl flush-caches” command. If you are using dnsmasq, you can clear DNS using “sudo killall -HUP dnsmasq” instead.
Is your internet browsing experience slow on your Linux device, or are the websites you visit outdated or the wrong website? Let’s discuss flushing DNS cache in Linux and how to know if you really need it.
The domain name service is the bit of magic that converts names into numbers. It takes device network names and website names and looks up their IP addresses. The network can then use the IP address to properly route traffic to those devices or sites.
This research, known as requests, does not happen instantly. There is a small finite period of time involved. Internet DNS queries may require querying precursor DNS servers, root name servers, top-level domain servers, and authoritative name servers. DNS queries are fast, but to make them even faster, responses to recent DNS queries are cached on DNS precursor servers.
If the response to a DNS query is in the cache of the precursor server, no other server needs to be contacted. The response is returned from the cache of the precursor server. Similarly, a small cache is maintained by your broadband router at home. If you request a local network device using its network device name, your router provides the IP address. It can also cache responses it has received from external DNS servers.
Typically, Linux networks and computers are configured to use external DNS services, either provided by your ISP or a free service such as OpenDNS Where Google-DNS. There are good reasons why some people use their own DNS server, but most of us don’t. However, your Linux computer, even if it is not running a DNS server, may still cache DNS query results.
The problem with using cached data is that everything relies on the assumption that none of the cached details have changed since they were cached. If the details have changed, the information you receive will be out of date.
If a cache entry or the entire cache is corrupted, you will get poor performance at best and security vulnerabilities at worst. This is when you’ll want to look into “flushing” or clearing the DNS cache.
To determine if your Linux computer caches DNS queries, use the
is-active option of the
systemctl ordered. The daemon that manages the DNS cache is the systemd network name resolution handler, known as
systemctl is-active systemd-resolved
If the response is “active”, DNS caching is in progress. If the answer is “inactive”, it is not. On this particular computer it is active. We can use the
resolvectl command with the statistics option to see how many records are in the cache.
We can see that there are 330 entries in this computer’s DNS cache.
Examining your DNS cache
Examining DNS cache entries is not a prerequisite for flushing the cache, and if you have no interest in doing so, you can skip this whole step. Sometimes, however, it can be informative. You may see garbled entries that indicate corruption, or you may see error messages related to device addressing issues on your network.
Now, there is no easy way to see these entries. We can do it, but we have to be a little creative.
USR1Where user-defined number one signalis a signal which can be sent by
killall orders. This signal has no predefined meaning. Applications are free to ignore this signal or react in any way the developers have implemented.
systemd-resolved demon reacts to
USR1 by writing its cache to the system logs. You can then use the
journalctl command to filter DNS entries.
We will use the
killall command with
USR1 To send the signal to the
systemd-resolved Devil. Note that even though we use the
killall order, the
systemd-resolved daemon continues to run. We are not sending a termination signal.
sudo killall -USR1 systemd-resolved
We will now use the
journalctl order with the
-u (filter by
systemd unit) option to extract log entries that were generated by
systemd-resolved . We will redirect this output to a text file called “dns.txt”.
sudo journalctl -u systemd-resolved > dns.txt
We will use the
less file viewer to display the contents of the file.
You will be able to find cached mappings between domain names and IP addresses by scrolling and searching through the text.
We can see an entry for Google which has an IP address of 188.8.131.52. You can check this by putting the IP address in a web browser. You should see the Google search home page.
Flushing the cache removes all entries and restarts the collection process. If there are, it forcibly removes bad and corrupt entries from the cache.
Ordering is simple; we use
resolvectl with the
We silently return to the command line. To confirm that something really happened, we will check the DNS cache statistics again.
We can see that the cache size has dropped to zero. It will increase over time as it accumulates new entries.
dnsmasq The app provides DNS cache and DHCP server. It is popular with users who want to run their own DNS server, especially on non-systematized installations.
dnsmasq DNS caching is easy. We have to send the
SIGHUP signal that indicates to the
dnsmasq daemon to effectively reset. This clears its DNS cache. To send the signal, we use the
killall order with the
-HUP flag and application name.
sudo killall -HUP dnsmasq
Of course, if your computer doesn’t cache at all, you don’t have to check anything.
If it caches DNS queries but everything works fine, you can also ignore it. But if you’re experiencing slow or sporadic webpage updates while browsing the web, or if you’re seeing the wrong webpages, it’s probably a good time to flush your DNS cache.