Flushing the DNS cache on a Linux machine is a fundamental troubleshooting step for resolving network issues, particularly when domain names fail to resolve to the correct IP address. Unlike operating systems such as Windows, Linux distributions do not rely on a single, universal command, instead utilizing a variety of methods depending on the init system and the specific services running in the background. This guide provides a detailed walkthrough of the procedures required to clear the DNS resolver cache effectively.
Understanding the Linux DNS Caching Mechanism
Before executing any commands, it is essential to understand that Linux does not utilize a monolithic DNS cache like some other operating systems. Instead, caching is typically handled by a dedicated local DNS stub resolver, with the most common implementation being systemd-resolved. This service sits between applications and the upstream DNS servers provided by the network, storing recent queries to speed up resolution times. If systemd-resolved is not in use, the system might be relying on nscd (Name Service Cache Daemon) or simply forwarding queries directly to the configured nameservers without local caching.
Identifying Your DNS Resolver Service
To determine the correct method for flushing the cache, you must first identify which service is managing DNS resolution. The most reliable way to do this is by checking the process list or the active systemd units. By understanding the backend in use, you avoid executing commands that will have no effect on the system configuration, ensuring that your troubleshooting efforts are efficient and effective.
Check for systemd-resolved
The prevalence of systemd across modern Linux distributions makes systemd-resolved the most likely candidate. You can verify its status by querying its state. If this service is active, flushing the cache involves interacting directly with the resolvectl or systemd-resolve command-line tools to manage the internal cache.
Check for nscd or other resolvers
In environments that do not utilize systemd, or on older server installations, nscd might be the service handling name caching. Alternatively, some distributions or containerized environments might have no local cache at all, relying solely on the DNS settings provided by the DHCP or cloud provider. Identifying the absence of a local daemon is just as important as identifying its presence.
Flushing systemd-resolved Cache
If your system uses systemd-resolved, the process is straightforward and requires only a single terminal command. This command communicates directly with the systemd daemon to instruct it to purge all negative and positive cache entries. Performing this action forces the system to query authoritative DNS servers for fresh records on the next lookup.
The resolvectl flush-caches command
The recommended method for clearing the cache is to use the resolvectl utility with the flush-caches option. This command is universally supported in distributions running systemd version 229 or later. It is a safe operation that does not disrupt network connectivity but rather optimizes future resolution attempts.
Commands for Alternative Scenarios
In the event that your system does not utilize systemd-resolved, or if you are working within a minimal container environment, alternative commands are available. These methods target the specific daemon managing the cache or, in the case of direct DNS configuration, simply involve restarting the networking stack to force a refresh of the resolver settings.
Using the nscd command
If your system relies on nscd for Name Service caching, the flush operation is handled through its init script or direct binary interaction. You must ensure the nscd service is running to execute the flush command successfully, as the daemon must be active to clear its internal memory structures.
Restarting the networking service
For systems without a dedicated caching daemon, or for users who prefer a more brute-force approach, restarting the networking service effectively clears the DNS configuration cache. This action reloads the resolver configuration from /etc/resolv.conf, ensuring that any changes to nameservers or search domains are applied immediately.