Troubleshooting Slow DNS Lookups with systemd-resolved on My Linux Homelab Server

Introduction to Troubleshooting Slow DNS Lookups

I’ve been running a Linux homelab server for a while now, and one issue that’s caught my attention recently is slow DNS lookups. With the increasing reliance on online services, fast and reliable DNS resolution is crucial. I noticed my server was taking longer than usual to resolve domain names, affecting my homelab’s overall performance. After digging in, I found the issue was related to systemd-resolved, the DNS resolver service that comes bundled with systemd. I’ve seen this go wrong when the default resolvers aren’t responding quickly.

Understanding systemd-resolved

systemd-resolved is a straightforward DNS resolver designed to work seamlessly with systemd. By default, it uses a combination of DNS resolvers, including the ones provided by your ISP and public resolvers like Google’s DNS. However, this can lead to slow DNS lookups if the default resolvers are not responding quickly. Don’t bother with trying to tweak the default resolvers; it’s often more effective to configure alternative ones.

Identifying the Issue

To troubleshoot slow DNS lookups, I started by checking the systemd-resolved status:

systemctl status systemd-resolved

This showed me the service was running, but didn’t give me any clues about the slow DNS lookups. The real trick is to use the resolvectl command to query the DNS resolver directly:

resolvectl query example.com

Repeating this command several times, I noticed the response time was consistently high. This is where people usually get burned - they don’t test the DNS lookup speed thoroughly.

Checking DNS Resolver Configuration

Next, I checked the DNS resolver configuration:

resolvectl status

This showed me the current DNS resolver configuration, including the list of resolvers being used. I noticed the default resolvers were not responding quickly, causing the slow DNS lookups. In practice, this is often due to the ISP’s resolvers being overloaded or poorly configured.

Configuring Alternative DNS Resolvers

To resolve the issue, I configured alternative DNS resolvers known to be fast and reliable. I added the following lines to the /etc/systemd/resolved.conf file:

[Resolve]
DNS=1.1.1.1 8.8.8.8

This tells systemd-resolved to use Cloudflare’s DNS (1.1.1.1) and Google’s DNS (8.8.8.8) as the primary resolvers. I then restarted the systemd-resolved service:

systemctl restart systemd-resolved

After restarting, I repeated the resolvectl query command to test the DNS lookup speed. The response time was significantly faster, and my homelab server was now resolving domain names quickly.

Additional Tips and Considerations

When configuring alternative DNS resolvers, consider the security implications. Using a public DNS resolver like Cloudflare’s DNS or Google’s DNS can provide additional security features like DNS over HTTPS (DoH) and DNS over TLS (DoT). However, understand the trade-offs and potential risks associated with using a public DNS resolver. For example, some public DNS resolvers may collect user data or impose restrictions on certain types of traffic.

Troubleshooting Notes

If you’re experiencing slow DNS lookups with systemd-resolved, I recommend checking the following:

  • The systemd-resolved status and logs for any errors or warnings
  • The DNS resolver configuration using resolvectl status
  • The response time of the default resolvers using resolvectl query
  • The configuration of alternative DNS resolvers in /etc/systemd/resolved.conf For more information on systemd-resolved and DNS configuration, you can refer to the systemd.io documentation.

See also