Support > About cybersecurity > Linux Environment DNS Cache Cleanup Guide: Principles, Methods, and Practical Application
Linux Environment DNS Cache Cleanup Guide: Principles, Methods, and Practical Application
Time : 2025-11-11 15:33:25
Edit : Jtti

Basic operations on US VPS servers include DNS cache cleanup. Proper DNS cache management effectively resolves domain name resolution errors, improves network access efficiency, and is a crucial aspect of ensuring service stability.

A DNS cache can be viewed as a temporary database storing the results of recent domain name queries. When an application accesses a domain name, the system first queries the local cache. If a valid record exists, it directly returns the corresponding IP address, avoiding the waiting time of querying a remote DNS server. This mechanism significantly improves network response speed and reduces the load on external DNS queries. However, when the IP address corresponding to a domain name changes, the old record in the cache can cause access failures or redirects to incorrect addresses.

In Linux systems, DNS cache management methods vary depending on the distribution and network configuration. Mainstream caching services include systemd-resolved, nscd (Name Service Cache Daemon), and dnsmasq. Understanding the actual DNS resolution service running in the system is a prerequisite for choosing the correct cleanup method.

For modern Linux distributions using the systemd-resolved service (such as Ubuntu 18.04+, CentOS 8+), the most direct and effective way to clear the DNS cache is to use the systemd-resolve command. The following command will immediately clear the cache:

sudo systemd-resolve --flush-caches

This command will not produce any obvious output, but you can check the cache status using sudo systemd-resolve --statistics to confirm the clearing effect.

If the system uses the nscd service, you can clear the cache by restarting the service:

sudo systemctl restart nscd

Alternatively, you can use a dedicated cleanup command:

sudo nscd -i hosts

nscd caches various name service query results, including hostnames, users, and group information. Restarting the service will clear all cached entries at once.

For lightweight DNS forwarders and DHCP servers like dnsmasq, the common cleanup method is also to restart the service:

sudo systemctl restart dnsmasq

In some configurations, dnsmasq may run as a component of NetworkManager. In this case, restarting NetworkManager will also indirectly reset dnsmasq and the DNS cache.

Besides basic cleanup operations, there are some specific scenarios to be aware of. When the system is not running a dedicated DNS caching service, you can try restarting the network management service to refresh the DNS state:

sudo systemctl restart NetworkManager

After manually modifying the /etc/hosts file, cache cleanup is usually required for the changes to take effect. For systems using newer kernels, you can also try using the ip command to clean the route cache:

sudo ip -6 route flush cache
sudo ip -4 route flush cache

After performing the cleanup operation, you need to verify the effect. The most direct method is to use the nslookup or dig command to query the domain name that previously had caching issues:

nslookup example.com

or

dig example.com

Observe whether the returned IP address has been updated to the correct value. It's worth noting that some applications (such as web browsers) may maintain their own DNS caches. In such cases, clearing the application's cache simultaneously is necessary to completely resolve the issue.

DNS cache clearing is typically performed in the following situations: after a website migration and server IP address change, old records need to be cleared immediately; to resolve certain types of DNS hijacking or poisoning issues; during network troubleshooting, to rule out caching as a factor; and to ensure immediate effect after modifying local DNS resolution records (such as /etc/hosts).

While clearing the DNS cache is simple and effective, its potential impact should be considered. For a period after clearing the cache, the system needs to re-query the DNS server, which may cause a slight decrease in domain name resolution speed in the short term. When performing clearing operations on a production VPS, the potential impact on dependent services should be assessed. Additionally, some systems with strict security policies may log DNS cache clearing operations, which requires special attention during auditing.

For environments that require frequent DNS cache clearing, consider setting up a scheduled task to automatically perform the clearing operation, or writing an operations and maintenance script that integrates clearing functionality. Monitoring the system's DNS query failure rate can also help identify problems promptly and trigger cache clearing when necessary.

Understanding and mastering DNS cache cleanup methods on a US VPS server running Linux is an essential skill for every system administrator. By selecting appropriate cleanup strategies for different system environments and following best practices, network stability and access efficiency can be effectively maintained, providing reliable network infrastructure services for upper-layer applications.

Relevant contents

How to optimize Japanese servers for machine learning Sharing the process of smoothly migrating business to a VPS server, with a focus on risk control. How to resolve mobile website compatibility issues? Analysis and Solutions for Website Errors Indicating Untrusted SSL Certificate Methods to protect against root vulnerabilities in Japanese cloud server containers Analysis of the actual protection capabilities of 100G Hong Kong DDoS protected servers Several core methods for viewing port status in Debian system Analysis of methods for viewing virtual host databases Can the old DNS resolution be retained after a domain name is changed to a different DNS server? Common causes and solutions to DNS domain name resolution conflicts
Go back

24/7/365 support.We work when you work

Support