How to Flush DNS Cache in Ubuntu 22.04 or 20.04

sudo systemd-resolve --flush-caches

In this article you will learn what is the purpose of DNS caching and why it is important to flush it.

Additionally you’ll be acquainted with how to clear DNS cache using the system-resolved service.

In order to understand the importance of flushing your DNS cache, first we need to break down some several components.

What is DNS ?

In the field of networking, machines don’t go by name like us humans, they only go by numbers.

Computers, smartphones and similar devices talk and identify with each other over a network, which is by using numbers such as IP addresses.

In contrast, humans have a strong tendency to using names instead of numbers. So in order to link the communication gap between humans and machines so to make the communication a lot easier, networking architects developed a naming system called Domain Name System or DNS for short.

The objective of DNS in simple words is to resolve names to numbers, to be more specific, DNS resolves domain names to IP addresses. If a person typed in on their browser and hit the return button, DNS will resolve Google’s domain name to by searching through its database and find the matching IP address of your web address (domain name). Only then your machine will be able to communicate with Google.

What is DNS Cache?

DNS caching helps a network to work in an efficient way, it frees up bandwidth and also minimizes search time (IP address and domain name).

In large networking structures, resolving domain names to IP addresses can consume a fair amount of bandwidth and also results in long search times.

So the part of DNS caching comes in, after going through the process of resolving an IP address of a resource (the process: local machine, resolver server, root server, TLD), this piece of data is stored (It’s called a resource record) within the local cache of the machine, this machine can be a personal computer or a DNS server.

Furthermore, as long as the resource record is stored in the current machine, the process of resolving the same domain name to its IP address will be overridden when the same connection action is attempted.

Obviously, this trick will minimize the request and response time between the local machine and the web server, also keeps the bandwidth free from the process of resolving IP addresses.

Why Flush DNS Cache ?

Along the obvious efficiency and time saving benefits, DNS caching can have a negative impact.

The problem with storing a copy of a resource record for later use is that the original record can change at any time, leaving you with an out dated copy that will not direct you to the correct location.

Additionally, one major concern with DNS caching is that it can be used as an attack vector. Since DNS direct machines on where to locate resources, it can be used to mislead by distributing incorrect information.

When this is done at the DNS cache level, it is called DNS cache poisoning, where false data is sent to the local cache, directing machines to malicious IP addresses which are attempting to steal personal data.

Using systemd-resolved to flush DNS Cache

system-resolved is a local system service that provides network name resolution to local applications, which is a part of the systemd suite of system management tools. This tool is available for all of the latest Linux distributions, including Ubuntu 20.01 / 22.04. However, to flush/check the DNS cache in Ubuntu 22.04 we will use the resolvectl command, which is a tool – ( belongs to core/ system package as well) – used to resolve domain names, DNS resource records and services with the systemd-resolved resolver service. Either way, systemd-resolved “service” should be up and running for both cases ( Ubuntu 20.04 or 22.04 ).

Checking system-resolved statues

First and foremost we should check if system-resolved is active and running. So let’s fire up the terminal and execute the mentioned below command:

sudo systemctl is-active systemd-resolved

If the command returns active, then the system-resolved tool is running on your machine. On the contrary if you encountered the following output/problem:

sudo systemctl is-active systemd-resolved

The above output inactive means that system-resolved is not active and running correctly on your machine. All we have to do is type and execute the following two commands and check again if the system-resolved is active:

sudo systemctl enable systemd-resolved.service
Created symlink /etc/systemd/system/dbus-org.freedesktop.resolve1.service → /lib/systemd/system/systemd-resolved.service.
Created symlink /etc/systemd/system/ → /lib/systemd/system/systemd-resolved.service.
sudo systemctl start systemd-resolved.service
sudo systemctl is-active systemd-resolved

Note: To check what DNS resolver your system is using execute the following command:

sudo lsof -i :53 -S
systemd-r 4845 systemd-resolve   12u  IPv4  76253      0t0  UDP localhost:domain
systemd-r 4845 systemd-resolve   13u  IPv4  76254      0t0  TCP localhost:domain (LISTEN)

Check the above command line output, and notice that our system is using system-resolved service as a DNS resolver.

Checking the DNS Cache Size

Checking the DNS Cache Size in Ubuntu 20.04

Normally before removing your DNS cache, you’d want to check some information about it, including its size. To achieve that, we should use the -statistics argument as demonstrated below.

sudo systemd-resolve –statistics
DNSSEC supported by current servers: no

Current Transactions: 0
  Total Transactions: 5439

  Current Cache Size: 70
          Cache Hits: 731
        Cache Misses: 1450

In the above command line output, check the Current Cache Size; For our situation the cache size is 70. Naturally we want this value to be 0.

Checking the DNS Cache Size in Ubuntu 22.04

In case you encountered the following output:

sudo : systemd-resolve: command not found

It means your Linux distribution (e.g. Ubuntu 22.04) is supporting only the resolvectl command to check the cache size. So, if you are using Ubuntu 22.04, execute the following command to check you cache size:

resolvectl statistics
DNSSEC supported by current servers: no
Current Transactions: 0
  Total Transactions: 135

  Current Cache Size: 24
          Cache Hits: 10
        Cache Misses: 28

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

Flushing the DNS Cache

After checking the DNS cache size, the obvious next step is to make its value 0.

Flushing DNS Cache in Ubuntu 20.04

To do so in Ubuntu 20.04, we will have to use the --flush-caches command line argument, just as so:

sudo systemd-resolve --flush-caches

The above the command will not return any output; Check your DNS cache size again with the -statistics command line argument, and you will notice that the DNS cache size is now 0.

Flushing DNS Cache in Ubuntu 22.04

For Ubuntu 22.04 users, follow the below given command (resolvectl) :

resolvectl flush-caches

Now let’s check the cache size:

resolvectl statistics
DNSSEC supported by current servers: no


Current Transactions: 0
  Total Transactions: 39

  Current Cache Size: 0
          Cache Hits: 10
        Cache Misses: 32

Note: If you want set the statistics counters shown in statistics to 0, use the reset-statistics argument as so:

sudo resolvectl reset-statistics

Another method to clear your DNS Cache is simply by restarting the local system-resolved service. We can achieve that by running the below mentioned command.

sudo systemctl restart systemd-resolved

Run the DNS Cache size again and you will notice that it was cleared.

A third way to flush the DNS cache is by sending the USR2 signal to the system-resolved service. You can perform this action by following the given command below.

sudo killall -USR2 systemd-resolved

Now to actually check if the DNS cache was actually flushed, we will have to send a USR1 signal to our system-resolved:

sudo killall -USR2 systemd-resolved

Note: The command given above just tells the systemd-resolved service to write all the current cache entries to the system log.

Now you can then export (for later use) the log messages written by your systemd-resolved service to a text file with the following command:

sudo journalctl –u -r systemd-resolved > ~/logsysmd.txt


In this how to article, we’ve learned what is a Domain name system/ DNS, what are the benefits of DNS Caching, and why it is important to flush/clear the DNS cache.

We also discussed the different ways of flushing the DNS cache (Ubuntu 20.04 & 22.04), plus viewing information about our cached data.

Fortunately, the process of flushing DNS cache is quite easy, it only take a few steps to be achieved. I hope you learned something new. If you have any feedback or questions please feel free to leave a comment and we’ll get back to you as soon as possible.

Notify of
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
1 year ago

in the last command (to export .txt file) i got the following error: failed to add match ‘-u’: Invalid argument and i would like to know what that means

You May Also Like