Flushing the DNS cache on a Linux machine is a fundamental troubleshooting step that resolves a surprising number of connectivity issues. When you browse the web, your system stores the IP addresses of visited domains in a local cache to speed up subsequent requests. However, when a website changes its hosting server or an IP becomes outdated, this cached data can cause errors, preventing you from accessing the correct resource. Learning how to manage this cache is essential for any system administrator or power user.
Understanding the DNS Cache Mechanism
Before executing commands, it is important to understand what happens under the hood. Unlike Windows, Linux does not rely on a single, universal service for DNS caching. Instead, the responsibility is often delegated to specific applications or daemons. If you are using a modern distribution with `systemd`, the `systemd-resolved` service typically handles the cache. Alternatively, if you have manually configured `dnsmasq` or are using a dedicated caching daemon like `nscd`, the location of the cache varies. Diagnosing the issue correctly ensures you flush the right component.
Checking Your Current DNS Configuration
Before flushing, you should verify which DNS service is active and confirm that the cache contains the records you expect. You can query the current status of the `systemd-resolved` service to see if it is operational. If your system does not use `systemd-resolved`, this command will reveal that the service is inactive, prompting you to investigate other methods. This diagnostic step prevents unnecessary command execution and helps narrow down the source of the problem.
Verify Service Status
Use the following command to check if the resolved service is managing your DNS:
Flushing with Systemd-resolved
On distributions utilizing `systemd-resolved`, the process is straightforward and requires elevated privileges. The `resolvectl` utility provides a clean interface to manage the cache. By running the flush command, you instruct the daemon to discard all negative and positive cache entries. This action forces the system to query the DNS servers again on the next request, ensuring you receive the most current records.
Execute the Flush Command
To clear the cache, open a terminal and enter the following command:
sudo resolvectl flush-caches You will be prompted for your password. Upon successful execution, the command returns no output, indicating that the cache has been cleared. You can immediately run the status command again to observe the counters resetting, confirming the operation took effect.
Handling Alternative DNS Caching Services
Not all Linux environments rely on `systemd-resolved`. Lightweight distributions or custom-configured servers might use `dnsmasq`, `nscd`, or even `pdnsd` for caching. In these scenarios, the generic `systemd` commands will fail silently. You must target the specific service process. Flushing these caches usually involves restarting the daemon process, which reloads the configuration and purges stored data from memory.
Restart Dnsmasq
If `dnsmasq` is handling your DNS, use your package manager to restart the service:
sudo systemctl restart dnsmasq Restart Nscd If you are using the Name Service Cache Daemon, use the following command: