How to Migrate cPanel Accounts Between Servers Using WHM Transfer Tool (Zero-Downtime Guide) Moving live…
Flush DNS Cache Linux: Clear and Troubleshoot DNS

Flush DNS Cache Linux: Clear Cached Records and Troubleshoot DNS Issues
To flush DNS cache Linux systems, you first need to identify which service is caching DNS responses. Linux servers may use systemd-resolved, nscd, dnsmasq, or a separate DNS caching service. Some systems do not maintain a local DNS cache at all.
DNS caching improves performance by storing recently resolved domain names and their records. However, after changing an A record, AAAA record, nameserver, or other DNS setting, cached responses can make a domain appear to point to an old IP address.
In this guide, you will learn how to flush DNS cache on Linux, clear caches maintained by common resolver services, troubleshoot stale DNS records, verify DNS changes with dig, and determine whether the remaining cache is local or controlled by an upstream DNS resolver.
What Is DNS Cache in Linux?
A DNS cache stores the results of previous DNS queries so that subsequent requests can be answered without repeating the entire DNS lookup process.
For example, when a Linux application resolves example.com, a DNS resolver may cache the returned IP address for a period determined by the record’s time to live (TTL).
DNS caching can happen at several levels:
- Local resolver cache: A service such as
systemd-resolvedcaches DNS responses on the Linux machine. - Caching daemon: Services such as
nscdordnsmasqmay cache hostname or DNS lookup results. - Network DNS resolver: A router, hosting provider, or public DNS service may cache responses.
- Application cache: Some applications maintain their own DNS cache.
- Browser or operating system cache: A user’s browser or client device may retain DNS information independently of the server.
These caches are separate. Flushing the local Linux resolver does not automatically clear a public DNS resolver’s cache or a browser’s internal cache.
When Should You Flush DNS Cache on Linux?
Clearing a local DNS cache can help when a system continues using a cached response that no longer reflects the expected DNS configuration.
Common situations include:
- A domain has been moved to a new server IP address.
- An A or AAAA record has recently changed.
- A website migration appears incomplete from one machine.
- A local resolver returns an outdated result.
- DNS troubleshooting shows different answers from different resolvers.
- An application continues resolving a hostname to an unexpected address.
- A caching service is suspected of retaining an unwanted DNS response.
However, not every DNS problem is caused by caching. Incorrect nameserver settings, failed DNS queries, broken delegation, DNSSEC errors, and network connectivity issues can produce similar symptoms.
Before flushing anything, compare the answers returned by the local resolver and a separate DNS server.
Step 1: Check the Current DNS Resolution
Start by checking which IP address the Linux system returns for the affected domain.
Use getent to test the system’s hostname resolution mechanism:
getent ahosts example.com
Replace example.com with your affected domain.
Next, use dig to inspect the DNS response:
dig example.com A
For IPv6 records, run:
dig example.com AAAA
Look at the ANSWER SECTION to identify the returned records. The status field also helps identify errors such as NXDOMAIN or SERVFAIL.
To display a more concise result, use:
dig +short example.com A
If the local result differs from what you expect, compare it with a public resolver:
dig @1.1.1.1 example.com A
You can also test Google Public DNS:
dig @8.8.8.8 example.com A
Interpret the results carefully:
- If the default query returns an old address but a direct query returns the new address, the local resolver configuration or another DNS path may be responsible.
- If both return the old address, an upstream cache, authoritative DNS configuration, or propagation delay may be involved.
- If the local query fails but direct queries work, investigate the configured resolver and its service status.
- If the responses contain different records, check the TTL, authoritative nameservers, and whether the queries reach the same DNS view.
Direct queries to public resolvers are diagnostic tests. Your hosting provider or private network may require a specific DNS resolver, and external DNS traffic may be restricted.
Step 2: Flush DNS Cache with systemd-resolved
systemd-resolved is a DNS resolution service used by many modern Linux distributions, including common Ubuntu installations. When this service is running and caching responses, you can clear its cache using resolvectl.
First, check whether the service is active:
systemctl is-active systemd-resolved
Inspect the current resolver configuration:
resolvectl status
To flush the local DNS cache, run:
sudo resolvectl flush-caches
The command clears cached DNS resource records maintained by systemd-resolved. It does not clear caches on remote DNS servers or in applications that maintain their own cache.
Check resolver statistics, if supported by your installed version:
resolvectl statistics
Then test the domain again:
resolvectl query example.com
You can also repeat the original DNS lookup:
dig example.com A
If the result remains unchanged, determine whether the new answer is available from the authoritative DNS servers and whether the query is being handled by a different resolver.
Should you restart systemd-resolved?
Restarting the service is usually unnecessary when your goal is simply to flush its cache. Use resolvectl flush-caches first.
If the service is malfunctioning, inspect its status and logs:
systemctl status systemd-resolved
sudo journalctl -u systemd-resolved -n 100 --no-pager
A service restart should be considered only when troubleshooting indicates a service problem. On a production VPS, be aware that restarting DNS services may temporarily affect name resolution for applications.
Step 3: Clear DNS Cache on Ubuntu and Debian
Ubuntu and Debian systems can use different DNS configurations depending on their release, installation method, and network manager.
Check the resolver configuration:
cat /etc/resolv.conf
Inspect whether the file is a symbolic link:
ls -l /etc/resolv.conf
If the configuration uses the local systemd-resolved stub address, check the service:
systemctl status systemd-resolved
When systemd-resolved is managing DNS, use:
sudo resolvectl flush-caches
If the machine does not use systemd-resolved, do not assume that this command will clear its DNS cache. Identify the service that actually handles DNS resolution.
Check which resolver services are running:
systemctl --type=service --state=running | grep -E 'resolved|nscd|dnsmasq|named'
This is a quick diagnostic, not a complete inventory of every possible resolver. A service may use a different name or run outside systemd.
Avoid replacing /etc/resolv.conf or manually adding public nameservers just to flush a cache. The file may be managed by NetworkManager, DHCP, Netplan, or another networking component.
Step 4: Flush the nscd Cache
The Name Service Cache Daemon (nscd) can cache information used by applications for hostname and other name-service lookups.
First, check whether it is installed and running:
systemctl status nscd
If your system uses nscd, you can invalidate its cached host entries with:
sudo nscd -i hosts
This invalidates the hosts cache managed by nscd. It is not a universal command for every DNS caching service, and it does not flush upstream DNS resolvers.
If the command is unavailable, nscd may not be installed. Do not install it solely to flush DNS cache without first determining whether your system needs that service.
After invalidating the cache, test the hostname again:
getent hosts example.com
Then compare the result with:
dig example.com A
The two commands may use different resolution paths, depending on the system’s name-service configuration and the tools installed.
Step 5: Restart dnsmasq When It Is Caching DNS
dnsmasq is a lightweight DNS forwarder and DHCP service commonly used on local networks, virtual machines, and some Linux server configurations.
Check whether it is running:
systemctl status dnsmasq
If dnsmasq is actively providing DNS caching for your system, restarting it can clear its in-memory cache:
sudo systemctl restart dnsmasq
However, restarting dnsmasq may interrupt DNS forwarding or DHCP-related services, depending on how it is configured.
On a production server, inspect the service configuration and dependencies before restarting it. If the service provides DNS for other systems, coordinate the change to avoid unnecessary disruption.
After the restart, test the configured resolver:
dig example.com A
If dnsmasq forwards requests to another caching DNS server, that upstream server may still return its own cached response.
Step 6: Flush Cache on a BIND DNS Server
Some Linux servers run BIND (named) as a recursive or authoritative DNS server. If BIND is configured to provide recursive caching, you can use its control utility to clear cached records.
Check whether the service is running:
systemctl status named
On some Debian-based systems, the service may be named bind9 instead:
systemctl status bind9
If you administer a BIND server with the appropriate permissions and control configuration, flush its cache with:
sudo rndc flush
This command is for a BIND server you administer, not for an ordinary Linux client that simply uses a remote DNS resolver.
A cache flush on a recursive BIND server does not change the authoritative DNS records for your domain. It only affects the cache maintained by that server.
If you operate an authoritative-only DNS server, flushing its cache may not be relevant to the issue you are troubleshooting.
Step 7: Understand DNS TTL and Propagation
One of the most common misunderstandings in DNS troubleshooting is assuming that every DNS cache can be cleared from your Linux server.
Each DNS record has a TTL that tells caching resolvers how long the record can generally be retained. After changing a record, other resolvers may continue returning the previous response until their cached entries expire or are otherwise refreshed.
For example, suppose a DNS record previously pointed to:
example.com. 3600 IN A 203.0.113.10
The TTL shown is 3600 seconds, or one hour. This is an illustrative example, not a recommended value for every domain.
If the record changes to a new IP address, a resolver that cached the old record may continue using it until its cached copy expires. Flushing your local cache cannot force every external resolver to discard its cached record.
To inspect a record’s TTL:
dig example.com A
Review the TTL value shown in the answer section. If you need to determine which server is providing an outdated response, query the relevant resolver directly and inspect the domain’s authoritative nameservers.
Keep in mind that a low TTL does not guarantee that every client will immediately see a DNS change. Applications, browsers, operating systems, and intermediary resolvers may have their own caching behavior.
Step 8: Verify DNS Changes with Authoritative Queries
If a DNS record was recently changed, verify that the authoritative DNS servers are returning the intended value.
First, inspect the domain’s nameservers:
dig example.com NS
Then query a specific authoritative nameserver directly:
dig @ns1.example.net example.com A
Replace ns1.example.net with an actual authoritative nameserver for the domain.
To investigate the DNS delegation path, run:
dig +trace example.com
A trace can help identify where an unexpected answer or delegation problem appears. However, it may be affected by network restrictions and does not replace checking the DNS provider’s zone configuration.
For a domain you control, verify:
- The A and AAAA records contain the intended addresses.
- The registered nameservers point to the intended DNS provider.
- The authoritative DNS zone has been updated correctly.
- DNSSEC configuration is valid if DNSSEC is enabled.
- The record’s TTL is understood before interpreting cached responses.
If the authoritative server still returns the old address, the issue is not solved by flushing your local Linux cache. Correct the authoritative DNS configuration first.
Step 9: Troubleshoot DNS Cache Problems That Persist
If flushing the cache does not fix the issue, use a structured diagnostic process instead of repeatedly restarting services.
Check the local resolver
cat /etc/resolv.conf
resolvectl status
Use the second command when systemd-resolved is available.
Compare local and remote answers
dig example.com A
dig @1.1.1.1 example.com A
If the answers differ, identify which resolver is returning the unexpected result.
Check whether the problem is limited to one domain
getent hosts example.com
getent hosts another-example.net
If many unrelated domains fail, the problem may involve the local resolver or network connectivity rather than a single cached record.
Review resolver logs
For systemd-resolved:
sudo journalctl -u systemd-resolved -n 100 --no-pager
For dnsmasq, if it is managed by systemd:
sudo journalctl -u dnsmasq -n 100 --no-pager
Review errors and timestamps around the time the DNS problem occurred.
Test application connectivity
Once DNS resolution returns the expected address, test the actual application:
curl -I --connect-timeout 10 https://example.com
A correct DNS answer does not guarantee that the destination web server, firewall, TLS configuration, or application is working. If DNS is correct but the connection still fails, troubleshoot those components separately.
Common DNS Cache Troubleshooting Commands
| Command | Purpose |
|---|---|
resolvectl flush-caches |
Flush the local systemd-resolved cache |
resolvectl status |
Inspect resolver configuration |
resolvectl statistics |
View resolver statistics when supported |
nscd -i hosts |
Invalidate the nscd hosts cache |
systemctl restart dnsmasq |
Restart dnsmasq and clear its in-memory cache |
rndc flush |
Flush the cache on a managed BIND server |
dig example.com A |
Inspect an A record and its TTL |
dig @1.1.1.1 example.com A |
Query a specific DNS resolver |
dig example.com NS |
Inspect the domain’s nameserver records |
dig +trace example.com |
Trace DNS delegation |
Use only the commands relevant to services running on your system. They are not interchangeable, and some require administrative privileges.
Frequently Asked Questions
How do I flush DNS cache on Linux?
First identify the DNS caching service. For systems using systemd-resolved, run sudo resolvectl flush-caches. If the system uses nscd, dnsmasq, or BIND, use the appropriate method for that service.
Does Linux always cache DNS records?
No. DNS caching depends on the system configuration. Some Linux systems use systemd-resolved, nscd, or another caching daemon, while others rely on a remote DNS resolver without maintaining a local DNS cache.
Will flushing the DNS cache immediately update my domain’s IP address?
It can remove a stale local cache entry, but it does not update authoritative DNS records or clear caches on remote resolvers. Confirm that the authoritative record is correct and check the TTL when results differ between resolvers.
Why does dig show the new IP address but my application still uses the old one?
The application may use a different resolver path or maintain its own cache. Compare dig with getent hosts, inspect the system resolver configuration, and review the application’s DNS caching behavior.
Is restarting systemd-resolved necessary to flush DNS cache?
Usually not. The resolvectl flush-caches command is the appropriate first step for clearing its cache. Restart the service only when there is evidence of a service problem or another specific reason.
Conclusion
Knowing how to flush DNS cache Linux systems use helps troubleshoot stale DNS responses after website migrations, IP address changes, and DNS configuration updates. The correct procedure depends on whether caching is handled by systemd-resolved, nscd, dnsmasq, BIND, or another component.
Start by comparing the local DNS response with a direct resolver query, flush only the relevant cache, and verify the authoritative record if the old address persists. This approach avoids unnecessary service restarts and helps distinguish local caching issues from DNS propagation or configuration problems.
Need a reliable environment for your Linux workloads? Explore ServerNext VPS solutions or visit ServerNext to learn more about available hosting options.
