How to View and Flush DNS Cache on Linux
When you access a website using its domain name, your system sends a request to a DNS server to get the IP address for that domain. This domain-IP address pair is saved in the DNS cache for later use so you don’t have to send requests to the DNS server every time to make a connection.
Sign up forfree
Forgot your password?
Create an account
*Required: 8 chars, 1 capital letter, 1 number
By continuing, you agree to thePrivacy PolicyandTerms of Use.You also agree to receive our newsletters, you can opt-out any time.

Why Flush DNS Cache on Linux?
There are several reasons why you might want to rebuild the DNS cache stored on your system. Maybe you have an outdated DNS record for a website and want to re-fetch it from the DNS server. Or perhaps, your system’s been compromised and you want to make sure the DNS cache hasn’t been tampered with, alsoknown as DNS spoofing.
When you flush your DNS cache, the system has to ping the DNS server again and get the new domain-IP address record from it, removing any outdated or compromised data in the process.

How to View the Local DNS Cache on Linux
Before systemd, most Linux distributions didn’t have system-wide DNS caching, 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 the DNS entries.
The following sections will guide you on how to view the DNS cache contents generated by systemd-resolved, nscd, and dnsmasq, so you can understand the cached data before you decide to flush it.

View DNS Cache for systemd-resolved
To view systemd-resolved cache records, you need to first temporarily kill the service, then export its logs to a file.
Begin by sending a SIGUSR1 signal to kill the systemd-resolved service:

Usethe journalctl commandand the standard output operator to save the output to a text file:
You can then view the contents of the file using a text editor like Vim:

In the file, search for “CACHE:” by pressingEscape, typing “/CACHE:”, and hittingEnter. All the DNS records listed underCACHE:are a part of the local DNS cache. Pressnto jump to the next set of DNS entries if you’re using Vim.
View the Local DNS Cache for nscd
To view the local cache generated by nscd, you need to read the contents of the nscd hosts database using the strings command.
On Debian and Ubuntu-based distros, this file is located at/var/cache/nscd/hosts. Run the following command to view the file:
To view general statistics about the nscd DNS cache, use the-gflag:
Display the DNS Cache Generated by dnsmasq
dnsmasq stores the DNS cache in memory so it’s not easy to get the exact records. But you’re able to send a kill signal to dnsmasq and log its output to get the number of DNS queries processed.
To do so, first, make sure dnsmasq is up and running byusing the systemctl command:
If the status showsActive, run the following command to kill the service:
Using the journalctl command, extract the dnsmasq logs and save them to a text file:
Finally, view the contents of the fileusing a file-viewing utilitylike cat or less:
How to Flush DNS Cache on Linux
Flushing the DNS cache means removing the cached DNS records from your computer. This forces it to send a request to the DNS server to retrieve the new DNS entries.
Here’s how you can flush the DNS cache on Linux:
Using systemd-resolved
You can use the resolvectl command to flush the DNS cache stored by systemd-resolved:
If you’re running Ubuntu 17.04 or 18.04, use the systemd-resolved command to flush the cache:
Flush the nscd DNS Cache on Linux
The most convenient way to remove the DNS cache for nscd is by simply restarting the service. You can do so by running:
If that doesn’t work, first, check if the local cache stored on your PC is persistent. You can use the-gflag to verify it:
If that’s the case, use the-iflag with the nscd command to clear the records (istands forinvalidate):
Remove the dnsmasq DNS Cache
Flushing the DNS cache generated by dnsmasq is simple. Since the cache is stored in memory, restarting the service deletes all the stored entries.
To restart dnsmasq, run the following systemctl command:
Or, issue the following command:
Enter the administrator password if prompted. dnsmasq will now restart and all your existing DNS entries will be removed from the cache.
After flushing the DNS cache, it’s best to view the local cache entries and verify if the data was successfully removed. You can use dig, one of themany network troubleshooting Linux commands, and check the Query time value in the output. If it’s more than 0 msec, the cache was successfully cleared (0 msec means the domain record is still in the cache).
Clear Google Chrome’s DNS Cache
The web browser you frequently use also caches the DNS records. When you enter a URL, the system searches for a cache entry in the local browser cache. If not found, it checks for the records in the local system cache. It’s crucial to clear your web browser’s DNS cache since it has higher precedence than the system-wide cache.
For demonstration, let’s flush the DNS cache in Google Chrome. There are ways to do it on other browsers too, so it’s best that you google how to do it for the browser you use.
To begin, type “chrome://net-internals/#dns” in the URL bar and hitEnter:
ClickClear host cacheto clear the DNS entries stored in Google Chrome.
Linux Is the Best OS to Learn Networking
Linux might seem complicated at first, but if you invest some time into learning how it works, you’ll quickly realize it’s fantastic, and maybe even better than Windows or macOS.
Most online servers run Linux, and it’s one of the reasons why Linux is ideal if you want to learn networking, or wish to know how computers work, in general.
With every web service running on a server, it’s now essential to master computer networking. Here are some reasons to learn networking on Linux.
Don’t let aging hardware force you into buying expensive upgrades.
Anyone with more than a passing interest in motorsports must see these films.
This small feature makes a massive difference.
My iPhone does it all, but I still need my dumb phone.
Now, I actually finish the books I start.