大网数据广告活动
服务器运维问答

How to get Netplan to set the DNS server in /etc/resolv.conf based on info coming from the DHCP server?

· 52CE 我爱测

I'm setting up a 4 x RPi Cluster with the following simple network topology: All 4 RPi nodes have the latest Ubuntu Server LTS 20.04 OS. rpi1 is my primary/gateway node. I did the following on rpi1: Assigned a static IP Address o…

问题原文

原帖链接 · Magnus · CC BY-SA 4.0

I'm setting up a 4 x RPi Cluster with the following simple network topology:

All 4 RPi nodes have the latest Ubuntu Server LTS 20.04 OS. rpi1 is my primary/gateway node. I did the following on rpi1:

  • Assigned a static IP Address on eth0 of 10.0.0.1 using netplan
  • Disabled systemd-resolved and manually created /etc/resolv.conf with value nameserver 127.0.0.1
  • Setup dnsmasq with the following /etc/dnsmasq.conf:
# Basics:
interface=eth0
listen-address=127.0.0.1

# DHCP Settings (MAC Addresses removed)
dhcp-range=10.0.0.1,10.0.0.128,12h

dhcp-host=XX:XX:XX:XX:XX:XX,rpi1,10.0.0.1
dhcp-host=XX:XX:XX:XX:XX:XX,rpi2,10.0.0.2
dhcp-host=XX:XX:XX:XX:XX:XX,rpi3,10.0.0.3
dhcp-host=XX:XX:XX:XX:XX:XX,rpi4,10.0.0.4

dhcp-option=option:router,10.0.0.1
dhcp-option=option:dns-server,10.0.0.1
dhcp-option=option:netmask,255.255.255.0

# Forward to nameservers:
server=8.8.8.8
server=8.8.4.4

# Misc
bind-interfaces
domain-needed
bogus-priv
expand-hosts
  • Added identities to the /etc/hosts file:
10.0.0.1 rpi1
10.0.0.2 rpi2
10.0.0.3 rpi3
10.0.0.4 rpi4

This setup seems to work successfully as a DHCP server as it correctly assigns the desired IP Addresses to the eth0 devices of the remaining "minor" nodes (rpi2, rpi3, rpi4) based on their MAC address.

HOWEVER, I can't figure out how to use Netplan on the minor nodes to register the nameserver being broadcast by the dnsmasq-DHCP server running on rpi1. I've tried all sorts of settings in the /etc/netplan/50-cloud-init.yaml file on a minor node, but when I run sudo netplan apply, it ALWAYS creates a /etc/resolv.conf file with the following nameserver-location info:

nameserver 127.0.0.53
options edns0

If I manually edit /etc/resolv.conf to point to rpi1 (nameserver 10.0.0.1) then it works (e.g. I can then ping rpi3 from rpi2), but I would like to understand why running netplan apply doesn't set this automatically based on info coming from the dnsmasq broadcast. I've tried various settings in my /etc/netplan/50-cloud-init.yaml, but nothing seems to affect the resultant /etc/resolv.conf file.

Here's where I got to last on the /etc/netplan/50-cloud-init.yaml file on rpi2 before giving up (note: I also enable direct Wi-Fi access here for initial setup/debugging):

network:
    version: 2    
    ethernets:
        eth0:
            dhcp4: true
            optional: true
            dhcp4-overrides:
                use-dns: false
            nameservers:
                addresses: [10.0.0.1]
    wifis:
        wlan0:
            optional: true
            access-points:
                "my-wifi-signal":
                    password: "mypassword"
            dhcp4: no
            addresses: [192.168.0.52/24]
            gateway4: 192.168.0.1
            nameservers:
                addresses: [8.8.8.8,8.8.4.4]

It's also odd to me that when I run systemd-resolve --status after applying Netplan with this config, it seems to indicate that it DOES now recognize the location of the desired DNS nameserver on rpi1:

$ systemd-resolve --status
...

Link 2 (eth0)
      Current Scopes: DNS
DefaultRoute setting: yes
       LLMNR setting: yes
MulticastDNS setting: no
  DNSOverTLS setting: no
      DNSSEC setting: no
    DNSSEC supported: no
  Current DNS Server: 10.0.0.1
         DNS Servers: 10.0.0.1

But, as I said, when I try to e.g. ping rpi4 from rpi3 it doesn't work (I get the message "ping: rpi4: Temporary failure in name resolution").

In summary:

How can I make it so that running sudo netplan apply will consult the DHCP server for a DNS Nameserver and use that to set the content of /etc/resolv.conf?

社区高分解答

原帖链接 · Mashkoor Qadir · CC BY-SA 4.0

The same issue I faced too. Netplan uses systemd-resolvd for name resolution and most of time I found the /etc/resolve.conf file is a symlink to /run/systemd/resolve/stub-resolv.conf and which uses 127.0.0.53 for loopback interfaces and the nameservers defined in /etc/netplan/00-cloud-init.yaml file are written in /run/systemd/resolve/resolv.conf.

Following steps worked for me.

$ sudo unlink /etc/resolv.conf
$ sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

and restart systemd-resolved service

$ sudo systemctl restart systemd-resolved.service 

Hope it will help you too.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-09-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。