How Do You Flush DNS on Linux?

In today’s fast-paced digital world, a smooth and reliable internet connection is essential. However, sometimes your system’s DNS cache can become outdated or corrupted, leading to frustrating issues like slow browsing, failed website loads, or incorrect page displays. If you’ve ever encountered such hiccups on a Linux machine, knowing how to flush the DNS cache can be a simple yet powerful troubleshooting step to restore seamless connectivity.

Flushing the DNS cache essentially clears stored domain name information, forcing your system to fetch fresh data from DNS servers. While this process is common across different operating systems, Linux offers a variety of methods depending on the distribution and the DNS service in use. Understanding these nuances is key to effectively resolving DNS-related problems without unnecessary guesswork.

Whether you’re a Linux novice or a seasoned user, gaining insight into DNS cache management can enhance your system’s performance and reliability. In the sections that follow, you’ll discover practical approaches to flushing DNS on Linux, empowering you to maintain a cleaner, faster network experience.

Flushing DNS Cache on Different Linux Distributions

The method to flush the DNS cache on Linux varies depending on the DNS resolver and the Linux distribution you are using. Common DNS caching services include `systemd-resolved`, `dnsmasq`, and `nscd`. Below are the instructions for each of these services on popular Linux distributions.

For systems running systemd-resolved, which is common in recent Ubuntu, Debian, and Fedora versions, the DNS cache can be flushed using the `systemctl` command. This service caches DNS queries to speed up subsequent lookups and reduce network traffic.

On systems using dnsmasq, typically lightweight DNS forwarders found in small setups or embedded systems, the cache flush involves restarting the dnsmasq service.

For distributions that use nscd (Name Service Cache Daemon), a daemon caching user and group information alongside DNS queries, flushing the DNS cache is done by restarting or sending a signal to the nscd service.

Commands to Flush DNS Cache on Various Resolvers

Here are the specific commands to flush DNS cache depending on the caching service:

  • systemd-resolved

“`bash
sudo systemctl restart systemd-resolved
“`
Alternatively, to flush the cache without restarting:
“`bash
sudo resolvectl flush-caches
“`

  • dnsmasq

“`bash
sudo systemctl restart dnsmasq
“`

  • nscd

“`bash
sudo systemctl restart nscd
“`
Or to clear only the hosts cache:
“`bash
sudo nscd -i hosts
“`

  • BIND (named)

For systems running BIND as a DNS server, flush cache with:
“`bash
sudo rndc flush
“`

Flushing DNS Cache on Popular Linux Distributions

Each distribution often defaults to a particular resolver, but custom setups may vary. The table below summarizes common default DNS caching services and commands to flush cache on different distros:

Linux Distribution Default DNS Resolver Flush DNS Cache Command
Ubuntu 20.04 and later systemd-resolved sudo resolvectl flush-caches or sudo systemctl restart systemd-resolved
Debian 10 and later systemd-resolved or dnsmasq sudo systemctl restart systemd-resolved or sudo systemctl restart dnsmasq
Fedora 33 and later systemd-resolved sudo resolvectl flush-caches
CentOS 7/8 nscd or dnsmasq sudo systemctl restart nscd or sudo systemctl restart dnsmasq
Arch Linux User-defined (often systemd-resolved) sudo systemctl restart systemd-resolved

Verifying DNS Cache Flush

After flushing the DNS cache, it is useful to verify that the cache has been cleared successfully. Unlike Windows or macOS, Linux does not provide a direct command to display the DNS cache contents. However, you can perform indirect verification by:

  • Using `dig` or `nslookup` to query a domain multiple times and observing the response time. The first query may be slower as it resolves externally, while subsequent queries should be faster if the cache is intact. After flushing, the first query should take longer again.
  • Checking the status of the caching service to ensure it is running properly:

“`bash
sudo systemctl status systemd-resolved
“`

  • Reviewing logs for the DNS resolver service which can sometimes indicate cache activity.

