Flush DNS Cache on Ubuntu 20.04 - Step by Step process ?

DNS server is used to convert domain names to IP addresses.

When you type a website name in to address bar of your browser, your system does not know where to find this address. It asks for this address from the DNS server. 

The DNS server contains the mapping of the domain name to IP addresses.

This domain name to IP address mapping changes from time to time.

Our system also maintains a temporary database containing domain to IP mapping information about the sites we visited recently. 

This temporary database is known as DNS cache. It is used to avoid fetching the frequently or recently visited sites more quickly from the locally stores DNS cache instead of retrieving them from DNS servers.

Sometimes, it is required to flush the DNS cache. This is required in cases like when the cache becomes corrupted, and it is redirecting you towards the wrong websites. 

It is also needed when a site is moved to a different address but you the browser is still loading the old site. 

Flushing the DNS cache deletes the entire domain to IP mapping entries including old and invalid records. The next time when the system tries to access the websites, DNS cache repopulates the new entries.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform DNS configurations and related queries.

In this context, we shall look into how to flush the DNS cache on Ubuntu.


How to Flush the DNS Cache on Ubuntu 20.04 LTS ?

Most Ubuntu (18.04 and later) and Debian systems use systemd-resolved daemon to cache DNS queries. We can also use it to flush the DNS cache.

You can perform the following tasks.


1. Check "systemd-resolved" Status

Let's first check if systemd-resolved is running. Execute the below command to do so:

$ sudo systemctl is-active systemd-resolved

If you see "active" in the above command's output, it means systemd-resolved is running on your system.


2. View Current Cache Size

Now if you want to view the current cache size along with some other information, execute the below command in Terminal:

$ sudo systemd-resolve --statistics

Following is the output of the above command, which shows different information. 

At present, we are only concerned with the "Current Cache Size", which is almost full. We want to make the cache size 0 by flushing it.


3. Flush DNS Cache

Now in order to flush the DNS cache, execute the below command in Terminal:

$ sudo systemd-resolve --flush-caches


4. Verify the Cache Size

You can then look up the statistics to ensure that the size of your cache is now zero:

$ sudo systemd-resolve --statistics

The output will show the current cache size as "0" which confirms that the DNS cache has been cleared.


[Need urgent assistance in fixing Security Firewall on Ubuntu Linux Servers? We are available to help you. ]

This article covers how to flush the DNS cache on Ubuntu. If the sites are not loading properly or you are receiving outdated sites, try to flush the DNS cache to see if the issue resolves. 

Remember, after the DNS cache is flushed, sites will first load slightly slower. 

But once the DNS cache is re-populated, sites will be loaded faster.


Facts about flushing DNS cache on Ubuntu:

1. DNS cache is a temporary database that stores information about previous DNS lookups. 

2. Whenever you visit a website, your OS and web browser will keep a record for the domain and the corresponding IP address. 

3. Flushing DNS cache eliminates the need for repetitive queries to the remote DNS servers and allows your OS or browser to resolve the website's URL quickly.


How to use dns-clean to flush DNS-Cache ?

You can clear the DNS cache is by starting the dns-clean utility. Run the following command in your Terminal to do so:

$ sudo /etc/init.d/dns-clean start

Related Posts