Ubuntu 24.04多网络接口场景下DNS优先级配置最佳实践咨询
Hey there, let's tackle this DNS priority issue you're facing with your dual-network setup on Ubuntu 24.04. I've dealt with similar corporate network scenarios before, so here's the most reliable approach to get this working smoothly without routing all your DNS traffic through your employer.
Why your current priority approach isn't working
First, let's clarify the core problem: Ubuntu's default DNS resolver (systemd-resolved) doesn't "fall back" to secondary DNS servers if the first one returns a "domain not found" response. It stops at the first server's answer. So when you try to resolve your employer's internal hostnames, your home WiFi's DNS will return an error, and the system won't check your employer's DNS after that.
Best Practice: Split DNS by Domain/Subnet
Instead of relying on generic priority, you should configure DNS routing rules—telling the system to use your employer's DNS only for their specific domains or subnets, and your home DNS for everything else. Here's how to do it:
Option 1: Configure via systemd-resolved directly
- First, confirm
systemd-resolvedis running:systemctl status systemd-resolved - Identify your employer's ethernet interface name (use
ip link show; it might look likeenp0s20f0u1) - Get your employer's DNS server address (check NetworkManager connection settings or run:
nmcli con show "Your Employer Ethernet Connection Name" | grep ipv4.dns - Create a custom resolved config file:
Paste this content (replace placeholders with your actual values):sudo nano /etc/systemd/resolved.conf.d/company-dns.conf[Resolve] # Target domains/subnets that should use the employer's DNS Domains=~your-company-domain.com ~10.10.0.0/16 # Employer's DNS server address DNS=10.0.0.1 # Bind this rule to the employer's ethernet interface Interface=enp0s20f0u1 - Restart the service to apply changes:
sudo systemctl restart systemd-resolved
Option 2: Use NetworkManager GUI (simpler for non-terminal users)
- Open NetworkManager, find your employer's ethernet connection, and click "Edit Connection"
- Go to the IPv4 Settings tab
- In the DNS section:
- Add your employer's DNS server address
- In "Search Domains", add your company's domain suffix (e.g.,
your-company-domain.com) - Check the box labeled "Use this connection's DNS servers for these domains only"
- Save the settings and reconnect to the ethernet connection
Verify the configuration works
Flush DNS caches to test fresh:
sudo resolvectl flush-cachesCheck if an internal hostname uses the correct DNS:
resolvectl query internal-host.your-company-domain.comThe output should show it's using your employer's DNS server.
Verify general traffic uses your home DNS:
resolvectl query google.comThis should use your home WiFi's DNS.
Important Notes
- Don't manually edit
/etc/resolv.conf—Ubuntu 24.04 defaults to a symlink tosystemd-resolvedmanaged files. If you've modified it before, restore the default link:sudo ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf - Ensure NetworkManager is set to hand off DNS management to
systemd-resolved: In each connection's IPv4 settings, set "DNS" to "Automatic (DHCP) addresses only" (don't force custom DNS unless necessary).
备注:内容来源于stack exchange,提问作者Gordonei

