User Agents

Web browsers and everyday web tech, explained

How to Flush DNS Cache on Linux (systemd-resolved, dnsmasq, nscd)

· 4 min read · Cookies and Cache

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Flushing the DNS cache on Linux clears stored domain-name-to-IP mappings so your system re-queries the DNS server for fresh results. This is useful after changing a website's IP address, testing DNS changes, or when you see stale 'connection refused' errors. The exact command depends on which caching service your distribution uses — most modern systems use systemd-resolved, but some use dnsmasq or nscd. This guide covers all three.

What You'll Need

  • Terminal access (Ctrl+Alt+T on most desktop distros)
  • sudo privileges (you'll be prompted for your password)
  • Knowledge of which DNS resolver your system uses — we'll show you how to check

Step-by-Step

  1. Identify your DNS caching service. Run this command in the terminal:
    ps aux | grep -E 'systemd-resolved|dnsmasq|nscd' | grep -v grep
    If you see systemd-resolved, go to Step 2. If you see dnsmasq, go to Step 3. If you see nscd, go to Step 4. If nothing appears, your system may not cache DNS at all — in that case, no flush is needed.
  2. Flush systemd-resolved cache. Use the following command:
    sudo resolvectl flush-caches
    On older systems, you might need sudo systemd-resolve --flush-caches instead. No output means success.
  3. Flush dnsmasq cache. Restart the dnsmasq service:
    sudo systemctl restart dnsmasq
    This clears the cache because dnsmasq is stateless. If you're using a local dnsmasq instance, you can also send a SIGHUP signal: sudo killall -HUP dnsmasq.
  4. Flush nscd cache. Restart the nscd service:
    sudo systemctl restart nscd
    Alternatively, you can clear only the hosts cache: sudo nscd -i hosts.
  5. Verify the flush. After flushing, query a domain you recently changed (or just any domain) to see if it resolves to the new IP. Use dig example.com or getent hosts example.com. If you're testing a specific site, replace example.com with the actual domain.

Common Mistakes

  • Using the wrong command for your service. Running resolvectl on a system that uses dnsmasq won't work. Always check first.
  • Forgetting sudo. Most flush commands require root privileges. If you get a 'permission denied' error, prefix with sudo.
  • Assuming a flush fixes all network issues. DNS cache is only one layer. If the problem persists, check your network connection, DNS server configuration, or the site's status.
  • Restarting the wrong service. On some systems, both systemd-resolved and dnsmasq run simultaneously. Flushing one may not affect the other.

Tips

  • Check your DNS server first. If you're seeing stale results, verify that your DNS server (e.g., 8.8.8.8 or your router) actually has the updated record using dig @8.8.8.8 example.com.
  • Flush after changing your /etc/hosts file. The hosts file is not cached by systemd-resolved, but nscd and dnsmasq may cache it. Restart the service after editing.
  • Use a single command for systemd-resolved. resolvectl flush-caches also clears the negative cache (failed lookups), which is handy after fixing a downed server.
  • Set a shorter TTL for testing. If you control the DNS records, temporarily lower the TTL (time-to-live) to 60 seconds before making changes, so caches expire quickly.

Troubleshooting

Command not found: If resolvectl isn't available, your system likely uses an older version of systemd. Try systemd-resolve --flush-caches. If that fails, install systemd-resolved or switch to another method.

No service found but DNS still seems cached: Some network managers (like NetworkManager) have their own DNS caching. Restart NetworkManager: sudo systemctl restart NetworkManager. This is a broader reset but effective.

Flush worked but site still shows old IP: The cache might be on your router, your ISP, or a public DNS server. Try flushing again after a few minutes, or use a different DNS server temporarily (edit /etc/resolv.conf or your network settings).

Permission denied even with sudo: If you're in a container or restricted environment, you may not have permission to restart system services. In that case, you can only clear the cache for your own user by logging out and back in, or using killall -HUP if the service runs in user space.

Frequently Asked Questions

How often should I flush DNS cache on Linux?

Rarely. Only when you suspect stale data, after changing DNS records, or when troubleshooting connectivity. Flushing too often can slow down name resolution slightly because the cache is empty.

Does flushing DNS cache affect my internet speed?

Only temporarily. The first few lookups after flushing may take a few milliseconds longer, but the cache repopulates quickly. No lasting impact.

Can I flush DNS cache without sudo?

No, not on standard system-wide services. The commands require root to modify system state. You can, however, clear your browser's DNS cache separately without sudo.

What's the difference between flushing DNS and clearing browser cache?

DNS cache stores IP addresses. Browser cache stores web page files (HTML, images). They're unrelated. Flushing DNS won't clear your browser's cached pages, and vice versa.

Is it safe to restart dnsmasq or nscd?

Yes. These services are designed to be restarted without breaking network connectivity. There may be a brief moment (milliseconds) where a DNS query fails, but it's harmless.

Why does my Linux system not have any DNS cache?

Some minimal installations or servers don't run a caching service by default. DNS queries go directly to the configured nameserver each time. That's fine — you just don't need to flush anything.