Open In App

How to Flush the DNS Cache in Linux?

Last Updated : 26 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Are you facing domain resolution issues, or are the websites you're trying to browse not loading fast enough? Well, flushing the DNS cache in Linux can be a fast and effective solution. The DNS cache stores the IP addresses of all the websites you browse and over a period of time, this data becomes outdated or corrupted which causes issues while browsing the internet.

In this guide, we're going to discuss how to flush the DNS cache in Linux, using different methods so whether you're using Ubuntu, CentOS, or any other Linux distribution, this guide will help you clear DNS entries and resolve common DNS errors seamlessly.

What is DNS Cache

The DNS cache is a temporary storage that stores all the DNS records for the websites you visit on a daily basis. These records map domain names to their respective IP address and allows users to access the websites way faster. That's why this website cookies and cache become corrupted and leads to certain issues such as:

  • Unable to load Website pages
  • Network issues
  • Slow browsing speed
  • Frequent page crash

In such cases, flushing the DNS cache in Linux can help ensure that your system retrieves the latest and most accurate DNS information from the internet.

How to Find a local DNS resolver

To find that we can execute the following command to determine the local DNS resolver on our Linux system.

sudo lsof -i :53 -S
  • lsof: The lsof command is used to list open files and associated processes.
  • -i :53: This specifies the query for processes using port 53, which is the default port for DNS.
  • -S: This option shows the processes associated with the specified socket.
Finding Local DNS Resolver
Finding a Local DNS Resolver

In the output, we can see that our process is associated with port 53 which mentions "systemd-resolved". So this is our local DNS resolver.

How to Flush the DNS Cache in Linux

Below we have listed all the possible methods through which we can Flush the DNS Cache on Linux systems

Method 1: Using resolvectl Command (for systemd-based Systems)

Step 1: Open Terminal and Run the resolvectl Command

You can use Linux Keyboard Shortcut using Ctrl + Alt + T to open Terminal instantly and now we will be using the resolvectl command to flush the DNS cache on a Linux system, specifically through the flush-caches option.

sudo resolvectl flush-caches
  • sudo: Executes the command with superuser privileges.
  • resolvectl: The command-line client for interacting with systemd-resolved, a system service managing DNS resolution.
  • flush-caches: Option instructing systemd-resolved to clear and refresh the DNS cache.
Flushing DNS Cache using resolvectl command
Flushing DNS Cache using resolvectl command

Step 2: Verify the DNS cache Flushing

To verify whether the DNS cache is flushed or not, we can execute the below command to verify:

 sudo systemctl status systemd-resolved
  • sudo: Executes the command with superuser privileges.
  • systemctl: A command-line utility to control systemd services.
  • status: Displays information about the current status of a service.
  • systemd-resolved: The name of the service for which we want to check the status.
Verifying the DNS Cache Flush
Verifying the DNS Cache Flush

This confirms that the DNS cache has been cleared and your system is ready to retrieve fresh DNS data.

Method 2: Using signals

The Ubuntu 20.04 and newer versions, use systemd for managing system services. If your system uses systemd-resolved to manage DNS resolution, you can flush the DNS cache using the resolvectl command.

Step 1: Open Terminal and Run the resolvectl Command

In this method, we are sending the USR2 signal to the systemd-resolved process, prompting it to flush and refresh its DNS cache. Using signals offers an alternative method to clear the cache without restarting the entire service.

sudo killall -USR2 systemd-resolved
  • sudo: Executes the command with superuser privileges.
  • killall: Sends a signal to terminate or signal processes based on their names.
  • -USR2: Sends the USR2 signal to the specified process, in this case, systemd-resolved.
Using signals to Flush DNS Cache
Using signals to Flush DNS Cache

Step 2: Verify the DNS cache Flushing

The below command can be used to verify the system journal logs that are related to the systemd-resolved. By using the command, we can verify the flushing process:

sudo journalctl -r -u systemd-resolved
  • sudo: Executes the command with superuser privileges.
  • journalctl: Retrieves and displays system journal logs.
  • -r: Displays logs in reverse order (most recent first).
  • -u systemd-resolved: Filters logs specifically for the systemd-resolved service.
Verifying Flushing Process
Verifying Flushing Process

This command will send the USR2 signal to the systemd-resolved process, effectively clearing the DNS cache without restarting the service.

Method 3: Using systemctl to Restart systemd-resolved (for systemd-based Systems)

You can also choose to clear the DNS cache by restarting the systemd-resolved service using systemctl and this method forces the service to reload the DNS cache. Let's see how to eprform this action:

Step 1: Open Terminal

Press Ctrl + Alt + T to access the Terminal on your Linux system instantly.

Step 2: Run the systemd-resolved Command

Run the following command and ensure that you have admin privilege:

sudo systemctl restart systemd-resolved

Step 3: Recheck by Opening the Browser

Once the service restarts, your DNS cache will be cleared. You can verify the same by opening your preferred web browser to test it by yourself.

This method is useful for those who wish to ensure that their DNS resolver service is refreshed and up-to-date.

Method 4: Using the nscd for Older Systems

This method is suitable for older Linux distributions where we can use nscd (Name Service Cache Daemon). Let's see how we can perform this action:

Step 1: Access the terminal

Press Ctrl + Alt + T to access the Terminal on your Linux system instantly.

Step 2: Run the nscd Command

Run the following command and ensure that you have admin privilege:

sudo /etc/init.d/nscd restart

Step 3: Recheck by Opening the Browser

Once the service restarts, your DNS cache will be cleared. You can verify the same by opening your preferred web browser to test it by yourself.

How to Verify if the DNS Cache Was Cleared or Not

Once you've performed any of the provided method to flush the DNS, you can verify the same by the following steps:

Step 1: Open the Terminal and Run this Command

Open the terminal and use this command to view the status of systemd-resolved:

sudo systemctl status systemd-resolved

Step 2: Test DNS resolution by using nslookup

You may also test the DNS resolution by using the nslookup command to see if the system retrieves the fresh information:

nslookup google.com

Common DNS Flushing Issues

While trying to flush the DNS cache, you might face certain issues, here's how to resolve them in no time:

1. Command Not Found: If this issue occurs while trying to run resolvectl or systemclt, ensure that your system uses systemd-resolved for DNS management. You can check the Linux distribution and its version to verify its compatibility.

2. DNS Resolution Still Fails: Try the following if the DNS resolution fails:

  • Check your DNS settings in the /etc/resolv.conf file.
  • Restart the Networking service using sudo systemctl restart network-manager
  • Ensure that your DNS server is configured properly (for custom DNS)

3. Flush DNS Cache without systemd: You will be required to use the nscd or dnsmasq (depending upon the distribution)


Next Article

Similar Reads