DNS cache stores the DNS query results for faster access to frequently visited websites. However, sometimes the cache may become outdated or corrupted, leading to issues with website access. Clearing the DNS cache may be necessary if you’re experiencing problems with website access or network connectivity. In this article, I’ll show you how to flush DNS cache in Ubuntu 22.04 or 20.04.
Table of Contents
Checking the DNS Cache in Ubuntu
Before clearing the cache, let’s first check if the cache is causing the issue. Follow the steps below:
- Open the terminal by pressing
Ctrl+Alt+T
or search for the terminal in the applications menu. - Type the following command and press enter:
sudo systemd-resolve --statistics
This command will display statistics for your DNS resolver, including the number of queries, cache hits, and cache misses.
- Check the cache hit rate. If the rate is less than 50%, it means the cache is not being utilized effectively, and clearing the cache may help improve website access.
Clearing DNS Cache in Ubuntu
There are multiple ways to clear DNS cache in Ubuntu, but we’ll focus on the two most common methods: using the systemd-resolved service and restarting the network manager service.
Method 1: Using systemd-resolved Service
The systemd-resolved service provides system-wide DNS caching and name resolution. Here are the steps to clear DNS cache using this service:
- Open the terminal and enter the following command to flush the cache:
sudo systemd-resolve --flush-caches
- After flushing the cache, enter the following command to restart the systemd-resolved service:
sudo systemctl restart systemd-resolved.service
This command will reload the systemd-resolved configuration and start the service.
- Verify if the cache has been cleared by running the
sudo systemd-resolve --statistics
command again.
Method 2: Restarting the Network Manager Service
The network manager service is responsible for managing network connections in Ubuntu. Restarting this service can help clear the DNS cache. Here’s how:
- Open the terminal and enter the following command to stop the network manager service:
sudo systemctl stop NetworkManager.service
- Enter the following command to flush the cache:
sudo systemd-resolve --flush-caches
- Finally, enter the following command to start the network manager service:
sudo systemctl start NetworkManager.service
- Check if the cache has been cleared by running the
sudo systemd-resolve --statistics
command again.
Final words
Clearing DNS cache can help improve network connectivity issues in Ubuntu. I hope this article helped you resolve any issues you were facing with DNS cache in Ubuntu.