Ubuntu

How to Flush DNS Cache on Ubuntu 20.04

Flush DNS Cache on Ubuntu 20.04

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.

Let’s see how to flush the DNS cache on Ubuntu.

Note: The procedure described here has been tested on Ubuntu 20.04 LTS. The commands have been executed on Terminal which can be opened through Ctrl+Alt+T shortcut.

How to Flush the DNS Cache

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.

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 8. 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 following output shows the current cache size is “0” which confirms that the DNS cache has been cleared now.

This is 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.

Similar Posts