Flush DNS Cache on Linux

How to Flush DNS Cache on Linux

Let’s see How to Flush DNS on Linux and How the DNS Cache Works.

Here some topics that we will cover in this article:

How to Flush DNS on Linux
How to Flush DNS in Mac
How to test if DNS is working

Before showing you how to flush DNS cache on Linux, let me give you some background about this topic. Or, If you would like to jump directly to the command line to flush DNS Cache, feel free.

But, In my experience, it’s worth knowing the details so that you can resolve any issue regarding DNS and save a lot of time.

Let me prove to you, continue reading. Let’s get started.

How DNS cache works on Linux

On Linux, a caching DNS server acts by doing all the DNS queries that your system performs and then storing, or caching, the results in memory. When the operating system cache in memory, all DNS queries (the results), if you make a duplicate request for a domain like bitslovers.com, the result will get instantaneously from memory, so it will be much faster.

Let me explain why:

For example, when you are accessing a website, you type www.bitslovers.com on your Browser, you are using the DNS protocol to get the server IP.

And, to improve performance, the DNS requests that you make are stored in a cache on your computer, so you don’t need to go out to the Internet and find out the IP for that server again.

The most important part:

How DNS Cache works on Linux
How DNS Cache works on Linux

When to Flush DNS

For instance, sometimes you have noticed a weird behavior, or if you have a website that you manage the DNS for it, and you need to make some tests by adding or change records to your DNS, you may want to flush the DNS cache of your server, to see the change immediately on your computer.

Also, if you are trying to access one website on Browser and the page doesn’t load, and at the same time, you are sure that you are connected to the Internet. 

What does that mean for you?

A situation like that could indicate to us an issue with your DNS cache.

Here’s the deal: 

I want to mention a vital file configuration file on Linux regarding DNS: /etc/resolv.conf.

What is resolv.conf in Linux

Let’s see what resolv.conf file does, also be aware that any change on this file requires you to flush DNS.

The resolv.conf is a configuration file that holds information that permits a computer to change human-readable alpha-numeric domain names (FQDN) into machine-readable IP addresses (IPv4 or IPv6). Converting domain names into IP addresses is named resolution, domain name resolution, or DNS resolution.

The Resolver uses the /etc/resolv.conf, let’s talk about the Resolver:

The Resolver is a collection of routines in the C library that provide access to the DNS. The resolver configuration file resolve.conf (/etc/resolv.conf) contains information read by the resolver routines the first time a process invokes them. The file is designed to be human-readable and includes a list of keywords with values that accommodate various types of resolver information.

Example of resolv.conf on Linux:

Please don’t copy the example and replace it on your file, and each network configuration has your file. If you replace it with my example, it will make your DNS resolution stop working.

options edns0
search lan

Let’s explore each term from resolv.conf

The “nameserver”:

nameserver is the IP address of the name server. You can configure more than one nameservers in resolv.conf

If there are multiple nameservers, the resolver library executes the queries following the same sequence as listed in resolv.conf. 

The “search”:

The “search” term includes a list of domain search paths that Resolver will query for any names, and this is very useful when you want to perform DNS lookup in multiple domains. In addition, the Resolver can use the “search” term list to resolve shorthand names.

The “options” term:

The main idea of “options” to configure resolver config parameters like timeout, ndots, rotate, etc.

Why is resolv conf overwritten?

One of the reasons, and the most possible, if you any VPN clients, most of them modify your /etc/resolv.conf is to ensure that the VPN client computer uses a secure nameserver for hostname resolution. Nothing is stopping you from changing resolv.conf after the VPN router cached the nameserver.

On the other hand, the file resolv.conf could be missing.

Resolv.conf missing

If the resolv.conf file missing or the information on it is invalid, you may see this error when you try to execute the command ping:

ping google.com
# output:
ping: google.com: Name or service not known

To resolve the issue above, you need to fix your resolv.conf only if you have sure that you are connected to the internet.

If your computer uses a WIFI connection, you can turn off and turn on the WIFI, executing this process forces the operating system to recreate the resolv.conf.

You can run:

nmcli radio wifi off && sleep 10 && nmcli radio wifi on

When you turn on the WIFI again, the computer will retrieve all information regarding DNS from your router

What is TTL (DNS)

Also, because the DNS records have an attribute called TTL (Time-to-Live), the amount of time in seconds that the cache will be valid. So, for example, if the TTL is 300 seconds, the operating system should refresh the DNS cache after that time if you request that same DNS and not get from the cache again.

But wait – there’s more.

In addition, flush DNS cache is also a good maintenance practice. DNS storage cache data doesn’t use too much data on your computer disk or server, but the list can become prominent with time if we don’t clear the cache for a period. 

404 Errors

Also, it can prevent the cache from getting back unsatisfactory results and 404 errors when you are going back to old servers, as old IP details may not show the current server details, for example, if TTL expired.

Sounds good, right?

For example, the most common way to clean the cache in most cases is by using the systemd. However, if you have configured, for example, the dnsmasq as DNS resolver for managing your DSN cache, it will be another way, but easy too.

What are DNS resolvers?

Following the list below the most common:

1 – systemd-resolved

2 – DNSMasq

