Ubuntu:netplan 禁用 dhcp

Ubuntu:netplan 禁用 dhcp

系统 ip 配置 b4 netplan 编辑:

root@pro-rpi-02:/# ip a sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether dc:a6:32:b3:45:e0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/26 brd 192.168.1.63 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.1.7/26 brd 192.168.1.63 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::4569:7d7a:108d:c2ad/64 scope link
       valid_lft forever preferred_lft forever

我已向 netplan 添加了 vlan:

root@pro-rpi-02:/# more /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses:
        - 192.168.1.2/26
      gateway4: 192.168.1.10
      nameservers:
          addresses: [192.168.1.24]
          search: [hello.home]
  vlans:
    vlan.8:
      id: 8
      link: eth0
      dhcp4: no
      addresses:
        - 192.168.8.2/24

现在到了有趣的部分:

root@pro-rpi-02:/cluster/Radius# ip addr sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether dc:a6:32:b3:45:e0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/26 brd 192.168.1.63 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.1.5/26 brd 192.168.1.63 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::4569:7d7a:108d:c2ad/64 scope link
       valid_lft forever preferred_lft forever
3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether dc:a6:32:b3:45:e1 brd ff:ff:ff:ff:ff:ff
4: vlan.8@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether dc:a6:32:b3:45:e0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.2/24 brd 192.168.8.255 scope global vlan.8
       valid_lft forever preferred_lft forever
    inet 192.168.8.112/24 brd 192.168.8.255 scope global secondary noprefixroute vlan.8
       valid_lft forever preferred_lft forever

有人能解释为什么我会得到 dhcp 地址 192.168.8.112/24 吗?或者至少让我知道从哪里开始查找?

root@pro-rpi-02:/# netplan --debug generate
DEBUG:command generate: running ['/lib/netplan/generate']
** (generate:750570): DEBUG: 12:17:28.037: Processing input file /etc/netplan/01-netcfg.yaml..
** (generate:750570): DEBUG: 12:17:28.038: starting new processing pass
** (generate:750570): DEBUG: 12:17:28.038: We have some netdefs, pass them through a final round of validation
** (generate:750570): DEBUG: 12:17:28.038: eth0: setting default backend to 1
** (generate:750570): DEBUG: 12:17:28.038: Configuration is valid
** (generate:750570): DEBUG: 12:17:28.038: vlan.8: setting default backend to 1
** (generate:750570): DEBUG: 12:17:28.039: Configuration is valid
** (generate:750570): DEBUG: 12:17:28.040: Generating output files..
** (generate:750570): DEBUG: 12:17:28.040: openvswitch: definition eth0 is not for us (backend 1)
** (generate:750570): DEBUG: 12:17:28.040: NetworkManager: definition eth0 is not for us (backend 1)
** (generate:750570): DEBUG: 12:17:28.040: openvswitch: definition vlan.8 is not for us (backend 1)
** (generate:750570): DEBUG: 12:17:28.040: NetworkManager: definition vlan.8 is not for us (backend 1)

更新 1,按照要求,这里是输出,仍然没有看到任何可以帮助我的东西:)

root@pro-rpi-02:~# networkctl --full --all --no-pager status
● 1: lo
                     Link File: /usr/lib/systemd/network/99-default.link
                  Network File: n/a
                          Type: loopback
                         State: carrier (unmanaged)
                           MTU: 65536
                         QDisc: noqueue
  IPv6 Address Generation Mode: eui64
          Queue Length (Tx/Rx): 1/1
                       Address: 127.0.0.1

● 2: eth0
                     Link File: /usr/lib/systemd/network/99-default.link
                  Network File: /run/systemd/network/10-netplan-eth0.network
                          Type: ether
                         State: routable (failed)
                          Path: platform-fd580000.ethernet
                        Driver: bcmgenet
                    HW Address: dc:a6:32:b3:45:e0 (Raspberry Pi Trading Ltd)
                           MTU: 1500 (min: 68, max: 1500)
                         QDisc: mq
  IPv6 Address Generation Mode: eui64
          Queue Length (Tx/Rx): 5/5
              Auto negotiation: yes
                         Speed: 1Gbps
                        Duplex: full
                          Port: mii
                       Address: 192.168.1.2
                                192.168.1.5
                                192.168.1.7
                                fe80::4569:7d7a:108d:c2ad
                       Gateway: 192.168.1.10
                           DNS: 192.168.1.24
                Search Domains: hello.home
             DHCP6 Client DUID: DUID-EN/Vendor:0000ab112ca77a819cd16ac00000

Mar 10 22:03:14 pro-rpi-02 systemd-networkd[926]: eth0: IPv6 successfully enabled
Mar 10 22:03:14 pro-rpi-02 systemd-networkd[926]: eth0: Link UP
Apr 19 16:51:22 pro-rpi-02 systemd-networkd[926]: eth0: Gained carrier
Apr 19 16:51:24 pro-rpi-02 systemd-networkd[926]: eth0: Gained IPv6LL
Apr 20 16:32:48 pro-rpi-02 systemd-networkd[926]: eth0: DHCPv6 lease lost
Apr 20 16:32:48 pro-rpi-02 systemd-networkd[672911]: eth0: Gained IPv6LL
Apr 20 16:32:48 pro-rpi-02 systemd-networkd[672911]: eth0: IPv6 successfully enabled
Apr 20 16:32:48 pro-rpi-02 systemd-networkd[672911]: eth0: Could not set route: Nexthop has invalid gateway. Network is unreachable
Apr 20 16:32:48 pro-rpi-02 systemd-networkd[672911]: eth0: Failed

