Clear DNS Cache on Ubuntu

Clear DNS Cache on Ubuntu

A hostname change can leave your Ubuntu computer using an earlier address. Ubuntu’s systemd-resolved service can store DNS answers in a local cache, and resolvectl can clear that cache.

I’ll show how to check which resolver is active and flush systemd-resolved’s local cache.

TL;DR

On Ubuntu using systemd-resolved, run sudo resolvectl flush-caches to clear its local DNS cache.

  • Check the resolver with systemctl is-active systemd-resolved.
  • Use resolvectl statistics to inspect its cache counters.
  • Ubuntu 18.04-era instructions may use systemd-resolve –flush-caches.
  • A local flush cannot clear a browser, router or upstream DNS cache.

What is a DNS cache on Ubuntu?

A DNS cache is a short-lived store of answers to domain-name lookups. A resolver uses a cached answer instead of asking another DNS server again while that answer remains valid.

When an application asks for a name such as example.com, the system resolver finds an address using its configured DNS servers. Ubuntu’s systemd-resolved can keep DNS resource records locally, then return a cached record to later requests.

Each DNS record has a time to live, or TTL. That value limits how long a resolver should keep the answer. The local lifetime is separate from the cache held by your router, internet provider or another recursive DNS service.

Flushing removes records from the resolver’s local cache so later lookups can request them again. It does not edit the DNS record at its source, change the configured DNS server or force another organization’s resolver to discard its own cached copy.

Ubuntu 18.04 and newer commonly use systemd-resolved, but an installation can be configured differently or run another cache such as dnsmasq or nscd. I check the running service before choosing a flush command because a command for a service that is absent cannot clear its cache. The service check narrows the next step.

LayerWhat it storesWhat a local flush affects
systemd-resolvedDNS resource records for local lookupsYes, when this service handles the lookup
BrowserApplication-specific name or connection dataNo, use the browser’s own controls
Router or recursive DNS serverAnswers shared across clientsNo, its operator or expiry controls that cache

This separation matters because the same hostname can pass through several caches before an application connects. A local flush only removes the entries held by the resolver that received the command.

If the issue is an incorrect resolver address rather than a stale answer, see the LinuxForDevices guide to changing DNS on Linux. The Ubuntu network configuration guide explains how resolver settings fit into the active network interface.

How to clear the DNS cache on Ubuntu step by step

Use resolvectl when systemd-resolved is active. The command asks that resolver to flush its locally maintained DNS records without restarting the service.

You need a terminal account with permission to run administrative commands. I use the resolver’s own control command rather than restarting networking, since a cache flush does not require taking the network connection down.

Step 1: Check which resolver is active

Check whether the Ubuntu system resolver is running before flushing it. This service check reports whether systemd-resolved is active. It does not identify every possible application-level or network-device cache.

systemctl is-active systemd-resolved

An active response means this is the service to address with resolvectl. If it reports inactive or the unit is missing, do not assume a successful command against systemd-resolved will clear the cache used by your applications.

You can also inspect the resolver’s DNS configuration with resolvectl status. On systems using systemd-resolved, the output describes the global and per-link DNS settings. That helps distinguish the local resolver from the DNS server it forwards requests to.

resolvectl status

For general service checks, the LinuxForDevices reference to listing services on Ubuntu covers other ways to inspect systemd units.

Step 2: Check the current cache statistics

Read the statistics before the flush if you want a comparison. The resolver reports counters such as the current cache size, cache hits and cache misses. The current size changes as new names are resolved.

resolvectl statistics

Record the current cache size, then check it again after flushing. A zero size immediately after the command is consistent with an empty resolver cache. A later name lookup can add new records again.

These statistics describe systemd-resolved only. Browser entries and router or recursive-server caches are separate.

Step 3: Flush the local DNS cache

Run the flush command with administrator privileges. This is the specific systemd-resolved operation for removing its DNS resource-record cache.

sudo resolvectl flush-caches

The command is silent on success. The captured invocation exited 0. No command output appeared:

pankaj@linuxfordevices:~$ sudo resolvectl flush-caches

[exit 0]
Terminal running systemd-resolve --flush-caches on Ubuntu 18.04

The existing terminal image shows the older systemd-resolve –flush-caches command returning to the prompt without a message. That is the compatibility spelling used in Ubuntu 18.04-era instructions.

Newer releases use resolvectl. Match the command to the system’s installed tool, since the two names are not both present everywhere.

Terminal running sudo resolvectl flush-caches with exit status 0

The captured command returned exit status 0 without extra output. That confirms this command invocation succeeded on a system where systemd-resolved is active. It does not prove that a separate cache elsewhere has been cleared.

Step 4: Check the cache and resolve the name again

Read the statistics again, then make a fresh lookup for the affected hostname. The statistics show the resolver’s current counters. The query checks whether the system can resolve that name now.

resolvectl statistics
resolvectl query example.com

Replace example.com with the hostname that failed. A successful query means a resolver returned an answer, not necessarily that it is the new address you expected. Compare the returned address with the authoritative or expected value for your own domain.

