Image: Jack Wallen

The DNS cache is a temporary database on an operating system that stores DNS lookups (information about visited websites and internet domains). How this works is fairly simple: When you visit a website on numerous occasions, your operating system retrieves the information for the website from the locally-stored cache, instead of having to use the slower public DNS records. Because of this, the loading of those websites is considerably faster.

However, there are times when a DNS cache can become corrupted. You might also notice an instance when a website has changed, but your browser insists on loading the old (cached) version of the site instead.

When this happens, what do you do? You flush the DNS cache on your operating system. Once the DNS cache is flushed, the initial loading of websites will be slightly slower, but they’ll be correct. Once the DNS for a site is cached, the loading of said site will speed up.

How you flush the DNS cache will depend on your operating system. I want to show you how easy it is to flush the DNS cache on the Linux operating system.

SEE: Resolve IT issues quickly with these 10 PowerShell cmdlets (TechRepublic download)

What you’ll need

The only things you’ll need to make this work are a Linux distribution that uses systemd and a user with sudo privileges. This same process will work with both Ubuntu-based and Red Hat-based distributions. I’ve tested the process on both Ubuntu 18.04 and CentOS 8 and it works flawlessly.

How to flush your cache

The first thing we need to do is make sure that systemd-resolved is running. To do that, open a terminal window on your desktop or server and issue the command:

sudo systemctl is-active systemd-resolved

In the output of that command, you should only see:

active

If that’s the case, you’re okay to proceed. We’ll then check some statistics for the DNS cache with the command:

sudo systemd-resolve --statistics

When you run that command, you should see a listing for Transactions, Cache, and DNSSEC Verdicts (Figure A).

Figure A

The statistics for the DNS cache on Ubuntu Server 18.04.

The important bit of information is Current Cache Size. We’re going to reset that to 0, by flushing the cache with the command:

sudo systemd-resolve --flush-caches

Once again, issue the command:

sudo systemd-resolve --statistics

You should now see that the Current Cache Size is at 0 (Figure B).

Figure B

We’ve successfully flushed our DNS cache on Ubuntu Server 18.04.

The caveat

If you’re using a Red Hat-based distribution, other than CentOS, you might find the systemd-resolve command doesn’t work. If that’s the case, you might have to try one of the following commands to flush your DNS cache:

sudo systemctl restart nscd.service

Or:

sudo systemctl restart named

One way or another, one of the commands shown here will flush the DNS cache on your Red Hat-based Linux distribution.

And that’s all there is to clearing out that DNS cache. If you find sites aren’t loading properly, or there’s something wonky going on with your network and you can’t quite pin down the problem, give that DNS a little flush and see if it doesn’t clear up the issue.

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays