Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Command

sudo resolvectl flush-caches

This clears the cache of systemd-resolved, the local resolver on Ubuntu and on other systems where it is enabled. It prints nothing when it succeeds. The next lookup for each name goes to the upstream DNS servers again.

Find Out Which Cache the Server Uses

Linux has no single system-wide DNS cache. Check what is running before you flush anything:

ls -l /etc/resolv.conf
cat /etc/resolv.conf
systemctl is-active systemd-resolved nscd dnsmasq

If /etc/resolv.conf lists nameserver 127.0.0.53, systemd-resolved caches. If it lists 127.0.0.1 and dnsmasq is active, dnsmasq caches. If it lists outside addresses and none of these services is active, the server has no local cache.

Check the Cache with resolvectl statistics

resolvectl statistics

The Cache section shows Current Cache Size, Cache Hits, and Cache Misses. Run it before and after flushing: the current cache size drops to zero and grows again as names are looked up. The hit and miss counters keep counting until you reset them with sudo resolvectl reset-statistics.

A Server with No Local Cache

On AlmaLinux and Rocky Linux, NetworkManager normally writes the upstream servers straight into /etc/resolv.conf, and Debian servers without the systemd-resolved package installed do the same. Each lookup then goes to those servers, and there is nothing on the server to flush. If you still see an old answer, it comes from the upstream resolver's cache, which keeps each record until its TTL runs out. Check the remaining time:

dig example.com A

The number before IN A in the answer is the TTL in seconds. Ask the domain's own nameserver to see the current record without any cache: dig example.com @resolver_ip, with the nameserver's address in place of resolver_ip.

nscd

The Name Service Cache Daemon can cache host lookups on some systems. Clear its hosts table:

sudo nscd -i hosts

The -i option invalidates one table; nscd fetches fresh answers on the next lookup. Check whether nscd caches hosts at all in /etc/nscd.conf, in the enable-cache hosts line.

dnsmasq

dnsmasq keeps its cache in memory, so a restart clears it:

sudo systemctl restart dnsmasq

Sending it the HUP signal, with sudo pkill -HUP dnsmasq, also clears the cache and rereads /etc/hosts without a full restart.

Caches Inside Applications

  • Nginx: a fixed host name in proxy_pass or in an upstream block is looked up when the configuration loads, unless it uses a variable with a resolver or the resolve parameter. After that name's address changes, run sudo nginx -t and reload Nginx.
  • Long-running applications: some runtimes and connection pools keep addresses for their own lifetime. Restart the service if it still connects to an old address.
  • The hosts file: a line in /etc/hosts overrides DNS and no flush removes it. Check with getent hosts example.com.

Caches Outside the Server

Flushing the server's cache does not touch the caches on your own side: your computer and your browser keep their own DNS caches, separate from the server, and a home or office router may cache answers too. Restarting the browser, or waiting for the record's TTL to run out, gets you a fresh answer there.

When Flushing Does Not Help

If lookups fail outright instead of returning an old answer, the resolver itself is the problem; see temporary failure in name resolution. If the name does not exist at all, see DNS_PROBE_FINISHED_NXDOMAIN, and for other browser lookup errors, ERR_NAME_NOT_RESOLVED.

Was this answer helpful?
Loading...