GrepBoard/Manuals
605 manuals ·

Flush DNS Cache on Linux: resolvectl flush-caches and More

·Tested on Ubuntu 24.04 (Docker) · systemd 255 systemd-resolved · dnsmasq 2.91 · 2026-10·Test scope ↓·6 steps

To flush DNS cache on Linux, find what caches first. On Ubuntu it's systemd-resolved: sudo resolvectl flush-caches. Flush dnsmasq, nscd and browsers separately.

Linux has no single "OS DNS cache". Caching is done by whatever program sits in the name resolution path — systemd-resolved, dnsmasq, nscd and so on. Ubuntu 17.10+ and Fedora 33+ enable systemd-resolved by default, so most of the time this one line is enough:

console
$ sudo resolvectl flush-caches

No output and exit code 0 means the cache was flushed. If you still get the old address, the cache is somewhere else, or an upstream resolver is holding the old value.

[1] Read nameserver in /etc/resolv.conf to see who answers.

nameserverWhat cachesHow to flush
127.0.0.53systemd-resolvedsudo resolvectl flush-caches
dnsmasq on 127.0.0.1 or similardnsmasqsudo kill -HUP <dnsmasq PID>, or restart the service
An external address (corporate DNS, router, 8.8.8.8)That serverCan't be flushed from this machine

If nscd is installed (systemctl status nscd), it caches hosts lookups too, regardless of nameserver; flush it separately ([4]).

[2] On systemd-resolved, look at what's cached first. The last line of resolvectl query tells you where the answer came from. Here the same name is queried twice:

console
$ resolvectl query example.comexample.com: 104.20.23.154                                  -- link: eth0…-- Information acquired via protocol DNS in 10.5ms.-- Data is authenticated: no; Data was acquired via local or encrypted transport: no-- Data from: network$ resolvectl query example.com…-- Information acquired via protocol DNS in 1.6ms.…-- Data from: cache

Data from: cache means the answer came from the cache. With systemd 254 or later you can dump the whole cache (Ubuntu 24.04 ships 255):

console
$ resolvectl show-cacheScope protocol=dnsexample.com IN A 172.66.147.243example.com IN A 104.20.23.154example.com IN AAAA 2606:4700:10::ac42:93f3example.com IN AAAA 2606:4700:10::6814:179a

[3] Flush and confirm with the numbers. When Current Cache Size in resolvectl statistics drops to 0, the cache is empty.

console
$ resolvectl statistics…Cache                                                                 Current Cache Size: 2…$ resolvectl flush-caches$ resolvectl statistics…                         Current Cache Size: 0

Running resolvectl query again now shows Data from: network. On a real server you may need sudo (the test ran as root in a container). Per the docs, flush-caches is mostly equivalent to sending SIGUSR2 to the service; the command is recommended because the signal is asynchronous. In the same container, the signal also took Current Cache Size from 2 to 0. The cache is also flushed automatically when the network configuration changes.

Older guides use systemd-resolve --flush-caches. The systemd-resolve command was not included in Ubuntu 24.04's systemd-resolved package:

console
$ ls -l /usr/bin/systemd-resolvels: cannot access '/usr/bin/systemd-resolve': No such file or directory

If you get systemd-resolve: command not found, use resolvectl instead.

[4] Flush dnsmasq and nscd their own way. Per its docs, dnsmasq clears its cache and rereads /etc/hosts when it receives SIGHUP. This is the query log (--log-queries) of a dnsmasq asked the same name, sent the signal, and asked again:

output
dnsmasq[573]: query[A] example.com from 127.0.0.1dnsmasq[573]: cached example.com is 172.66.147.243dnsmasq[573]: cached example.com is 104.20.23.154dnsmasq[573]: read /etc/hosts - 9 namesdnsmasq[573]: query[A] example.com from 127.0.0.1dnsmasq[573]: forwarded example.com to 1.1.1.1

Before the signal the answer was cached; after it (the read /etc/hosts line) dnsmasq forwarded the query upstream again. If several dnsmasq processes run, use pgrep -a -x dnsmasq to pick the PID of the one answering queries and send sudo kill -HUP <PID>.

For nscd, sudo nscd -i hosts invalidates just the hosts cache. The nscd docs say it keeps separate caches for found and not-found results, each with its own TTL, so it can also hide a newly created record. The TTLs are positive-time-to-live hosts and negative-time-to-live hosts in /etc/nscd.conf, in seconds.

[5] Check the browser separately. If curl and dig in a terminal get the new address but the browser still shows the old site, it's the browser's cache or an open connection. In Chromium-based browsers (Chrome, Edge), a widely used method is opening chrome://net-internals/#dns and clicking Clear host cache. If nothing changes, quit the browser completely and start it again.

[6] If everything is flushed and you still get the old value, the cache is outside this machine: a router, corporate DNS or public resolver holding it for the TTL. When a newly created name keeps coming back as nonexistent, New DNS record not resolving shows how to work out the time left from the SOA. If name resolution fails outright, see Temporary failure in name resolution.

Check it with the dig output explainer tool ↗ — calculated in your browser.

#dns#cache#resolvectl#systemd-resolved#dnsmasq#nscd