sodcalculator

10 Tech How-To

How to Flush DNS Cache in Linux (All Major Distros)

Guide 10-21574 · Updated · Step by step

Flushing the DNS cache on Linux clears the stored domain-name-to-IP-address mappings that your system has saved from previous lookups. This is useful after changing DNS servers, editing the hosts file, or troubleshooting “site not found” errors. The exact command depends on which DNS resolver your system uses, so this guide covers the most common ones.

What You’ll Need

  • A Linux system with sudo or root access (most flush commands require it).
  • A terminal window (Ctrl+Alt+T on most desktops).
  • Knowledge of which DNS resolver your system uses—we’ll show you how to check.

Step-by-Step

  1. Identify your DNS resolver. Most modern Linux distributions use one of these: systemd-resolved, dnsmasq, nscd, or the older BIND service. Run one of these commands to see what’s running:
    sudo systemctl status systemd-resolved
    sudo systemctl status dnsmasq
    sudo systemctl status nscd
    If you see an active service, that’s your resolver. If none are active, your system likely uses a simple caching stub or no local cache at all.
  2. Flush systemd-resolved (most common on Ubuntu, Debian, Fedora, and Arch). Run:
    sudo systemd-resolve --flush-caches
    On newer versions (systemd 239+), the command is:
    sudo resolvectl flush-caches
    If the first one says “Unknown command,” try the second. No output means success.
  3. Flush dnsmasq (used on many routers and some distros like OpenWrt or older Fedora). Restart the service:
    sudo systemctl restart dnsmasq
    This clears the cache completely. If you’re using dnsmasq as a local caching forwarder, this is the standard way.
  4. Flush nscd (Name Service Cache Daemon, common on RHEL/CentOS and some older systems). Run:
    sudo systemctl restart nscd
    Or use the provided script:
    sudo /etc/init.d/nscd restart
    This clears all cached name service entries.
  5. Flush BIND (if you run a local BIND server). Use rndc to flush the cache:
    sudo rndc flush
    If you have views defined, you may need to specify the view: sudo rndc flush view internal.
  6. Flush the browser cache (if the issue persists). Chrome/Edge: go to chrome://net-internals/#dns and click “Clear host cache.” Firefox: restart the browser or go to about:networking#dns and click “Clear DNS Cache.”

Common Mistakes

  • Using the wrong command for your resolver. Running systemd-resolve --flush-caches on a system without systemd-resolved will just return an error. Always check which service is running first.
  • Forgetting sudo. Most flush commands require root privileges. If you get a “Permission denied” error, run with sudo.
  • Assuming the browser uses the system cache. Some browsers (especially Chrome) maintain their own DNS cache. Even after flushing the system cache, you may need to clear the browser’s cache separately.
  • Restarting the wrong service. If you have both systemd-resolved and dnsmasq running (unusual but possible), restarting one won’t clear the other’s cache.

Tips

  • After flushing, test with nslookup example.com or dig example.com to confirm the new IP is being returned.
  • If you’re on a corporate or managed system, you might not have sudo rights—contact your IT admin.
  • For a quick one-liner that works on most systemd-based distros, use:
    sudo systemctl restart systemd-resolved (this also flushes the cache).
  • If you’re using a VPN, the VPN client may have its own DNS cache. Check its settings or reconnect to clear it.

Troubleshooting

  • “Failed to flush caches: Unit systemd-resolved.service not found.” Your system doesn’t use systemd-resolved. Check for dnsmasq or nscd as described in Step 1.
  • “Unknown command ‘flush-caches'” Your systemd version is older. Use sudo systemd-resolve --flush-caches instead.
  • “Operation not permitted” even with sudo. This can happen in containers or restricted environments. Try running sudo systemctl restart systemd-resolved instead.
  • Cache still not cleared after running the command. Verify the service is actually running: systemctl status systemd-resolved. If it’s inactive, there’s no cache to flush—the issue may be elsewhere (e.g., browser cache, network issue).
  • No command works. Some minimal distros don’t have a DNS cache at all—DNS lookups go directly to the configured servers. In that case, there’s nothing to flush; check your network settings instead.

Frequently Asked Questions

How often should I flush the DNS cache?

Only when you’re troubleshooting DNS issues or after changing DNS servers. There’s no need for regular flushing—the cache expires automatically based on TTL values.

Does flushing DNS cache speed up my internet?

No, it can actually slow things down temporarily because your system has to re-resolve every domain, which adds a few milliseconds per lookup. It’s for fixing issues, not performance.

Will flushing DNS cache log me out of websites?

No. DNS cache is separate from cookies and sessions. You’ll stay logged in, but the first visit to each site after flushing may take a second longer.

What’s the difference between systemd-resolved and dnsmasq?

Both are DNS resolvers that cache results. systemd-resolved is integrated into systemd and used by default on many distros. dnsmasq is a lightweight forwarder often used in routers or as a local cache. The flush command differs.

Does flushing DNS cache affect my hosts file?

No. The hosts file is read directly and isn’t cached. If you’ve edited /etc/hosts, changes take effect immediately without flushing.

Can I flush DNS cache without sudo?

In most cases, no. The commands require root privileges. Some desktop environments may allow a user to restart the resolver via a GUI, but terminal commands need sudo.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →