How to Flush DNS Cache on Ubuntu 20.04

If you are using Ubuntu for a long time, the chances of having a tremendous amount of cache stored on your Ubuntu Machine are pretty much high. By flushing the DNS cache on Ubuntu Machine 20.04, you can free up storage plus make your system a bit faster too.

What is DNS Cache?

To understand DNS cache, you will first have to understand what is DNS. DNS is Domain Name Server. They contain the mapping of domain names to IP addresses. Let me make it a bit simple to you by an example.

When you want to visit Facebook using a browser, you will type www.facebook.com in your browser’s Address bar. Here comes the working of DNS, it will get the IP address of Facebook by your given URL and land you to their site.

How to Flush DNS on Ubuntu

Ubuntu Machine also maintains the local database which contains domain to IP address mapping which we recently visited and more often visit too. This database is known as DNS cache which is used to speed up our browsing experience by storing frequently visited websites.

It is mainly used to avoid the fetching process to speed up things.

Why You Should Flush DNS Cache on Ubuntu?

Sometimes, there are cases of cache being corrupted and you are being landed on the wrong website because it heavily relies on the local cache.

For example, you might have landed on an old website like ours while the administrator has moved his new website to another IP but due to your DNS having stored old info you won’t reach a new website.

After clearing the DNS cache, the new entries will be made on your local database and you will be landed on the original and newly shifted address of your required website.

Flushing DNS Cache on Ubuntu 20.04

We will walk you through several methods by which you can flush the DNS cache and overcome all the problems that you are currently facing because of the DNS local database.

sudo killall -USR1

Method 1 – Flush DNS Using Systemd

This method uses a systemd utility for flushing DNS cache from our Ubuntu Machine. Follow the given steps:

 sudo systemctl is-active systemd-resolved 

By using this command, we are checking the status of the systemd-resolved. If you get an output of active in a terminal, it means you are good to go.

sudo systemctl is-active systemd-resolved

 sudo systemd-resolve –statistics 

This command will get you the current size of your DNS cache. You have to focus on the Current Cache Size which has indicated 80 in my system.

sudo systemd-resolve --statistics

 sudo systemd-resolve –flush-caches 

This command will not get you any output rather than just asking for your password. It has flushed DNS cache and we will see the result by the next command.

sudo systemd-resolve --flush-caches

 sudo systemd-resolve –statistics 

We have used this command again to show statistics and in my system, it is zero.

final result

Note: You may have to use sudo systemd-resolve –flush-caches this command 2-3 times to get your cache count close to zero.

Method 2 – Flushing DNS Using Signals

You can clear the DNS cache by sending “USR2” signals to systemd-resolved. Follow this given command to clear your DNS cache:

 sudo killall -USR2 systemd-resolved 

sudo killall -USR2 systemd-resolved

 sudo killall -USR1 systemd-resolved 

sudo killall -USR1

To check whether the cache was cleared or not, we will send “USR1” signals to systemd-resolved. It will fetch the current state of a system in the systemd journal.

 sudo journalctl -r -u systemd-resolved 

We will get an output of our journal in a readable form.

sudo journalctl -r -u systemd-resolved

Congratulations, you have successfully cleared the cache of DNS on your Ubuntu Linux system. That said, here are a few questions that are often asked by Ubuntu users related to DNS.

FAQs on Flushing DNS

Here are some frequently asked questions on DNS when it comes to clearing it on Ubuntu LTS!

Where does DNS information get stored in Linux?

Linux DNS configuration is stored in a file at /etc/resolv.conf. Each program or service that is managing DNS has its configuration file such as /etc/dnsmasq.conf (For dnsmsq service).

What is the Nscd service in Linux?

Nscd is a daemon that provides a cache for the common name services that includes providing cache access for passwd(5), group(5) through standard interfaces such as getpwnam(3), gethostbyname(3), and others.

Is it safe to flush DNS, and what does it exactly do?

Yes, it is completely safe to flush DNS. All Linux machines have their DNS local database and by flushing it, we are deleting old entries that our system has made. It can be helpful to overcome DNS-related issues.

Did You Successfully Flush DNS Cache?

Flushing DNS cache can help you to overcome the issues that you are facing such as being landed on the wrong website due to a corrupted DNS database or being landed on the old website from which the admin has moved for so long. Hope you’ve fixed it by flushing the DNS cache on your Ubuntu machine by using our detailed guide. 

Default image
Sagar Sharma

Sagar always uses Linux to its core and loves to write the technical side of system administration! While he's not writing, you can always find him exploring new Linux distros!

Articles: 20