News & Updates

How to Flush DNS Cache in Linux: Quick Command Guide

By Marcus Reyes 136 Views
how to flush dns linux
How to Flush DNS Cache in Linux: Quick Command Guide

Clearing the DNS cache on a Linux machine is a routine task for network troubleshooting, especially when a system fails to recognize a recently updated domain or IP address. While the operating system automatically manages this database, specific scenarios—such as switching hosting providers or resolving connectivity errors—require manual intervention. This process ensures that the resolver no longer relies on stale records and fetches the latest data from the authoritative nameservers.

Understanding the DNS Cache Mechanism

Before initiating the flush, it is essential to understand how Linux handles domain resolution. The system does not rely on a single universal cache; instead, the behavior depends on the networking stack and active services. Modern distributions often utilize systemd-resolved , NetworkManager , or nscd (Name Service Cache Daemon) to store recent queries locally to reduce latency and external traffic.

This local cache stores Time-To-Live (TTL) records, but administrators often need to override these defaults immediately. Flushing the cache invalidates these entries, forcing the client to perform a fresh lookup with the configured DNS servers, whether they are public resolvers like 8.8.8.8 or internal corporate DNS appliances.

Verifying the Current Cache Status

It is standard practice to check the cache status before and after the flush to confirm the operation was successful and to observe the current statistics. The method to view this data varies depending on the underlying service managing the cache. Running these commands provides insight into cache hits, misses, and the volume of data currently stored in memory.

Using systemd-resolved

If your distribution uses systemd-resolved , which is common in Ubuntu, Debian, and recent versions of Fedora, you can query the statistics using the following command:

Command
Description
sudo systemd-resolve --statistics
Displays current cache statistics for the resolved service.

Using nscd

For systems utilizing the Name Service Cache Daemon, the following command provides the necessary statistics regarding cache efficiency and stored entries:

Command
Description
sudo nscd -g
Displays the general statistics of the nscd daemon, including DNS cache status.

Flushing the Cache with systemd-resolved

The most modern approach to managing DNS in Linux involves systemd-resolved , a system service that provides a local DNS cache and a local DNS stub listener. To clear the entries stored by this specific service, you must communicate directly with its management interface. This action forces the daemon to drop all cached negative and positive replies.

Execute the following command with superuser privileges to perform the flush. You will not receive a visual confirmation upon success unless you check the statistics again, which is a common point of confusion for new Linux administrators.

Command
Description
sudo systemd-resolve --flush-caches
Flushes the DNS cache maintained by systemd-resolved.

Flushing the Cache with nscd

M

Written by Marcus Reyes

Marcus Reyes is a Senior Editor with 15 years of experience investigating complex global narratives. He brings razor-sharp analysis and unapologetic perspective to every story.