The captured flush command exited with status 0. On your Ubuntu system, read the statistics after flushing and compare the current cache size. New lookups can add records again after the cache has been cleared.

What if the DNS answer is still old?

A successful local flush clears only the local resolver’s cache. Check which layer still returns the old address before repeating the command, because routers and upstream recursive DNS servers retain separate copies.

If resolvectl is missing, check the Ubuntu release and available command names. Ubuntu 18.04 commonly uses systemd-resolve –flush-caches. Current Ubuntu documentation uses resolvectl flush-caches.

The error systemd-resolve: command not found means that executable is unavailable under that name. Check which resolver is active before trying another service command.

If systemd-resolved is inactive, another service may handle caching. Check whether nscd or dnsmasq is installed and active before using that service’s documented flush or restart operation. Restart a daemon only after identifying it as the resolver used by the machine.

For a systemd-resolved service restart, use sudo systemctl restart systemd-resolved only when a service restart is actually needed. Restarting briefly interrupts the resolver, while resolvectl flush-caches is the narrower cache operation. The LinuxForDevices guides to restarting Ubuntu networking and managing Linux services cover those separate tasks.

A browser can keep its own DNS or connection data after the operating-system resolver is flushed. Close and reopen the affected browser or use its documented network-cache controls when the command-line lookup returns the expected address but the browser still reaches the old destination.

A router or upstream recursive resolver can also return an old answer until its cached TTL expires. Clearing Ubuntu’s local cache starts another lookup through the configured DNS path. The remote resolvers keep their own caches until their records expire or their operators clear them.

Entries in /etc/hosts supply local name mappings to the system name-service stack. Inspect the file when a hostname resolves to a local address after a DNS cache flush. The network configuration guide is the next step when the resolver or interface DNS settings themselves need correction.

When a name resolves but the server still cannot be reached, the fault may be routing or connectivity rather than DNS. The LinuxForDevices guide to using traceroute on Linux can help inspect the network path after name resolution is working.

What you observeWhat to check next
systemd-resolved is active and the local cache size dropsResolve the affected name again and compare its address
systemd-resolved is inactiveIdentify the resolver your applications actually use
Terminal lookup is current but browser is notCheck the browser’s separate cache or connection state
Different clients still receive an old addressCheck the configured router or upstream resolver and its TTL
Name resolves but connection failsCheck routing and reachability after DNS is confirmed

This comparison keeps the check tied to the layer that owns the answer. A local flush leaves an upstream cache unchanged, and a different DNS server cannot repair a route that fails to reach the destination.

When you compare answers, use the same hostname and record type each time. An IPv4 address lookup and an IPv6 lookup can return different records, and a site can publish more than one address. Confirm which record the application requested before calling a different answer stale.

A DNS record’s TTL describes how long a DNS cache may retain that record before it should ask again. Clearing systemd-resolved removes its local copy immediately, but the configured recursive server can still return its own cached record until that server’s TTL expires. Only the operator of that remote resolver can clear its cache directly.

For a changed public record, compare the answer from the machine with the value published by the domain’s authoritative DNS provider. A local result that differs from the authoritative value points toward a resolver between your computer and that provider. If both values match but the application still fails, DNS may no longer be the failing layer.

Flushing is a troubleshooting action, not a routine maintenance command. The cache exists to reuse valid answers. Clear it when testing a change or isolating a name-resolution issue, then allow it to repopulate as applications make new requests.

Common errors when clearing the DNS cache

A flush only affects the resolver that handles local lookups. When an old answer remains, check which layer still returns it before repeating the command.

SymptomNext check
resolvectl is missingCheck whether the installed system uses systemd-resolved or the older systemd-resolve name
systemd-resolved is inactiveIdentify the resolver service actually handling requests
Terminal lookup is correct but a browser is notCheck the browser’s own DNS or connection cache
Other clients still receive the old addressCheck the router or upstream resolver and its record TTL

For an unexpected local address, inspect /etc/hosts, which can supply a mapping independently of the resolver cache, and check routing instead of flushing DNS again when the answer is correct but the connection still fails.

Conclusion: flush the local resolver cache

On Ubuntu systems using systemd-resolved, sudo resolvectl flush-caches clears the resolver’s local DNS records. Check the service first, then compare its statistics and resolve the hostname again.

If the old address remains, trace the next cache or configuration layer rather than assuming the local flush changes upstream DNS. For resolver changes, continue with the Linux DNS configuration guide.

FAQ

The right Ubuntu flush command depends on which resolver is active.

How do I clear the DNS cache on Ubuntu?

If systemd-resolved is active, run sudo resolvectl flush-caches. Ubuntu 18.04-era systems may use sudo systemd-resolve –flush-caches.

Why does systemd-resolve say command not found?

The systemd-resolve executable is unavailable under that name on your installation. Check the release and use resolvectl flush-caches when that command is installed and systemd-resolved is active.

Does flushing Ubuntu DNS clear my router’s cache?

No. The command clears the local systemd-resolved cache only. A router or upstream recursive DNS resolver may retain its answer until its own cache expires.