- Flushing the DNS cache on Ubuntu with “resolvectl flush-caches” can help troubleshoot connectivity issues and improve network performance.
- DNS caching reduces the time it takes to return DNS requests, but outdated or corrupt entries will cause problems.
- Remember to also clear the DNS cache in your internet browsers to ensure all cached data is cleared for troubleshooting.
The domain name resolving service is enabled by default in Ubuntu. That’s fine, it performs a useful service. But it’s worth periodically flushing its buffers, especially if you get flaky connectivity.
What Is DNS Caching?
The domain name system (DNS) service eats names and spits out numbers. It’s the service that lets us talk about network names or internet domain names, instead of learning a load of IP addresses. The DNS service translates internet domain names into IP addresses so that our connection requests and network traffic can be directed toward the correct server. Matching names to IP addresses is called resolving.
Internet DNS lookups are cascaded through a hierarchy of servers, known as precursor DNS servers, root name servers, top-level domain servers, and authoritative name servers. Lookups are very fast, but they’re not instantaneous.
To reduce the time it takes to return an answer to a DNS request, precursor DNS servers cache their most recent queries and answers. If someone has recently made the same DNS request, the server can return that value to your computer without having to involve any of the other servers.
Your router at home probably caches local network device names and IP addresses, and it may even cache responses from external DNS servers.
If the answer to a DNS request is found in the precursor server’s cache, no further servers need to be contacted. The answer is sent back from the cache. Similarly, if you try to connect to a local network device using its network device name, your router provides the IP address.
Using cached data might be faster than a full DNS lookup, but it is predicated on the assumption that the stored values are still valid. If the IP address of the website or local network device has changed, your computer won’t be able to talk to it.
Ubuntu adds its own cache to the mix. By default, the systemd-resolved service is enabled. This caches DNS requests and responses. If a single cache entry (or the whole cache for that matter) becomes corrupted, you can experience connectivity issues with remote servers, web resources, and local devices.
What Does Flushing the DNS Cache Do?
Flushing the DNS cache tells your computer to forget all the stored name and IP address pairs it has collected. That means any connection request that requires resolving from a name to an IP address requires your computer to query an outside source such as your local router or an external DNS service. The response will be cached.
Over time, your DNS cache will be populated by new entries, some of which are likely to be updated versions of older, stale cache entries. It’s an easy and safe operation. If you ever experience networking or internet flakiness, flushing your DNS cache is a good first step in troubleshooting.
Flushing the DNS Cache on Ubuntu
On Ubuntu, the DNS service is on by default. If it’s your computer, and you know you haven’t turned the resolved daemon off, then DNS will be on. It’s easy to check, which is handy if you need to work on someone else’s computer, and they can’t tell you whether DNS is on or off.
The command to use is straightforward. We’re using the systemctl command because we need to check on a part of the systemd collection of utilities, namely systemd-resolved. This is the systemd name resolving daemon.
systemctl is-active systemd-resolved
The response will either be “active” or “inactive.”
To verify that flushing the buffers actually does something, we can take a peek at the number of entries in the cache. We’ll check again, once we’ve flushed the buffers. This time, we’re using the resolvectl command to manipulate systemd-resolved directly.
Because this is a fresh installation of Ubuntu, there are relatively few entries in the cache. Regardless, when we flush the DNS buffers we expect to see that number drop to zero.
We’re silently returned to the command line. No output means everything went well. Let’s see what our cache size is now.
Our cache size is zero, just as we predicted.
Don’t Forget Your Browser’s Cache
Your internet browsers have their own caches. If you’re flushing your DNS buffers to try to remove a problem, make a point of doing the same for your browsers.
Flushing the DNS Cache in Firefox
Open a new tab in Firefox, enter this text in the URL address bar, and hit Enter:
Click the “DNS” entry in the sidebar.
Firefox shows you the most recent entries added to the DNS cache. To clear the cache, click the “Clear DNS Cache” button. I had to refresh the page to see the change, but the cache was definitely purged.
Flushing the DNS Cache in Google Chrome
To do the same operation in Google Chrome, enter this in the URL address bar and hit the Enter key:
Click the “Clear Host Cache” button. There’s no visible feedback to let you know anything has actually happened, but behind the scenes it has. Google Chrome’s DNS cache has been emptied.
Remember to Flush Your DNS Cache
Corrupt or wrong DNS entries can be the root cause of many unwanted networking behaviors. If you’re trying to isolate the issue, flushing your DNS buffers will remove them from the list of possibilities.
It’s easy to do, so it makes sense to do it as a first step. And with a bit of luck, your issue might be solved.