Skip to main content

Command Palette

Search for a command to run...

How to Flush DNS on Linux

Published
6 min readView as Markdown

Flushing the DNS cache on your Linux system can solve many internet connectivity problems. If you notice slow browsing, outdated website data, or issues accessing certain sites, clearing your DNS cache might help. In this article, I’ll guide you through the process of flushing DNS on Linux, no matter which distribution or DNS service you use.

You don’t need to be a Linux expert to follow along. I’ll explain everything step-by-step, including commands for popular Linux distros and different DNS services like systemd-resolved, dnsmasq, and nscd. By the end, you’ll know exactly how to clear your DNS cache and keep your internet running smoothly.

What Is DNS and Why Flush It?

DNS stands for Domain Name System. It translates website names like www.example.com into IP addresses that computers understand. Your system stores these translations in a DNS cache to speed up browsing.

Sometimes, this cache can get outdated or corrupted. That’s when you might see errors like “website not found” or load old versions of websites. Flushing the DNS cache forces your system to get fresh information from DNS servers.

Here’s why you might want to flush your DNS cache:

  • Fix slow or failed website loading
  • Clear outdated or incorrect DNS entries
  • Improve privacy by removing stored DNS queries
  • Troubleshoot network or DNS-related issues

How DNS Works on Linux Systems

Linux doesn’t have a single DNS caching system. Instead, it depends on the software your system uses. Common DNS caching services include:

  • systemd-resolved: Used by many modern Linux distros like Ubuntu and Fedora.
  • dnsmasq: A lightweight DNS forwarder often used in home routers or custom setups.
  • nscd (Name Service Cache Daemon): Older caching service still used in some distros.
  • BIND: A full DNS server that can also cache queries, mostly on servers.

Knowing which service your system uses helps you flush the DNS cache correctly.

How to Check Which DNS Service Your Linux Uses

Before flushing DNS, you need to identify the caching service running on your system. Here’s how:

  1. Check systemd-resolved status
    Run this command:

    systemctl is-active systemd-resolved
    

    If it returns active, your system uses systemd-resolved.

  2. Check for dnsmasq
    Run:

    pgrep dnsmasq
    

    If it returns a process ID, dnsmasq is running.

  3. Check for nscd
    Run:

    systemctl is-active nscd
    

    If active, nscd is your caching service.

  4. Check /etc/resolv.conf
    This file shows your DNS resolver configuration. It might give clues about your DNS setup.

How to Flush DNS Cache on Linux

Flushing DNS Cache with systemd-resolved

If your system uses systemd-resolved, flushing the DNS cache is simple.

Run this command with root privileges:

sudo systemd-resolve --flush-caches

You can verify the cache is cleared by checking statistics:

systemd-resolve --statistics

Look for “Current Cache Size” — it should be zero after flushing.

Flushing DNS Cache with dnsmasq

For systems running dnsmasq, restart the service to clear the cache.

Use this command:

sudo systemctl restart dnsmasq

If your system doesn’t use systemd, you can try:

sudo service dnsmasq restart

Restarting dnsmasq clears all cached DNS entries immediately.

Flushing DNS Cache with nscd

If nscd is your DNS caching daemon, flush the cache with:

sudo systemctl restart nscd

Or, if systemctl isn’t available:

sudo service nscd restart

You can also flush only the hosts cache with:

sudo nscd -i hosts

This command invalidates the hosts cache without restarting the whole service.

Flushing DNS Cache for BIND DNS Server

If you run a BIND DNS server, flush the cache using the rndc command:

sudo rndc flush

To flush a specific zone, use:

sudo rndc flushname example.com

This is mostly relevant for server administrators managing DNS servers.

Flushing DNS Cache Without a Caching Service

Some Linux systems don’t run a DNS caching service by default. In this case, there’s no local cache to flush. Your system queries DNS servers directly.

If you suspect stale DNS data, you can:

  • Restart your network manager:

    sudo systemctl restart NetworkManager
    
  • Restart your browser or clear its DNS cache (browsers like Chrome have their own DNS cache).

Additional Tips to Manage DNS on Linux

  • Clear browser DNS cache: Browsers like Chrome and Firefox cache DNS separately. Clear this cache if you still have issues after flushing system DNS.

  • Change DNS servers: Switching to public DNS servers like Google (8.8.8.8) or Cloudflare (1.1.1.1) can improve speed and reliability.

  • Check DNS settings: Use cat /etc/resolv.conf to see which DNS servers your system uses.

  • Use dig or nslookup: These tools help test DNS resolution and troubleshoot issues.

Common DNS Flush Commands Summary

DNS ServiceFlush CommandRestart Command
systemd-resolvedsudo systemd-resolve --flush-cachesN/A
dnsmasqN/Asudo systemctl restart dnsmasq
nscdN/Asudo systemctl restart nscd
BINDsudo rndc flushN/A

Troubleshooting DNS Issues After Flushing Cache

If flushing DNS doesn’t fix your problem, try these steps:

  • Restart your network:

    sudo systemctl restart NetworkManager
    
  • Check your internet connection: Make sure your network is working.

  • Try different DNS servers: Modify /etc/resolv.conf or your network settings.

  • Flush browser DNS cache: Browsers may still use old DNS data.

  • Check firewall settings: Sometimes firewalls block DNS traffic.

Conclusion

Flushing DNS on Linux is a straightforward way to fix many internet and network issues. Whether you use systemd-resolved, dnsmasq, or nscd, the commands to clear your DNS cache are simple and quick. Knowing which DNS service your system uses is key to flushing the cache correctly.

By following the steps in this guide, you can refresh your DNS cache anytime you face slow browsing or connectivity problems. Remember, clearing DNS cache is just one part of troubleshooting, but it often solves common issues fast. Keep these commands handy, and you’ll be ready to fix DNS problems on your Linux system with confidence.

FAQs

How do I know if my Linux system uses systemd-resolved?

Run systemctl is-active systemd-resolved. If it returns active, your system uses systemd-resolved for DNS caching.

Can I flush DNS cache without root access?

No, flushing DNS cache requires administrative privileges because it affects system services.

Does flushing DNS cache affect my internet speed?

Flushing DNS cache temporarily slows down browsing as your system fetches fresh DNS data, but it usually improves speed by fixing stale entries.

How often should I flush DNS cache on Linux?

Only flush DNS cache when you experience issues like slow loading or incorrect website data. Regular flushing isn’t necessary.

What if flushing DNS cache doesn’t fix my problem?

Try restarting your network manager, clearing browser cache, or changing DNS servers. If issues persist, check firewall or network settings.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts