Thứ Tư, Tháng Hai 12, 2025
spot_img
HomeTechnologyHow to clear DNS cache on Linux

How to clear DNS cache on Linux

When you visit a website using that website's domain name, your system sends a request to the DNS server to get the IP address for that domain. This IP-domain address pair is stored in the DNS cache for later use, so you don't have to send a request to the DNS server every time to make a connection.

But sometimes, the local DNS cache gets corrupted and causes HTTP errors. Fortunately, clearing and rebuilding the DNS cache on a Linux computer is very simple. Here's how.

Why clear DNS cache on Linux?

There are a number of reasons why you may want to rebuild the DNS cache stored on your system. Maybe you have outdated DNS records for a website and want to get them back from the DNS server. Or maybe your system has been compromised and you want to ensure that your DNS cache has not been tampered with, also known as DNS spoofing.

When you clear the DNS cache, the system must re-ping the DNS server and obtain a new IP-domain address record from there, deleting any outdated or compromised data in the process.

How to view local DNS cache on Linux

Before systemd, most Linux distributions did not have a system-wide DNS cache, unless a program like dnsmasq or nscd was set up manually. systemd comes with systemd-resolved, a service that resolves domain names to IP addresses and caches DNS entries.

The following sections will show you how to view the DNS cache content generated by systemd-resolved, nscd, and dnsmasq, so you can understand the data stored before deciding to delete it.

See DNS cache for systemd-resolved

To view systemd-resolved cache logs, you first need to temporarily disable the service, then export that service's logs to a file.

Xem thêm  How to take screenshots on Android when the app doesn't allow it

Start by sending the SIGUSR1 signal to disable 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 > ~/cache.txt

You can then view the file's contents using a text editor like Vim:

vim ~/cache.txt
See systemd-resolved DNS cache
See systemd-resolved DNS cache

In the file, search for “CACHE:” by pressing Escapeenter “/CACHE:” and press Enter. All DNS records listed in CACHE: Are part of the local DNS cache. Press n to jump to the next set of DNS entries if you're using Vim.

View local DNS cache for nscd

To view the local cache created by nscd, you need to read the contents of the nscd server database using the strings command.

On Debian and Ubuntu-based distributions, this file is located at /var/cache/nscd/hosts. Run the following command to view the file:

sudo strings /var/cache/nscd/hosts | uniq
View nscd local DNS cache contents
View nscd local DNS cache contents

To see general statistics about the nscd DNS cache, use the -g flag:

sudo nscd -g

Display DNS cache generated by dnsmasq

dnsmasq caches DNS in memory so it's not easy to get accurate records. But you can send a kill signal to dnsmasq and log its output to know the number of DNS queries processed.

To do this, first make sure dnsmasq is working by using the systemctl command:

sudo systemctl status dnsmasq

If the status is displayed Activerun the following command to turn off the service:

sudo pkill -USR1 dnsmasq

Using the journalctl command, extract the dnsmasq logs and save them to a text file:

sudo journalctl -u dnsmasq > ~/cache.txt

Finally, let's view the file's contents using a file viewer utility like cat or less:

cat ~/cache.txt

How to clear DNS cache on Linux

Clearing DNS cache means deleting cached DNS records from your computer. This forces it to send a request to the DNS server to retrieve new DNS entries.

Xem thêm  6 ways to permanently delete files on Windows

Here is how you can clear DNS cache on Linux:

Use systemd-resolved

You can use the Resolvectl command to clear the DNS cache stored by systemd-resolved:

sudo resolvectl flush-caches

If you are running Ubuntu 17.04 or 18.04, use the systemd-resolved command to clear the cache:

sudo systemd-resolved --flush-caches

Delete nscd DNS cache on Linux

The most convenient way to clear DNS cache for nscd is to restart the service. You can do so by running:

sudo /etc/init.d/nscd restart

If that doesn't work, first check whether the local cache stored on your PC is persistent or not. You can use flags -g to verify it:

sudo nscd -g

If so, use flag -i with nscd command to delete records (i stands for invalidate):

sudo nscd -i hosts

Clear dnsmasq DNS cache

Clearing the DNS cache created by dnsmasq is very simple. Because the cache is stored in memory, restarting the service will delete all stored items.

To restart dnsmasq, run the following systemctl command:

sudo systemctl restart dnsmasq

Or, issue the following command:

service dnsmasq restart

Enter the admin password if prompted. dnsmasq will now restart and all existing DNS entries will be removed from cache.

After clearing the DNS cache, it is best to view the entries in the local cache and verify whether the data has been successfully cleared. You can use dig, one of many network troubleshooting Linux commands, and check the query time value in the output. If it is greater than 0 milliseconds, the cache has been deleted successfully (0 milliseconds means the domain record is still in the cache).

dig google.com

Delete Google Chrome's DNS cache

The web browser you often use also caches DNS records. When you enter a URL, the system searches the local browser cache for the cached entry. If not found, the system will check the records in the local system cache. Clearing your web browser's DNS cache is very important because it has a higher priority than the system-wide cache.

Xem thêm  How to fix AirDrop not working error

To illustrate, the article will delete DNS cache in Google Chrome. There are ways to do it on other browsers too, so it's best to Google how to do it for the browser you use.

To get started, enter “chrome://net-internals/#dns” go to the URL bar and press Enter:

Delete Google Chrome's DNS cache
Delete Google Chrome's DNS cache

Click Clear host cache to delete DNS entries stored in Google Chrome.

At first glance, Linux seems complicated, but if you take the time to learn how it works, you will quickly realize that it is a great operating system and can even be better than Windows. or macOS.

Most online servers run Linux, and this is one of the reasons why Linux is the ideal operating system if you want to learn about networking or want to know how computers work in general.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments