Taming systemd-resolved: How I Stopped It From Overriding My resolv.conf Settings

Introduction to systemd-resolved

I’ve seen this go wrong when working with Linux systems - systemd-resolved taking over your DNS settings without warning. As a Linux user, you’re likely familiar with the resolv.conf file, which contains the DNS resolver configuration for your system. However, with the introduction of systemd-resolved, things have changed. systemd-resolved is a systemd service that provides DNS resolution and caching, and it can sometimes override the settings in your resolv.conf file. In this article, we’ll explore how to tame systemd-resolved and prevent it from overriding your resolv.conf settings.

Understanding systemd-resolved

The real trick is understanding how systemd-resolved works. It’s a part of the systemd suite of tools, and it’s designed to provide a more efficient and secure way of resolving DNS queries. It uses a caching mechanism to store the results of previous DNS queries, which can improve performance by reducing the number of queries sent to the DNS server. However, this caching mechanism can also cause problems if you’re trying to use a custom resolv.conf file. Don’t bother with trying to fight it - just disable it or configure it to work with your setup.

Disabling systemd-resolved

One way to prevent systemd-resolved from overriding your resolv.conf settings is to disable it altogether. You can do this by running the following command:

sudo systemctl disable --now systemd-resolved

This will stop the systemd-resolved service and prevent it from starting automatically on boot. In practice, this is usually the easiest solution if you don’t need the features provided by systemd-resolved.

Configuring systemd-resolved

If you don’t want to disable systemd-resolved entirely, you can configure it to use your custom resolv.conf file instead. To do this, you’ll need to create a resolv.conf file in the /etc/systemd/resolved.conf.d/ directory. You can do this by running the following command:

sudo nano /etc/systemd/resolved.conf.d/resolv.conf

In this file, you can specify the DNS servers and other settings that you want to use. For example:

[Resolve]
DNS=192.168.1.1 8.8.8.8
Domains=~.

This configuration tells systemd-resolved to use the DNS servers at 192.168.1.1 and 8.8.8.8, and to resolve all domains using these servers.

Using the resolv.conf file

If you’ve configured systemd-resolved to use your custom resolv.conf file, you’ll need to make sure that the file is in the correct location and has the correct permissions. The resolv.conf file should be owned by the root user and have permissions of 0644. You can check the permissions of the file by running the following command:

ls -l /etc/resolv.conf

If the permissions are not correct, you can change them by running the following command:

sudo chmod 0644 /etc/resolv.conf

This is where people usually get burned - forgetting to set the correct permissions.

Troubleshooting

If you’re having trouble getting systemd-resolved to work with your custom resolv.conf file, there are a few things you can try. First, make sure that the systemd-resolved service is running by checking the status of the service:

sudo systemctl status systemd-resolved

If the service is not running, you can start it by running the following command:

sudo systemctl start systemd-resolved

You can also check the logs for systemd-resolved to see if there are any error messages that might indicate what’s going wrong. You can do this by running the following command:

sudo journalctl -u systemd-resolved

I usually start with the logs when troubleshooting - they can give you a good idea of what’s going on.

Additional Tips

It’s worth noting that systemd-resolved can also be configured to use DNS over TLS (DoT) or DNS over HTTPS (DoH). These protocols provide an additional layer of security by encrypting DNS traffic. To configure systemd-resolved to use DoT or DoH, you’ll need to add the following lines to your resolv.conf file:

[Resolve]
DNSOverTLS=yes
DNSOverHTTPS=yes

You can also specify the DNS server to use for DoT or DoH by adding the following line:

[Resolve]
DNS=1.1.1.1#cloudflare-dns.com

This configuration tells systemd-resolved to use the Cloudflare DNS server over DoT. For more information on systemd-resolved, you can check out the systemd documentation.


See also