Open In App

How to Flush the DNS Cache in Linux?

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Flushing the Domain Name System (DNS) cache in Linux is a common troubleshooting step to resolve various networking issues and ensure the system retrieves the most up-to-date information when resolving domain names to IP addresses. The DNS cache stores previously resolved domain names and their corresponding IP addresses, reducing the time and resources required for subsequent requests. However, sometimes, outdated or incorrect information may be cached, leading to connectivity problems. Flushing the DNS cache in Linux involves clearing this stored data, prompting the system to require DNS servers for the latest information. This process can be crucial for resolving issues such as domain resolution errors, website inaccessibility, or changes in DNS configurations. This article will explore the methods to flush the DNS cache on various Linux distributions, providing users with a comprehensive guide to effectively manage and troubleshoot DNS-related issues.

What is DNS Cache?

DNS cache, or Domain Name System cache, is the process used by operating systems and network devices to store recently resolved domain names and their corresponding IP addresses. This caching process is implemented to enhance the efficiency and speed of subsequent domain name resolutions. When a user accesses a website or any network resource, the DNS system translates the human-readable domain name into a numerical IP address that machines can understand. To avoid repeatedly querying external DNS servers for frequently accessed domain names, the operating system caches this information locally. Subsequent requests for the same domain can then be quickly resolved by retrieving the information from the local DNS cache rather than making a new request to the DNS server. Clearing or flushing the DNS cache is a common troubleshooting step to ensure the most up-to-date information is retrieved and to address potential connectivity issues.

How to Find a local DNS resolver?

Before starting the DNS Cache flushing process, we need to find the local DNS resolver on our system. So 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
  • Method 2: Using signals

Method 1: Using resolvectl Command

Step 1: In this method, we are 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: 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

Method 2: Using signals

Step 1: 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: 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

Conclusion

In conclusion, flushing the DNS cache in Linux is a straightforward process that involves using the terminal and a few simple commands. To refresh the DNS cache, we can use the “resolvectl ” or “signals“, depending on their Linux distribution. The resolvectl command is commonly used in systems utilizing systemd. By executing these commands with appropriate options, users can promptly clear the DNS cache, ensuring that the system retrieves the latest DNS information when resolving domain names.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads