The Ephemeral DNS Cache
Ubuntu uses `systemd-resolved` to manage network name resolution. This service maintains a local DNS cache to speed up web browsing by remembering the IP addresses of recently visited domains. By default, this cache is highly volatile. `systemd-resolved` will automatically clear (or flush) the DNS cache whenever your network interface changes state—such as when you disconnect from a VPN, switch from Wi-Fi to Ethernet, or when the DHCP lease renews. While this ensures you don’t connect to stale IPs after a network change, it can be frustrating for developers who are trying to debug DNS propagation issues or analyze local cache behavior, only to find the cache has wiped itself.
Modifying systemd-resolved Configuration
To retain your cached DNS lookups longer and stop Ubuntu from automatically clearing the DNS cache during minor network events, you need to alter the behavior of the `systemd-resolved` daemon.
Open your terminal (Ctrl+Alt+T). You need to edit the resolved configuration file with root privileges. Type sudo nano /etc/systemd/resolved.conf and press Enter. Look for the line that says #Cache=yes. The hash symbol `#` means it is currently using the default setting. First, remove the `#` to uncomment the line. Then, if you want to aggressively force caching regardless of interface changes, you can try setting it to Cache=yes (though default behavior still clears on link down).
To truly stop the flushing upon interface changes, you often have to bypass `systemd-resolved` entirely for caching and use a more robust local resolver like `dnsmasq` or `unbound`. If you prefer to stick with `systemd-resolved`, you can only control its size, not its flush triggers. However, you can manually inspect it using resolvectl statistics. For development environments requiring persistent cache, disabling `systemd-resolved` (sudo systemctl disable systemd-resolved) and installing `unbound` is the standard approach to prevent automatic clearing.