● 3: wlan0
                     Link File: /usr/lib/systemd/network/99-default.link
                  Network File: n/a
                          Type: wlan
                         State: no-carrier (unmanaged)
                          Path: platform-fe300000.mmcnr
                        Driver: brcmfmac
                    HW Address: dc:a6:32:b3:45:e1 (Raspberry Pi Trading Ltd)
                           MTU: 1500 (min: 68, max: 1500)
                         QDisc: fq_codel
  IPv6 Address Generation Mode: none
          Queue Length (Tx/Rx): 1/1

Apr 19 16:51:22 pro-rpi-02 systemd-networkd[926]: wlan0: Link UP

● 4: vlan8
                     Link File: /usr/lib/systemd/network/99-default.link
                  Network File: /run/systemd/network/10-netplan-vlan8.network
                          Type: vlan
                         State: routable (configuring)
                        Driver: 802.1Q VLAN Support
                    HW Address: dc:a6:32:b3:45:e0 (Raspberry Pi Trading Ltd)
                           MTU: 1500 (max: 65535)
                         QDisc: noqueue
  IPv6 Address Generation Mode: eui64
                       VLan Id: 8
          Queue Length (Tx/Rx): 1/1
              Auto negotiation: yes
                         Speed: 1Gbps
                        Duplex: full
                          Port: mii
                       Address: 192.168.8.2
                                192.168.8.112
                       Gateway: 192.168.8.10
             DHCP6 Client DUID: DUID-EN/Vendor:0000ab112ca77a819cd16ac00000

Apr 20 16:32:48 pro-rpi-02 systemd-networkd[672911]: vlan8: netdev ready
Apr 20 16:32:49 pro-rpi-02 systemd-networkd[672911]: vlan8: Link UP
Apr 20 16:32:49 pro-rpi-02 systemd-networkd[672911]: vlan8: Gained carrier
Apr 20 16:32:49 pro-rpi-02 systemd-networkd[672911]: vlan8: IPv6 successfully enabled

答案1

在花了 X 个小时尝试找到修复方法后,我决定禁用DHCP服务器服务:

root@pro-rpi-02:~# systemctl status dhcpcd.service
● dhcpcd.service - DHCP Client Daemon
     Loaded: loaded (/lib/systemd/system/dhcpcd.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: man:dhcpcd(8)

现在一切都按照我希望的方式运行:

root@pro-rpi-02:~# more /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses:
        - 192.168.1.2/26
      gateway4: 192.168.1.10
      nameservers:
          addresses: [192.168.1.24]
          search: [hello.home]
  vlans:
    vlan8:
      id: 8
      link: eth0
      dhcp4: no
      dhcp6: no
      addresses:
        - 192.168.8.2/24
root@pro-rpi-02:~# ip a sh vlan8
4: vlan8@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether dc:a6:32:b3:45:e0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.2/24 brd 192.168.8.255 scope global vlan8
       valid_lft forever preferred_lft forever
    inet6 fe80::dea6:32ff:feb3:45e0/64 scope link
       valid_lft forever preferred_lft forever

答案2

顺便说一下,我在 IPv6 方面遇到了类似的问题,当我从 Ubuntu 18.04 升级到 20.04 并且 netplan 时,我注意到我现在有两个 IPv6 地址:

  1. 自动 DHCP?2001:19f0:6801:1dc3:5400:3ff:feeb:abea
  2. 我静态选择的自定义一个2001:19f0:6801:1dc3::1
> ip addr
[...]
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 56:00:03:eb:ab:ea brd ff:ff:ff:ff:ff:ff
    inet 217.69.14.107/23 brd 217.69.15.255 scope global enp1s0
       valid_lft forever preferred_lft forever
    inet6 2001:19f0:6801:1dc3:5400:3ff:feeb:abea/64 scope global dynamic mngtmpaddr noprefixroute 
       valid_lft 2591957sec preferred_lft 604757sec
    inet6 2001:19f0:6801:1dc3::1/64 scope global 
       valid_lft forever preferred_lft forever
    inet6 fe80::5400:3ff:feeb:abea/64 scope link 
       valid_lft forever preferred_lft forever
> curl ifconfig.io
2001:19f0:6801:1dc3:5400:3ff:feeb:abea

DHCP 是第一个,因此默认使用它,这不是我想要的(我希望使用更短的自定义 IPv6)。

我尝试使用 禁用 dhcp6,dhcp6: no但 DHCP 地址始终存在... 我也没有找到一种简单的方法来更改顺序以首先使用自定义地址。经过多次尝试和错误,我发现这是由“路由器通告”引起的,而不是由 DHCP 引起的,所以我不得不使用 禁用它accept-ra: no,并手动设置默认路由,因为这也是由路由器通告配置的。

我的最终配置文件如下:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp1s0:
      dhcp4: yes
      accept-ra: no # otherwise we get auto IPv6 too
      addresses: ['2001:19f0:6801:1dc3::1/64']
      gateway6: fe80::fc00:3ff:feeb:abea

(请注意,我在这里没有指定任何名称服务器,因为我正在运行本地解析器,但您可能必须这样做)

现在我终于只剩下自定义的 IPv6 了,而且它运行良好:

> ip addr
[...]
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 56:00:03:eb:ab:ea brd ff:ff:ff:ff:ff:ff
    inet 217.69.14.107/23 brd 217.69.15.255 scope global dynamic enp1s0
       valid_lft 86397sec preferred_lft 86397sec
    inet6 2001:19f0:6801:1dc3::1/64 scope global 
       valid_lft forever preferred_lft forever
    inet6 fe80::5400:3ff:feeb:abea/64 scope link 
       valid_lft forever preferred_lft forever
> curl ifconfig.io
2001:19f0:6801:1dc3::1

相关内容