The Ultimate Guide to Flushing DNS in Linux for Optimal Performance

The Ultimate Guide to Flushing DNS in Linux for Optimal Performance

Learn how to clear DNS caches on Linux! Discover what DNS caches are and find out if your computer is using one Review your DNS cache and follow our step-by-step guide to clear the cache and ensure smooth browsing Master the process with ease and efficiency

Key Takeaways

To clear the DNS cache on a Linux computer running on the systemd system, simply execute the command "resolvectl flush-caches". In case you are utilizing dnsmasq, you can instead use the command "sudo killall -HUP dnsmasq" to clear the DNS cache.

Is your Linux device causing slow internet browsing or leading you to outdated or incorrect websites? Let's delve into the process of flushing the DNS cache on Linux and determine if it's necessary.

What Are DNS Caches?

The domain name service performs a crucial function by transforming names into numerical values. It retrieves IP addresses for device network names and website names, enabling the network to efficiently direct traffic to the intended devices or sites.

These name look-ups, referred to as requests, do not occur instantly. They typically involve a brief, finite duration. Internet DNS requests may involve querying precursor DNS servers, root name servers, top-level domain servers, and authoritative name servers. While DNS requests are already swift, the caching of recent DNS request responses on the DNS precursor servers further enhances their speed.

When a DNS request can be answered by the precursor server's cache, no other servers need to be contacted and the answer is retrieved from the cache. Similarly, your broadband router at home maintains a small cache and can provide the IP address of a local network device if requested by its network device name. It can also cache responses from external DNS servers.

Typically, networks and Linux computers are configured to use external DNS services provided by an Internet Service Provider or services like OpenDNS or Google DNS. While some people choose to run their own DNS server, most of us do not. However, even if your Linux computer is not running a DNS server, it can still cache DNS request results.

Using cached data can be problematic because it relies on the assumption that none of the cached details have been altered since they were initially cached. If any changes have occurred, the information you obtain will be outdated.

In more severe cases, if a cache entry or the entire cache becomes corrupted, you will experience unreliable performance at best and potential security vulnerabilities at worst. This is when you should consider "flushing" or clearing the DNS cache.

Is Your Computer Using a Local DNS Cache?

The local DNS caches were active on certain test computers while disabled on others. On our Manjaro 21 computer, it was disabled, whereas it was enabled by default on Fedora 37 and Ubuntu 22.10.

To check if your Linux computer is caching DNS requests, you can utilize the is-active option in the systemctl command. The systemd network name resolution manager, known as systemd-resolved, is responsible for managing the DNS cache.

The Ultimate Guide to Flushing DNS in Linux for Optimal Performance

If the DNS caching is working properly, the response will be labeled as "active". In the case where it is not functioning, the response will be labeled as "inactive". On the current computer, DNS caching is indeed active. To determine the number of records stored in the cache, we can utilize the resolvectl command along with the statistics option.

resolvectl statistics

The Ultimate Guide to Flushing DNS in Linux for Optimal Performance

We can see there are 330 entries in the DNS cache of this computer.

Reviewing Your DNS Cache

Reviewing the DNS cache entries is not necessary before flushing the cache. If you're not interested in doing so, you can simply skip this step. However, it can sometimes be helpful to check the entries. You may come across scrambled entries that indicate corruption, or you might encounter error messages related to device addressing problems on your network.

Unfortunately, there is no direct method to view these entries. However, we can employ a creative approach. USR1, also known as user-defined signal number one, is a signal that can be sent through the kill and killall commands. Unlike other signals, USR1 does not have a predefined meaning. It is up to individual applications to decide how they respond to this signal, as per the developers' implementation.

The systemd-resolved daemon writes its cache to the system logs in response to USR1 signal. To filter out the DNS entries, we can utilize the journalctl command.

To send the signal to the systemd-resolved daemon, we will employ the killall command with USR1. It is important to note that despite using the killall command, the systemd-resolved daemon will still remain active. The signal we are transmitting is not for terminating the daemon.

sudo killall -USR1 systemd-resolved

The Ultimate Guide to Flushing DNS in Linux for Optimal Performance

To extract the log entries generated by systemd-resolved, execute the following command using journalctl with the -u option. The output will be redirected to a text file named "dns.txt."

sudo journalctl -u systemd-resolved > dns.txt

The Ultimate Guide to Flushing DNS in Linux for Optimal Performance

We'll use the less file viewer to view the contents of the file.

less dns.txt

The Ultimate Guide to Flushing DNS in Linux for Optimal Performance

You'll be able to find the cached mappings between domain names and IP addresses by scrolling and searching through the text.

The Ultimate Guide to Flushing DNS in Linux for Optimal Performance

How to Clear the DNS Cache on Linux

An IP address of 216.58.212.196 is associated with the entry for Google. To confirm this, enter the IP address in a web browser. It will redirect you to the home page of Google search.

Flushing the cache deletes all entries and initiates the collection process again. If present, this forcibly eliminates inaccurate and damaged entries from the cache.

The flush-caches option in resolvectl is employed to execute the command.

resolvectl flush-caches

The Ultimate Guide to Flushing DNS in Linux for Optimal Performance

We're silently returned to the command line. To confirm that something has actually happened, we'll check the DNS cache statistics again.

resolvectl statistics

The Ultimate Guide to Flushing DNS in Linux for Optimal Performance

We can see that the cache size is dropped to zero. It will increase over time as it accumulates new entries.

How to Flush the dnsmasq Cache on Linux

The dnsmasq application provides a DNS cache and a DHCP server. It is popular with users who want to run their own DNS server, especially on non-systemd installations.

To clear the DNS cache of dnsmasq, simply send the SIGHUP signal to the dnsmasq daemon using the killall command with the -HUP flag followed by the application name.

Example: sudo killall -HUP dnsmasq.

The Ultimate Guide to Flushing DNS in Linux for Optimal Performance

Flushed, With Success

If your computer is not caching at all, there is no need to check anything.

If DNS requests are being cached and everything is functioning properly, you can disregard it. However, if you are encountering sluggish or inconsistent webpage updates while browsing the web, or if you are being directed to incorrect webpages, it is advisable to clear your DNS cache.

Linux Commands

Files

tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld