Introduction to systemd-resolved
I’ve been dealing with DNS lookup failures on my home network, and I’ve found that the systemd-resolved cache can be a major culprit. I’ve seen this go wrong when the cache becomes outdated or corrupted, leading to frustrating DNS lookup failures. In this article, I’ll share my experience with taming the systemd-resolved cache to fix these issues.
Understanding the systemd-resolved Cache
The real trick is to understand how the systemd-resolved cache works. It’s designed to improve DNS lookup performance by storing recent queries and their results. However, this cache can become outdated or corrupted, leading to DNS lookup failures. To get a better understanding of the cache, let’s take a look at the systemd-resolved configuration file, usually located at /etc/systemd/resolved.conf. You can view the file using the following command:
cat /etc/systemd/resolved.conf
Don’t bother with trying to decipher the entire file - just focus on the Cache= and DNSStubListener= options. The Cache= option controls the caching behavior, while DNSStubListener= specifies the DNS stub listener. In practice, these options can have a significant impact on your DNS lookup performance.
Checking the systemd-resolved Cache
To check the current cache status, you can use the resolvectl command:
resolvectl status
This command displays information about the systemd-resolved service, including the cache status. You can also use resolvectl to query the cache directly:
resolvectl query <domain>
Replace <domain> with the domain you want to query. This is where people usually get burned - they don’t realize that the cache can return outdated results.
Clearing the systemd-resolved Cache
If you suspect that the cache is causing issues, you can clear it using the following command:
resolvectl flush-caches
This command flushes the entire cache, which can help resolve DNS lookup failures. However, be aware that clearing the cache may introduce a temporary performance penalty, as the system needs to re-query the DNS servers. I usually start with this step when troubleshooting DNS issues.
Configuring the systemd-resolved Cache
To prevent cache-related issues, you can configure the systemd-resolved cache to suit your needs. For example, you can set the cache size or disable caching altogether. To do this, edit the /etc/systemd/resolved.conf file and adjust the Cache= option. For instance, to set the cache size to 1000 entries, add the following line:
Cache=1000
You can also disable caching by setting Cache= to no:
Cache=no
After modifying the configuration file, restart the systemd-resolved service to apply the changes:
sudo systemctl restart systemd-resolved
This is where most people stop, but there’s more to it.
Troubleshooting DNS Lookup Failures
If you’re still experiencing DNS lookup failures after clearing and configuring the cache, it’s time to troubleshoot the issue. Here are some steps to help you identify the problem:
- Check the
systemd-resolvedservice status:
sudo systemctl status systemd-resolved
- Verify the DNS server configuration:
resolvectl status
- Test DNS resolution using a tool like
dig:
dig <domain>
Replace <domain> with the domain you want to test. This is where you can catch any underlying issues with your DNS setup.
Additional Resources
For more information on systemd-resolved and DNS resolution, I recommend checking out the systemd documentation and the freedesktop.org page on systemd-resolved.
See also
- Recovering from a Failed Borg Backup Repository: Lessons from a Homelab Mishap
- Taming Disk Space Consumption on My Home Server with Quarterly rsync Snapshots and a Dash of Log Rotation
- Taming Background Tasks with nohup and systemd: My Homelab Workflow
- Taming Rogue Background Jobs with nohup and systemd
- Taming systemd-resolved: How I Stopped It From Overriding My resolv.conf Settings