Systemd-networkd: Honoring DHCP DNS Settings
Hey guys! Ever found yourself wrestling with systemd-networkd trying to get it to play nice with DHCP-provided DNS settings across different networks? It can be a bit of a head-scratcher, especially when you're hopping between Wi-Fi networks or dealing with a mix of wired and wireless connections. Today, we're diving deep into how to make systemd-networkd and systemd-resolved work harmoniously, ensuring your laptop, like the one in our example running Gentoo, properly honors DNS settings supplied by DHCP, no matter the network you're connected to. So, grab your favorite beverage, and let’s get started!
Understanding the Challenge
Before we jump into the solutions, let's break down the problem. Imagine you're a laptop user who juggles multiple networks daily. You might connect to your home Wi-Fi, a coffee shop's network, or even a wired connection at work. Each network can hand out different DNS server addresses via DHCP. The goal is to ensure your system automatically uses the DNS servers provided by each network without manual intervention. This is where systemd-networkd
and systemd-resolved
come into play. These systemd components are designed to handle network configuration and DNS resolution, respectively. However, sometimes they might not pick up the DHCP-supplied DNS settings as expected, leading to DNS resolution issues.
Systemd-networkd, the network configuration daemon, manages your network interfaces. It reads configuration files to bring up interfaces, set IP addresses, and configure routing. Systemd-resolved, on the other hand, is a DNS resolution service that caches DNS lookups and handles DNS queries. When these two aren't communicating effectively, you might experience slow internet browsing, inability to resolve domain names, or inconsistent network behavior. The challenge often lies in configuring systemd-networkd
to properly listen for DHCP-provided DNS settings and then passing these settings to systemd-resolved
. This involves creating the correct network configuration files and ensuring the services are correctly enabled and started. Additionally, understanding the interplay between different configuration options and their precedence is crucial. For instance, globally defined DNS settings might override DHCP-provided settings if not configured correctly. Let's explore the steps to tackle this challenge head-on and ensure your system seamlessly adapts to different network environments.
Step-by-Step Configuration
Alright, let's roll up our sleeves and get into the nitty-gritty of configuring systemd-networkd to respect DHCP DNS settings. We'll break this down into a step-by-step guide to make it super easy to follow.
1. Network Interface Configuration
The first step is to create network configuration files for your interfaces. These files tell systemd-networkd how to manage your network connections. Usually, these files live in /etc/systemd/network/
. You'll need to create a .network
file for each interface you want systemd-networkd to manage. Let's create a configuration file for a wireless interface, say wlan0
.
sudo nano /etc/systemd/network/20-wlan0.network
Now, let's add the following content to the file:
[Match]
Name=wlan0
[Network]
DHCP=ipv4
[Match]
section specifies which interface this configuration applies to.Name=wlan0
tells systemd-networkd to use this configuration for thewlan0
interface.[Network]
section configures the network settings.DHCP=ipv4
instructs systemd-networkd to use DHCP for IPv4 configuration. This is the magic line that tells systemd-networkd to listen for DHCP-provided settings, including DNS servers.
If you have an Ethernet interface, like eth0
, you might want to create a similar configuration file for it. For example:
sudo nano /etc/systemd/network/10-eth0.network
And the content:
[Match]
Name=eth0
[Network]
DHCP=ipv4
Remember, the numbers in the filenames (e.g., 10-
, 20-
) determine the order in which systemd-networkd processes the files. This can be important if you have multiple interfaces and specific configuration priorities.
2. Enabling and Starting Services
Once you've created the network configuration files, you need to ensure that systemd-networkd and systemd-resolved are enabled and running. Enabling a service tells systemd to start it at boot, while starting it makes it run immediately.
First, let's enable and start systemd-networkd:
sudo systemctl enable systemd-networkd
sudo systemctl start systemd-networkd
Next, let's do the same for systemd-resolved:
sudo systemctl enable systemd-resolved
sudo systemctl start systemd-resolved
3. Linking resolv.conf
systemd-resolved
typically manages the /etc/resolv.conf
file, which is used by applications to look up DNS servers. You need to ensure that /etc/resolv.conf
is a symbolic link to the systemd-resolved
stub resolver. This is usually the default on many modern systems, but it's worth checking.
To do this, remove the existing /etc/resolv.conf
(if it's not a symlink) and create the symlink:
sudo rm /etc/resolv.conf
sudo ln -s /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
Alternatively, you can use the following command to check the status and create the symlink if needed:
sudo systemd-resolve --status | grep 'Link Local DNS' #Check current linked DNS
sudo unlink /etc/resolv.conf #Unlink any existing resolv.conf
sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf #re-create symlink as systemd-resolved managed DNS
This ensures that applications use systemd-resolved
for DNS lookups, which in turn will use the DNS servers provided by DHCP.
4. Verifying DNS Settings
After setting everything up, it's crucial to verify that your system is indeed using the DHCP-provided DNS settings. You can do this using the systemd-resolve
command.
Run the following command:
systemd-resolve --status
This will display detailed information about your network interfaces and their DNS settings. Look for the interface you're connected to (e.g., wlan0
) and check the Current DNS Server
and DNS Servers
fields. These should reflect the DNS servers provided by your network's DHCP server.
5. Troubleshooting Common Issues
Sometimes, things don't go as planned. If you're still having issues with DNS resolution, here are a few common problems and their solutions:
- Incorrect Configuration Files: Double-check your
.network
files for typos or incorrect settings. Even a small mistake can prevent systemd-networkd from working correctly. - Service Not Running: Ensure that both
systemd-networkd
andsystemd-resolved
are running. You can check their status usingsystemctl status systemd-networkd
andsystemctl status systemd-resolved
. - Conflicting DNS Settings: If you have other DNS configuration tools installed (like
NetworkManager
), they might be interfering with systemd-resolved. Consider disabling or removing them. - Firewall Issues: Your firewall might be blocking DNS traffic. Make sure that port 53 (DNS) is open for both TCP and UDP.
Advanced Configuration Tips
Now that we've covered the basics, let's explore some advanced tips to fine-tune your systemd-networkd setup.
1. Global DNS Settings
In some cases, you might want to specify global DNS settings that apply to all interfaces. You can do this by creating a global configuration file in /etc/systemd/resolved.conf.d/
. For example:
sudo nano /etc/systemd/resolved.conf.d/global.conf
And add the following content:
[Resolve]
DNS=8.8.8.8 8.8.4.4
This will set Google's Public DNS servers as the global DNS servers. However, keep in mind that these settings will be overridden by DHCP-provided DNS servers if DHCP=ipv4
is configured in your interface-specific .network
files.
2. Conditional DNS Forwarding
Conditional DNS forwarding allows you to forward DNS queries for specific domains to different DNS servers. This can be useful if you have a local domain (e.g., *.local
) that should be resolved by a specific DNS server.
To configure conditional DNS forwarding, you can add the Domains
option to the [Network]
section of your .network
file. For example:
[Match]
Name=wlan0
[Network]
DHCP=ipv4
Domains=example.com 192.168.1.0/24
This will forward DNS queries for example.com
and the 192.168.1.0/24
subnet to the DNS servers provided by DHCP on the wlan0
interface.
3. Using Link-Local Addressing
Link-local addressing allows devices on the same local network to communicate without needing a DHCP server. Systemd-networkd can automatically configure link-local addresses (in the 169.254.0.0/16
range) if DHCP fails. To enable link-local addressing, add the following to your .network
file:
[Network]
LinkLocalAddressing=yes
4. DHCP Client Options
Systemd-networkd supports various DHCP client options that allow you to customize the DHCP request. For example, you can specify a hostname or request specific DHCP options. To set DHCP client options, you can use the DHCPClientIdentifier
and SendOption
options in the [DHCP]
section of your .network
file.
[DHCP]
ClientIdentifier=mac
SendOption=60,vendor:MyVendor
Conclusion
Configuring systemd-networkd to honor DHCP-supplied DNS settings might seem daunting at first, but with a clear understanding of the steps involved, it becomes quite manageable. By creating the correct network configuration files, enabling and starting the necessary services, and verifying the DNS settings, you can ensure your system seamlessly adapts to different network environments. Remember to troubleshoot common issues and explore advanced configuration tips to fine-tune your setup. With these tools in your arsenal, you'll be a systemd-networkd pro in no time! Happy networking, guys!
Repair Input Keyword
- How do I configure systemd-networkd to use DHCP-provided DNS settings for different networks?
- How to set up systemd-networkd and systemd-resolved to respect DHCP DNS settings.
- Systemd DNS configuration: Getting DHCP DNS servers to work correctly.