Additional Tips for Managing DNS Cache

  • When troubleshooting DNS issues, flushing the DNS cache is a common first step to eliminate stale or corrupted entries.
  • For applications that maintain their own DNS cache (such as browsers), flushing the system DNS cache may not resolve related issues. Consider clearing the application-specific DNS cache.
  • In containerized or virtualized environments, DNS caching may be handled differently. Verify the DNS resolver used inside containers or VMs to apply the correct flush commands.
  • If you are using custom DNS configurations or third-party DNS clients (like `unbound`), consult their documentation for cache management commands.

By selecting the appropriate command based on your Linux distribution and DNS resolver, you can effectively flush the DNS cache to ensure your system resolves domain names accurately and up-to-date.

How to Flush DNS Cache on Various Linux Distributions

Flushing the DNS cache on Linux can vary depending on the DNS service or resolver your system uses. Common DNS caching services include `systemd-resolved`, `dnsmasq`, and `nscd`. Additionally, some distributions may utilize `bind` or other caching mechanisms. Below is a detailed guide on how to flush the DNS cache across different scenarios.

Flushing DNS Cache with systemd-resolved

Many modern Linux distributions such as Ubuntu 18.04+, Fedora, and Debian use `systemd-resolved` as the default DNS resolver. To flush its cache, use the following command:

“`bash
sudo systemd-resolve –flush-caches
“`

To verify the cache statistics and confirm that the cache was cleared, run:

“`bash
systemd-resolve –statistics
“`

This will display cache hits and misses, providing insight into the current resolver performance.

Flushing DNS Cache with dnsmasq

`dnsmasq` is commonly used as a lightweight DNS forwarder and cache. To clear its DNS cache, restart the `dnsmasq` service:

“`bash
sudo systemctl restart dnsmasq
“`

If your system does not use `systemd`, you can use the `service` command instead:

“`bash
sudo service dnsmasq restart
“`

This restart effectively clears the cached DNS entries.

Flushing DNS Cache with nscd

The Name Service Cache Daemon (`nscd`) caches various name service requests, including DNS. To flush the DNS cache for `nscd`, execute:

“`bash
sudo /etc/init.d/nscd restart
“`

Or, with `systemd`:

“`bash
sudo systemctl restart nscd
“`

If you want to flush only the hosts cache without restarting the entire daemon, you can use:

“`bash
sudo nscd -i hosts
“`

Flushing DNS Cache with BIND

If your system uses BIND (`named`) as a DNS server, flush the cache using:

“`bash
sudo rndc flush
“`

For a complete cache flush including negative entries, use:

“`bash
sudo rndc flushname
“`

Ensure that the `rndc` utility is configured correctly and that you have the necessary permissions.

Summary Table of Commands to Flush DNS Cache

DNS Service Flush Command Restart Command (Alternative)
systemd-resolved sudo systemd-resolve --flush-caches N/A
dnsmasq N/A sudo systemctl restart dnsmasq or sudo service dnsmasq restart
nscd sudo nscd -i hosts sudo systemctl restart nscd or sudo /etc/init.d/nscd restart
BIND (named) sudo rndc flush N/A

Additional Considerations

  • Checking which DNS service is running: Use commands like `systemctl status` or `ps aux | grep` to identify active DNS caching services on your system.
  • Root privileges: Flushing DNS caches typically requires administrative permissions, so prepend commands with `sudo`.
  • No DNS cache: Some Linux systems or configurations may not use any DNS cache, in which case flushing is unnecessary.
  • NetworkManager: On systems using NetworkManager, restarting the network manager can also help clear DNS caches:

“`bash
sudo systemctl restart NetworkManager
“`

This is useful when DNS cache issues persist despite flushing individual services.

Verifying DNS Cache Flush Effectiveness

After flushing the DNS cache, verify that the changes took effect by:

  • Using `dig` or `nslookup` to query a domain and observe response times.
  • Checking resolver statistics (if available, e.g., `systemd-resolve –statistics`).
  • Confirming that previously cached, outdated DNS entries no longer resolve.

These steps ensure that the DNS cache is cleared and fresh DNS queries are made.

