1. Home
  2. Linux

How to flush the DNS cache on Linux

Computers keep records of every website you visit in a “DNS cache.” This cache’s purpose is to save time when the user tries to access a domain; the system doesn’t have to look it up every time manually. Overall, DNS caching makes your internet usage faster, and that’s why virtually every operating system from macOS, to Windows to Linux do it.

Still, for as useful as DNS caching is, issues can sometimes arise, such as seeing an old website instead of the new updateed one, failing to connect to a remote server, etc. A great way to get around these kinds of errors on Linux computers is to “flush” the DNS cache on Linux. When you flush the DNS cache, it’ll refresh the DNS database and clear up a lot of the resolving issues many users experience.

Flush DNS – Systemd Resolved

At this point, a majority of Linux users are running an operating system that has the Systemd init system built into it. While the subject of Systemd is divisive in the community, there’s one thing that everyone knows: Systemd makes deep, complicated operating-system level tweaks and maintenance a whole lot easier than it was in the past. This fact is especially true with clearing the DNS cache.

Systemd handles the DNS cache in something known as “systemd resolved.” It’s a standard utility that finds it’s a way onto most Linux operating systems. To determine if your OS is using Systemd Resolved to handle DNS, and DNS cache on the system, launch a terminal window by pressing Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. After that, run the systemctl status command on the systemd-resolved.service file.

systemctl status systemd-resolved.service

Look through the printout of Systemd Resolved in the terminal window and locate the “Active” section once you’ve located the “Active” section and ensure that it has “active (running)” next to it. If it does, your Linux system is using this to manage it’s DNS.

To clear the DNS cache for Systemd Resolved, you’ll need to use one of the built-in features of Systemd Resolved. Specifically, the flush caches command.  It’ll instantly clear out the DNS and get you up and running again!

sudo systemd-resolved --flush-caches

Or, if you can’t use sudo, do:

su -
systemd-resolved --flushcaches

Once you’ve used the flushcaches command the DNS caches on the system should be sufficiently flushed. However, if after running this command you’re still running into some issues, it may be a good idea to restart the service altogether. To do this, make use of the systemctl restart command.

sudo systemctl restart systemd-resolved.service

Can’t use sudo? Log in as Root with su before trying to restart the service with systemd.

su -
systemctl restart systemd-resolved.service

When Systemd-Resolved finishes restarting, all DNS problems should be fixed!

Flush DNS – DNS Masq

DNSMasq is another popular DNS solution for the Linux platform that many operating systems use. Much like the Systemd-Resolved tool, users can interact with it with the systemctl command. To determine if your OS is using the DNSMasq tool to manage DNS, run the status command.

systemctl status dnsmasq.service

Read the status printout that systemd provides on screen. Make your way to the “Active” section of the readout. Scan through it and look for “active (running)” to confirm the service is active. If this is the case, it’s possible to immediately clear out the DNS cache for DNSMasq by using the systemctl restart command.  In the terminal, enter the following command.

sudo systemctl restart dnsmasq.service

Or, for those that can’t use systemd commands with sudo, try logging in as Root before trying to restart.

su -
systemctl restart dnsmasq.service

Flush DNS – NSCD

NSCD is the preferred DNS caching system for the majority of RedHat-based Linux operating systems. Therefore, if you’re using OpenSUSE Linux, CentOS or something similar, there’s a good chance that you’ll need to fuss with it to clear your DNS cache.

Note: though Fedora Linux is RedHat-based, by default it does not store a DNS cache.

To determine if your Linux OS uses NSCD, you’ll need to use the status command, as it is a systemd service.

systemctl status nscd.service

Running the status command on the NSCD service will give you a lot of detailed information about it. Skim through and find the section that says “Active.” If your system is using it, you’ll see some green text that says “active (running).”

Flushing the DNS cache with NSCD works pretty much like every other caching system that works with systemd. All that the user needs to do is run the systemctl restart command. It’ll reload the service and automatically clear out the DNS cache, which will fix any DNS issues you may be experiencing.

sudo systemctl restart nscd.service

Alternatively, if your Linux OS disables running systemctl commands with sudo, you can restart the service by first logging into the Root account using su.

su -

systemctl restart nscd.service


Leave a comment