How to Stop Ubuntu from Automatically Clearing the DNS Cache

The DNS Resolution Lag

By default, modern versions of Ubuntu utilize systemd-resolved to manage network name resolution. This service maintains a local DNS cache, which stores the IP addresses of recently visited websites to drastically speed up page load times on subsequent visits. However, this cache is volatile and is automatically flushed and cleared every time your system reboots, or whenever your primary network interface disconnects and reconnects. If you are on an unstable Wi-Fi connection that frequently drops, the constant cache clearing forces your browser to perform a slow DNS lookup for every single website you visit, resulting in a sluggish browsing experience.

Creating a Persistent Cache

To stop Ubuntu from aggressively dumping its DNS cache upon every network state change, and to ensure the cache survives reboots, you must install and configure a dedicated, persistent caching daemon like dnsmasq.

First, open your terminal (Ctrl+Alt+T) and disable the default resolver from conflicting by typing: sudo systemctl disable systemd-resolved && sudo systemctl stop systemd-resolved. Next, remove the old symlink with sudo rm /etc/resolv.conf and create a new static file by typing echo "nameserver 127.0.0.1" | sudo tee /etc/resolv.conf. Now, install the persistent caching daemon: sudo apt install dnsmasq. Open its configuration file with sudo nano /etc/dnsmasq.conf. Find the line #listen-address=, uncomment it by removing the hash, and change it to listen-address=127.0.0.1. Add a line at the bottom specifying your preferred upstream DNS, for example: server=8.8.8.8. Finally, find the line #cache-size=150, uncomment it, and increase the value to cache-size=10000. Save the file (Ctrl+O) and exit (Ctrl+X). Restart the service with sudo systemctl restart dnsmasq. Your DNS cache is now massive, persistent, and entirely independent of network drops.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.