How to Flush DNS Cache on Linux?

DNS is responsible for resolving website names into their respective IPs. Whenever you visit a website, your operating system and web browser keep a record of the domain and the corresponding IP Address. The cache files contain information about all visited sites, including their hostname, IP Address, etc.

The cache files’ validity period is determined by Time to Live. As long as the cache files are valid, they will answer content requests without going through the DNS server. However, using outdated DNS cache files will lead to errors and security vulnerabilities. 

These are some reasons why you should flush your DNS frequently – 

  • Technical Issues – To force the operating system to search the updated DNS records to resolve the connection and display updated content.

  • Search Behaviour – Storing DNS records makes it easier for hackers to get your search history.

  • Security – DNS cache files are important and become the primary target for spoofing, which can risk a user’s sensitive information.

In Linux, no such OS-level DNS caching unless a caching service such as systemd-resolved, DNSMasq, or NSCD is installed and running.

  • Systemd Resolved


Follow the command below to check whether a service is running or not – 

sudo systemctl is-active systemd-resolved.service

If the service is running, it will show Active, or else it will show Inactive.

To clear a DNS cache, use the Systemd Resolved command.

sudo systemd-resolved --flush-caches

Note
If your service is active and the command gets executed successfully, it will return nothing. So, it means you have to flush the DNS cache.

  • DNSMasq – It is a lightweight DNS caching nameserver. If your system uses DNSMasq as a caching server, you will need to restart that service to flush the DNS cache.

sudo systemctl restart dnsmasq.service

        Or

sudo service dnsmasq restart

  • NSCD – It is the preferred DNS caching system. If you use this system, run the below command to flush the DNS cache.

sudo systemctl restart nscd.service

       Or

sudo service nscd restart

 


Was this answer helpful?

« Back

chat