In this quick guide, we’ll show you how to clear your DNS cache on an Ubuntu-based operating system.
Domain name servers or DNS are crucial components of how the modern web works. This is what your operating system, such as Ubuntu, uses to translate a domain name such as “
pimylifeup.com“To an address that can be connected to, such as”
184.108.40.206„. Without a system like DNS, browsing the web would be a much more tedious process.
To translate a domain name, the operating system must make a call to an external DNS server. In turn, that server responds with the required IP address.
Operating systems such as Ubuntu will try to speed up DNS resolution by creating a cache for each DNS request. This means that when performing a DNS lookup, you only need to access the local cache for the IP address, making the process almost instantaneous.
However, the DNS cache on Ubuntu and other systems is not perfect because it relies on an external service. So if the system cached a bad result, you’ll want to delete it.
In the next two sections, we’ll give you a quick overview of how exactly the DNS cache system works. In addition, we’ll cover how you can easily and quickly clear the DNS cache on Ubuntu.
What is DNS Cache
With the release of Ubuntu 17.04, the operating system began to use “
systemd-resolve”To manage the network name resolution.
With this new implementation came a DNS cache built into Ubuntu. This cache is designed to significantly improve DNS lookup speeds, as the application will not have to leave your system after the initial lookup.
To show how all of this works, let us give you an overview of how DNS request flow flows when using a DNS cache.
1. When Ubuntu searches for a domain name such as “
pimylifeup.com“, The request is made locally to the resolution service.
This service operates on the local address 127.0.0.53 and is used by the system by default.
2. When the service on your Ubuntu system receives the DNS request, it will check if that domain name exists in the DNS cache.
If the domain name exists in the DNS cache, the service will respond instantly to your request.
If the domain name is not in the DNS cache, it will make a request to an external DNS server. You can set the DNS server that Ubuntu uses for these requests.
3. Your DNS request is redirected to the defined DNS server. We hope that this server will translate the domain name into a connectable IP address for your system.
Ubuntu will wait a short response time before switching to the secondary DNS server.
4. If your Ubuntu device receives a valid DNS response, it will store it in the DNS cache. Once cached, the answer is
Subsequent requests can now be served by your local DNS service, speeding up the process.
Clear the DNS cache on Ubuntu
To clear the DNS cache on your Ubuntu system, you will need access to a user with superuser privileges (sudo).
In addition, all steps will need to be completed in the terminal. If you are unfamiliar with the terminal, don’t worry, because the commands we have to use are very simple.
Before you continue, you’ll need to find out which version of Ubuntu you’re running. These steps differ slightly depending on the version you are using.
1. Because we will need to use commands, you will need to open the terminal on your Ubuntu device.
If you are using the desktop version of Ubuntu, you can open the terminal by pressing CTRL + OTHER + T on your keyboard.
2. With the terminal open, you will need to use one of the following commands to clear the Ubuntu DNS cache.
These commands differ because Ubuntu has come to use “
systemd-resolveIn new versions. However, despite the different names, the tools work pretty much the same.
Clear the DNS cache on Ubuntu 20.04, Ubuntu 22.04 and later
If you are using any version of Ubuntu newer than 20.04, you can clear the DNS cache using the command below.
Clear the DNS cache on Ubuntu 17.04 and Ubuntu 18.04
On earlier versions of Ubuntu, such as 18.04 and 17.04, you will need to use the following command to clear the DNS cache
3. Your system's DNS cache should now be cleared. Any DNS issues you have should hopefully be resolved now.
If you would like to see the status of this cache, you can continue reading our next section.
View the DNS cache status on Ubuntu
Now that we've shown you how to clear DNS cache on Ubuntu, let us explore how to see the cache status.
The same tools we used in the previous section to clear the DNS cache also allow you to recover statistics. These statistics give you the current cache size and the number of hits and caches.
1. As well as clearing the cache, you will need to perform these steps in the terminal on your device.
When using the Ubuntu desktop, you can quickly open the terminal by pressing CTRL + OTHER + T.
2. The commands you run will differ slightly depending on the version of Ubuntu you are running.
Obtain DNS Cache statistics on Ubuntu 20.04, Ubuntu 22.04 and later
If you're using Ubuntu 20.04, 22.04 or later, you can use the following command to get statistics from the DNS cache.
Download DNS Cache statistics on Ubuntu 17.04 and Ubuntu 18.04
If you are using Ubuntu 17.04 or 18.04, the command below will retrieve the statistics from the DNS cache.
3. Below you can see the result you will get from running any of the commands below.
The DNS cache size of our Ubuntu systems is currently 0, because we just cleared the cache.
Using these statistics, you can get an idea of how many DNS requests your system caches and how many times that cache is accessed or lost.
By this point in the guide, you should know how to clear the DNS cache on an Ubuntu system.
As I explained earlier, caching is designed to significantly improve the speed of DNS lookups by caching locally.
If you have any questions about clearing the DNS cache, please comment below.
We have a variety of other Ubuntu guides if you want to learn more about the system. Alternatively, we have a lot of general Linux guides.