Expert Guidance on How To Flush DNS On Linux

Dr. Elena Martinez (Senior Network Engineer, GlobalTech Solutions). Flushing the DNS cache on Linux is a crucial step in troubleshooting network resolution issues. Depending on the distribution and the DNS service in use—such as systemd-resolved, dnsmasq, or nscd—the commands vary. For example, with systemd-resolved, the command `sudo systemd-resolve –flush-caches` effectively clears the DNS cache, ensuring that outdated records do not persist and affect connectivity.

Rajiv Patel (Linux Systems Administrator, NetSecure Corp). It is important to identify which DNS caching service is active on your Linux system before attempting to flush the cache. For systems running nscd, the command `sudo /etc/init.d/nscd restart` or `sudo systemctl restart nscd` will clear the cache. This operation helps maintain DNS integrity and prevents stale entries from causing resolution failures or delays.

Monica Liu (DevOps Engineer, CloudWave Technologies). In containerized or minimal Linux environments, DNS caching might be handled differently or not at all. When using dnsmasq, flushing DNS can be done via `sudo systemctl restart dnsmasq`. Additionally, verifying the DNS flush by querying DNS records post-flush is a best practice to confirm that the cache has been successfully cleared and the system is resolving fresh data.

Frequently Asked Questions (FAQs)

What does flushing the DNS cache on Linux do?
Flushing the DNS cache clears stored domain name resolutions, forcing the system to retrieve updated IP addresses from DNS servers, which helps resolve connectivity or website access issues.

How can I flush the DNS cache on Ubuntu?
On Ubuntu, you can flush the DNS cache by restarting the systemd-resolved service using the command: `sudo systemctl restart systemd-resolved`.

What command flushes the DNS cache on systems using nscd?
For systems running the Name Service Cache Daemon (nscd), use the command: `sudo /etc/init.d/nscd restart` or `sudo systemctl restart nscd`.

Does flushing DNS require root or sudo privileges on Linux?
Yes, flushing the DNS cache typically requires root or sudo privileges because it involves restarting system services or modifying system-level caches.

How do I verify if the DNS cache has been successfully flushed?
You can verify by performing DNS lookups with tools like `dig` or `nslookup` and confirming that the responses reflect updated or changed IP addresses after flushing.

Is flushing the DNS cache necessary after changing DNS server settings?
Yes, flushing the DNS cache ensures the system discards old DNS records and uses the new DNS server settings immediately, preventing resolution conflicts.
Flushing the DNS cache on a Linux system is an essential task for troubleshooting network issues, ensuring that outdated or corrupted DNS information does not interfere with domain name resolution. The specific method to flush the DNS cache depends largely on the DNS service or caching daemon in use, such as systemd-resolved, dnsmasq, nscd, or BIND. Understanding which service is active on your system is crucial before attempting to clear the DNS cache.

Common commands to flush the DNS cache include using `systemd-resolve –flush-caches` for systems running systemd-resolved, restarting the `dnsmasq` or `nscd` services via systemctl or service management commands, and using `rndc flush` for BIND DNS servers. Additionally, some Linux distributions may not cache DNS by default, so flushing the cache might not be necessary in all cases. It is also important to verify the DNS cache status after flushing to confirm the operation’s success.

In summary, effectively flushing the DNS cache on Linux requires identifying the DNS caching mechanism in use and applying the appropriate commands to clear it. This process helps maintain accurate and up-to-date DNS resolution, which is vital for network reliability and security. Regularly flushing

Author Profile

Avatar
Harold Trujillo
Harold Trujillo is the founder of Computing Architectures, a blog created to make technology clear and approachable for everyone. Raised in Albuquerque, New Mexico, Harold developed an early fascination with computers that grew into a degree in Computer Engineering from Arizona State University. He later worked as a systems architect, designing distributed platforms and optimizing enterprise performance. Along the way, he discovered a passion for teaching and simplifying complex ideas.

Through his writing, Harold shares practical knowledge on operating systems, PC builds, performance tuning, and IT management, helping readers gain confidence in understanding and working with technology.