
How to Flush DNS Cache on Linux (Ubuntu 24.04, Debian & RHEL)
Clean guide to flush DNS cache on Linux with resolvectl flush-caches (systemd-resolved), dnsmasq, and nscd with verification commands.
Need to flush the DNS cache on Linux? If your system uses systemd-resolved, run:
sudo resolvectl flush-cachesUse it after editing /etc/hosts, changing resolvers, or testing a migration when a hostname still resolves to an old IP. Need every platform in one place? See how to flush the DNS cache on Windows, macOS and Linux.
Linux DNS flush commands by distribution
| Linux Distribution / Resolver | Command | Status / Check |
|---|---|---|
| Ubuntu 22.04 / 24.04 (systemd-resolved) | sudo resolvectl flush-caches | resolvectl statistics |
| Older systemd-resolved | sudo systemd-resolve --flush-caches | systemd-resolve --statistics |
| Any system using nscd | sudo nscd -i hosts | sudo nscd -g |
| dnsmasq local service | sudo systemctl restart dnsmasq | journalctl -u dnsmasq |
| BIND (named) | sudo rndc flush | sudo rndc status |
How to flush DNS on Linux
To flush DNS on Linux, run the command for your resolver:
- systemd-resolved (most modern distros):
sudo resolvectl flush-caches. - Older systemd:
sudo systemd-resolve --flush-caches. - nscd:
sudo systemctl restart nscd(orsudo nscd -i hosts). - dnsmasq:
sudo systemctl restart dnsmasq. - Local BIND:
sudo rndc flush.
Then verify with resolvectl query myproject.test or getent hosts myproject.test.
Does Linux even cache DNS?
Plain glibc has no DNS cache, so editing /etc/hosts takes effect immediately. A cache exists only when you run a caching layer: systemd-resolved, nscd, dnsmasq, or a local resolver. If unsure which you use:
resolvectl status
systemctl is-active systemd-resolved nscd dnsmasqWhich resolver does your system use?
The right command depends on what sits between your applications and the DNS servers. Look at /etc/resolv.conf:
ls -l /etc/resolv.conf
cat /etc/resolv.conf| What you see | What it means | Flush with |
|---|---|---|
nameserver 127.0.0.53 | systemd-resolved is the local stub | sudo resolvectl flush-caches |
nameserver 127.0.0.1 | A local resolver such as dnsmasq runs on the machine | Restart that service |
| A router or ISP address | No local cache on this machine | Nothing to flush locally |
Fedora uses systemd-resolved by default, like current Ubuntu. If NetworkManager runs its own dnsmasq (dns=dnsmasq in its configuration), restart NetworkManager with sudo systemctl restart NetworkManager. Restarting systemd-resolved also clears its cache, but resolvectl flush-caches does the same without interrupting resolution.
When to flush
- You edited
/etc/hostsand a tool still resolves the old IP. - You changed
/etc/resolv.confor your DNS server. - You moved a staging site to a new server before propagation.
For editing the file itself, see how to edit the hosts file on Ubuntu and Linux. If your /etc/hosts change does nothing, the cache is rarely the cause: see /etc/hosts not working.
Confirm the flush worked
With systemd-resolved, compare the cache counters before and after:
resolvectl statistics
sudo resolvectl flush-caches
resolvectl statisticsAfter the flush, the line Current Cache Size should drop to 0. The next lookup fills the cache again with fresh answers.
Verify the result
resolvectl query myproject.test # through systemd-resolved, /etc/hosts included
getent hosts myproject.test # the full name-service stack, /etc/hosts included
dig example.com # DNS servers only, never reads /etc/hostsgetent hosts reflects the full name-service stack, including /etc/hosts. Use dig only to test a real DNS record, because it skips the hosts file.
Caches that survive a flush
When the old answer persists after a successful flush, the stale copy lives in the application:
- Browsers keep their own DNS cache. In Chrome or Edge open
chrome://net-internals/#dns(oredge://net-internals/#dns) and clear the host cache. In Firefox openabout:networking#dns. - Long-running servers may resolve a name once and keep the address. Nginx, for example, resolves the hostnames in
proxy_passwhen it loads its configuration, so runsudo nginx -s reloadafter a change. - JVM and Node.js processes can hold addresses they already looked up. Restart the process.
- Containers and WSL use their own resolver and hosts file. Restart the container, or flush inside the distribution.
Troubleshooting
- resolvectl not found: your system may not use systemd-resolved; use the nscd/dnsmasq command instead.
- Still cached in the browser: clear the browser DNS cache and retry in a new tab.
- WSL2: flush on the Windows host too with
ipconfig /flushdns.
One command for any setup
If you work on many machines, this snippet flushes whichever cache is active:
if systemctl is-active --quiet systemd-resolved; then
sudo resolvectl flush-caches
elif systemctl is-active --quiet nscd; then
sudo nscd -i hosts
elif systemctl is-active --quiet dnsmasq; then
sudo systemctl restart dnsmasq
else
echo "No local DNS cache is running."
fiIf you manage many local domains across machines, ToggleHosts keeps hosts entries organized and consistent across Linux, macOS and Windows. Best hosts file editors for Linux compares it with SwitchHosts, hostctl and sudoedit.
_Last tested: June 2026 on Ubuntu 24.04, Fedora 40 and Debian 12._
Sources and further reading
Frequently Asked Questions
Run sudo resolvectl flush-caches (older systems: sudo systemd-resolve --flush-caches).
Not always. Plain glibc has no cache. Caching exists only if you run systemd-resolved, nscd, dnsmasq, or a local BIND resolver.
Restart the service: sudo systemctl restart dnsmasq (or send SIGHUP to the dnsmasq process).
Check /etc/resolv.conf. A nameserver of 127.0.0.53 means systemd-resolved, 127.0.0.1 usually means a local resolver such as dnsmasq, and a router address means there is no local cache to flush.
Related Articles
Ubuntu Hosts File: Location and How to Edit /etc/hosts (2026)Editing the hosts file
Ubuntu Hosts File: Location and How to Edit /etc/hosts (2026)
8 min read
How to Flush DNS Cache on Windows, macOS & Linux (2026)Flushing DNS & cache
How to Flush DNS Cache on Windows, macOS & Linux (2026)
8 min read
Flush DNS on macOS Sonoma (14): Exact Command (2026)Flushing DNS & cache
Flush DNS on macOS Sonoma (14): Exact Command (2026)
2 min read