systemd/NetworkManager - DNS server ordering

3 min read 22-10-2024
systemd/NetworkManager - DNS server ordering

Introduction

In a modern Linux environment, managing network configurations has become more sophisticated and efficient with tools like systemd and NetworkManager. One of the key aspects of these systems is how DNS (Domain Name System) servers are prioritized, which is essential for ensuring fast and reliable name resolution. This article will explain the DNS server ordering in systemd and NetworkManager, how you can configure it, and the implications of these settings for system performance.

The Problem Scenario

When using systemd and NetworkManager, you may encounter issues with DNS server ordering. The DNS servers that your system uses for name resolution are not necessarily the ones you want or expect. This can lead to slow internet access or failed lookups if the primary DNS server is unreachable.

Here’s a common code snippet related to DNS server configuration within NetworkManager:

[main]
dns=default

In this configuration, the DNS server ordering might not align with your expectations, leading to potential connectivity problems.

How DNS Server Ordering Works

Default DNS Ordering

By default, NetworkManager uses the DNS servers provided by the DHCP server, if you are on a dynamic IP configuration. If you have configured static IP addresses, it might use the DNS servers specified in your /etc/resolv.conf file. However, the order in which DNS servers are queried is often a significant factor in determining resolution speed.

Manual Configuration

To manually set DNS servers and prioritize them effectively, you can modify the NetworkManager configuration. Here’s a step-by-step guide:

  1. Open NetworkManager Connection Editor: You can do this via the command line or GUI. For command line, you would typically use:

    nm-connection-editor
    
  2. Select Your Connection: Choose the active connection you want to modify.

  3. Modify IPv4/IPv6 Settings: In the IPv4 or IPv6 settings tab, switch from "Automatic" to "Automatic (DHCP) addresses only" to allow for static DNS.

  4. Enter DNS Servers: Input the DNS servers you want to use in the “DNS servers” field, separating them with commas. The order here matters; the first DNS server will be queried first.

  5. Save and Restart the Connection: After saving the changes, you should restart the NetworkManager service for changes to take effect:

    sudo systemctl restart NetworkManager
    

Advanced Configuration with systemd-resolved

In many recent distributions, systemd-resolved is used in conjunction with NetworkManager. To check or configure DNS settings with systemd-resolved, you can do the following:

  • Check Current Settings:

    systemd-resolve --status
    
  • Set DNS Servers: Edit the /etc/systemd/resolved.conf file and specify your preferred DNS servers:

    [Resolve]
    DNS=1.1.1.1 8.8.8.8
    
  • Restart the service:

    sudo systemctl restart systemd-resolved
    

Practical Example

Let’s say you want to prioritize Cloudflare’s DNS server (1.1.1.1) over Google’s (8.8.8.8). You would input these servers in the order specified:

DNS=1.1.1.1,8.8.8.8

After saving the configuration and restarting the services, your system will prioritize Cloudflare’s DNS for any DNS resolution tasks, potentially resulting in improved speed and privacy.

Conclusion

Understanding and configuring DNS server ordering in systemd and NetworkManager can significantly enhance your system's networking performance. By following the steps outlined in this article, you can ensure that the most reliable and fastest DNS servers are being used for your system.

Additional Resources

By paying attention to DNS server ordering and settings, you can ensure that your system remains performant and reliable, minimizing issues related to name resolution.