How to Clear DNS Cache in Ubuntu 22.04 or 20.04

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.

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:

  1. Open the terminal by pressing Ctrl+Alt+T or search for the terminal in the applications menu.
  2. 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.

  3. 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:

  1. Open the terminal and enter the following command to flush the cache:
    sudo systemd-resolve --flush-caches
  2. 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.

  3. 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:

  1. Open the terminal and enter the following command to stop the network manager service:
    sudo systemctl stop NetworkManager.service
  2. Enter the following command to flush the cache:
    sudo systemd-resolve --flush-caches
  3. Finally, enter the following command to start the network manager service:
    sudo systemctl start NetworkManager.service
  4. 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.

Gyula Virag
Gyula Virag

Gyula is a developer and a passionate geek father with a deep love of online marketing and technology. He always seeks challenging adventures and opportunities to create something permanent in the digital world.

Articles: 54