3 – BIND

4 – Nscd

What your DNS resolver?

First, let’s double-check which DNS resolver you are using, so you can discover which command you should use:

sudo lsof -i :53 -S

In most cases, the output is:

systemd-r 21830 systemd-resolve 12u IPv4 15913414 0t0 UDP localhost:domain
systemd-r 21830 systemd-resolve 13u IPv4 15913415 0t0 TCP localhost:domain (LISTEN)

In my case, I am using the systemd-resolve, as you can see on the output above.

If you got “sudo: lsof: command not found,” just run the command below to install it.

sudo apt-get install -y lsof

To clarify, the DNS runs on port 53. The lsof command will show us which process/application is using that port, so we will find which application manages the DNS on your computer.

Finally, let’s see how to reset DNS cache.

Flush DNS using systemd-resolved

This command works on all modern Linux like Debian, Ubuntu, Linux Mint, Arch Linux.

sudo systemd-resolve --flush-caches 
sudo resolvectl flush-caches

Would you like to make sure that the cache was clean up? It’s simple just run the command below:

sudo systemd-resolve --statistics

You can check that the “Current Cache Size” will be zero.

How to Flush DNS on Linux using signals

There is another way to flush DNS cache by executing and sending a “USR2” signal to the “systemd-resolved.” This signal will tell the systemd to flush the DNS cache for us.

sudo killall -USR2 systemd-resolved

Next, you can check if the command above flushed the DNS cache correctly by sending a “USR1” signal to the systemd-resolved service. This way, it will show its current state in the systemd journal.

sudo killall -USR1 systemd-resolved
sudo journalctl -r -u systemd-resolved

To understand that command and for extra acknowledge, the signal “USR1” and “USR2” are helpful for simple interprocess communication. This signal helps the developers write a signal handler for them to receive the signal. They haven’t a specific meaning – intended for whatever arbitrary use the developer wants. Whoever designed the program can use it to perform some routine like “shut down” or “dump logs” or “restart the service” or anything. But, in this case, the “system-resolved” developers use that signal to flush DNS.

How to Flush DNS Cache on Redhat and Fedora?


Nscd is a caching service used on Fedora and RedHat. If your system is using Nscd, to flush the DNS cache, you need to restart the Nscd service:

sudo service nscd restart


sudo systemctl restart nscd.service

How to flush DNS on Linux with DNSMasq

Dnsmasq is a DHCP and DNS caching nameserver.

If your operating system is utilizing DNSMasq as a caching server, to clear the DNS cache, you need to restart the Dnsmasq service using that command below:

sudo systemctl restart dnsmasq.service

How to view and flush DNS cache from BIND on Linux

Likewise DNSMasq, we have the BIND or Berkeley Internet Name Domain is the most popularly used Open source software that uses DNS protocols, which allows us to implement IP to domain name conversion in both directions.

To see the cache:

rndc dumpdb -cache

Let’s flush the DNS cache, run the command below:

rndc flush

Second, reload bind:

rndc reload
server reload successfully

How to Flush DNS Cache on macOS?

Because we have many macOS versions, you need to make sure which one you are using, but to make it easier, let’s go through them.

First, you must execute the command as a user with system administrator privileges (known as sudo user).

So, to clear the DNS cache in macOS, execute the subsequent steps:

Mas OS Flush DNS:

  1. Open the Finder.
  2. Find and go to Applications > Utilities > Terminal. You will see the Terminal window.
  3. In the Terminal, type the following command and press Enter:
  4. sudo killall -HUP mDNSResponder
  5. Insert your sudo password, and press Enter. If the command runs with success, the system will not show any message.

if the version of macOS is between the versions below, the process to flush DNS cache it’s a bit different:

Between macOS version 10.11 and 10.9:

sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder

Or macOS version 10.10

sudo discoveryutil mdnsflushcache
sudo discoveryutil udnsflushcaches

Finally, macOS version 10.6 and 10.5

sudo dscacheutil -flushcache

How to test if DNS is working

On Terminal:

1 – Execute a ping command:

ping google.com

2 – Use dig command:

dig google.com

You should see someting like that:

; <<>> DiG 9.11.3-1ubuntu1.12-Ubuntu <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50781
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 1
; EDNS: version: 0, flags:; udp: 65494
;google.com. IN A ;; ANSWER SECTION:
google.com. 74 IN A
google.com. 74 IN A
google.com. 74 IN A
google.com. 74 IN A
google.com. 74 IN A
google.com. 74 IN A

;; Query time: 0 msec
;; WHEN: Tue Jun 29 15:22:12 CDT 2021
;; MSG SIZE rcvd: 135

Or you can try to open a web page on your Browser.


In conclusion, you learned how you could immediately and efficiently flush your DNS cache on Linux. Is it straightforward, right?

In addition, I hope you have learned how the cache works because it will save you many headaches!

If you would like to learn more about the /etc/resolv.conf you can check this manual.

If you would like to go deep into the Linux operation system, check this post with tips and a learning path to become an expert Linux user.

Before you go! Thanks for your visit. I hope that you came back. Could you take one 1 minute and give me feedback?

Help me to share this article on your social network.

Leave a Comment

Your email address will not be published.