Networking

How To: Clear the DNS Cache

Today’s article will be a nice and easy article where we learn how to clear the DNS cache as a simple exercise. This isn’t very difficult and won’t take too much time, so this article should be relatively short.

If you don’t know what DNS cache is, that’s fine. I’ll do my best to explain.

Chances are good that you do not need to clear your DNS cache. This isn’t something you’ll need to do all that often, maybe not ever. I only clear the DNS cache when I need to.

What is DNS?

DNS stands for Domain Name Service. When you type a domain name into your browser’s address bar, it relies on an IP address behind the scenes. DNS is the interface between those two.

You can think of DNS like a phone book, translating names to numbers.

While not important, a single IP address can host many websites. So, think of DNS as the phone book and nameservers are like the names of people who live in the same apartment complex.

As you browse, your computer tries to save you some time. It saves a cache of DNS hits. It saves a cache of domain names and their IP addresses. With a speedy connection, you won’t notice this as much today. However, it’s meant to speed up browsing when you revisit a site you’ve already visited.

Make sense? 

Let’s say you’re like me and have a website. For reasons, you decide to change your hosting company. You do so and update the nameservers. You now have a new IP address for your domain name, at least you will when the changes propagate.

Suddenly, you have an old IP address cached for that domain name. Because it is in the cache, your system won’t look that address up again. What do you do to get access to the site again?

Clear DNS Cache:

We’ll learn to clear the DNS cache in the terminal. In fact, I don’t know of a GUI way to do this for the system. (It’s possible to clear the DNS cache in Chrome via a GUI.) So, open a terminal. Many of you can just press CTRL + ALT + T and your default terminal will pop open.

With your terminal now open, enter one of the following commands to see the state of affairs regarding your DNS cache:

sudo systemd-resolve --statistics

Or:

sudo resolvectl statistics

One of those two commands should work for you.

Here’s an example output:

$ sudo resolvectl statistics
DNSSEC supported by current servers: no

Transactions
Current Transactions: 0
Total Transactions: 563365

Cache
Current Cache Size: 44
Cache Hits: 1397
Cache Misses: 10952

DNSSEC Verdicts
Secure: 0
Insecure: 0
Bogus: 0
Indeterminate: 0

Now, let’s clear that cache.

One of the following commands should work for you:

sudo resolvectl flush-caches

Or:

sudo systemd-resolve --flush-caches

There won’t be any output from that command to confirm that the cache has been cleared. If you run the first command all over again, you should see something like this after you’ve run the command:

Current Cache Size: 0

See? It’s pretty easy to clear the DNS cache!

Closure:

This is only something a few people will need to do. If you’re having issues visiting a site you recently were able to access without issue then this might be something you try. You can try to clear the DNS cache to see if it helps but there are a million and ten reasons why a site may suddenly be down and DNS is unlikely to be the issue unless you have a specific reason to expect this particular problem and solution.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

Recent Posts

Enable/Disable Your Network Interface

Today we'll cover one way to enable or disable your network interface in the Linux…

5 months ago

Check Your NIC Speed In The Terminal

Today's exercise is a nice and simple exercise where we check your NIC speed in…

5 months ago

Easily Monitor Your Wireless Connection

Have you ever wanted to easily monitor your wireless connection? Well, now you can learn…

6 months ago

Count The Files In A Directory

I think I've covered this before with the ls command but this time we'll count…

6 months ago

Get System Information With The ‘uname’ Command In Linux

Today we'll be learning about a basic Linux command that's known as 'uname' and it…

6 months ago

hardinfo Has Been Rebooted As hardinfo2

If you've used hardinfo in the past, it may interest you to know that hardinfo…

6 months ago