Ubuntu 20.04 - 使用 Netplan 命名 NIC 并匹配 MAC

Ubuntu 20.04 - 使用 Netplan 命名 NIC 并匹配 MAC

在这里提问之前我已经做了研究;尝试了很多不同的方法,但我已经迷茫了。

我正在运行 20.04(修改版,ESXi 上的 GNS3 VM),有 10 个 NIC。如果我不时更改设置或拓扑,NIC 名称也会更改,因此我需要它们持久化。

我正在运行的主要应用程序需要旧的命名约定,在我的例子中是从 Eth0 到 Eth9。没关系。

以下是应用程序在 /etc/netplan 中使用的文件内容:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      match:
        macaddress: 00:50:56:00:0a:01
      dhcp4: no
      addresses:
        - 192.168.100.3/24
      gateway4: 192.168.100.1
      nameservers:
        addresses: [1.1.1.1, 8.8.8.8]
    eth1:
      match:
        macaddress: 00:50:56:00:0a:04
      dhcp4: yes
    eth2:
      match:
        macaddress: 00:50:56:00:0b:01
      dhcp4: yes
    eth3:
      match:
        macaddress: 00:50:56:00:0b:02
      dhcp4: yes
    eth4:
      match:
        macaddress: 00:50:56:00:0b:03
      dhcp4: yes
    eth5:
      match:
        macaddress: 00:50:56:00:0b:04
      dhcp4: yes
    eth6:
      match:
        macaddress: 00:50:56:00:0c:01
      dhcp4: yes
    eth7:
      match:
        macaddress: 00:50:56:00:0c:02
      dhcp4: yes
    eth8:
      match:
        macaddress: 00:50:56:00:0c:03
      dhcp4: yes
    eth9:
      match:
        macaddress: 00:50:56:00:0c:04
      dhcp4: yes

当我运行 netplan apply 时似乎没有问题;但是 sudo netplan --debug generate 的实际输出让我有不同的看法:

** (generate:1473): DEBUG: 14:22:26.352: Processing input file /etc/netplan/01-netcfg.yaml..
** (generate:1473): DEBUG: 14:22:26.353: Processing input file /etc/netplan/90_gns3vm_static_netcfg.yaml..
** (generate:1473): DEBUG: 14:22:26.353: starting new processing pass
** (generate:1473): DEBUG: 14:22:26.353: We have some netdefs, pass them through a final round of validation
** (generate:1473): DEBUG: 14:22:26.354: eth5: setting default backend to 1
** (generate:1473): DEBUG: 14:22:26.354: Configuration is valid
** (generate:1473): DEBUG: 14:22:26.354: eth4: setting default backend to 1
** (generate:1473): DEBUG: 14:22:26.354: Configuration is valid
** (generate:1473): DEBUG: 14:22:26.354: eth3: setting default backend to 1
** (generate:1473): DEBUG: 14:22:26.354: Configuration is valid
** (generate:1473): DEBUG: 14:22:26.354: eth9: setting default backend to 1
** (generate:1473): DEBUG: 14:22:26.354: Configuration is valid
** (generate:1473): DEBUG: 14:22:26.354: eth2: setting default backend to 1
** (generate:1473): DEBUG: 14:22:26.354: Configuration is valid
** (generate:1473): DEBUG: 14:22:26.354: eth8: setting default backend to 1
** (generate:1473): DEBUG: 14:22:26.354: Configuration is valid
** (generate:1473): DEBUG: 14:22:26.354: eth1: setting default backend to 1
** (generate:1473): DEBUG: 14:22:26.354: Configuration is valid
** (generate:1473): DEBUG: 14:22:26.354: eth7: setting default backend to 1
** (generate:1473): DEBUG: 14:22:26.354: Configuration is valid
** (generate:1473): DEBUG: 14:22:26.354: eth0: setting default backend to 1
** (generate:1473): DEBUG: 14:22:26.354: Configuration is valid
** (generate:1473): DEBUG: 14:22:26.354: eth6: setting default backend to 1
** (generate:1473): DEBUG: 14:22:26.354: Configuration is valid
** (generate:1473): DEBUG: 14:22:26.355: Generating output files..
** (generate:1473): DEBUG: 14:22:26.355: openvswitch: definition eth0 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.355: NetworkManager: definition eth0 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.355: openvswitch: definition eth1 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.355: NetworkManager: definition eth1 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.356: openvswitch: definition eth2 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.356: NetworkManager: definition eth2 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.356: openvswitch: definition eth3 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.356: NetworkManager: definition eth3 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.356: openvswitch: definition eth4 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.356: NetworkManager: definition eth4 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.356: openvswitch: definition eth5 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.356: NetworkManager: definition eth5 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.356: openvswitch: definition eth6 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.356: NetworkManager: definition eth6 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.357: openvswitch: definition eth7 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.357: NetworkManager: definition eth7 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.357: openvswitch: definition eth8 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.357: NetworkManager: definition eth8 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.357: openvswitch: definition eth9 is not for us (backend 1)
** (generate:1473): DEBUG: 14:22:26.357: NetworkManager: definition eth9 is not for us (backend 1)
(generate:1473): GLib-DEBUG: 14:22:26.357: posix_spawn avoided (fd close requested) 
(generate:1473): GLib-DEBUG: 14:22:26.365: posix_spawn avoided (fd close requested) 

dmesg | grep eth 的输出

[    4.621720] e1000e 0000:03:00.0 eth0: (PCI Express:2.5GT/s:Width x1) 00:50:56:00:0a:01
[    4.621723] e1000e 0000:03:00.0 eth0: Intel(R) PRO/1000 Network Connection
[    4.621865] e1000e 0000:03:00.0 eth0: MAC: 3, PHY: 8, PBA No: 000000-000
[    4.733469] e1000e 0000:04:00.0 eth1: (PCI Express:2.5GT/s:Width x1) 00:50:56:00:0b:03
[    4.733472] e1000e 0000:04:00.0 eth1: Intel(R) PRO/1000 Network Connection
[    4.733577] e1000e 0000:04:00.0 eth1: MAC: 3, PHY: 8, PBA No: 000000-000
[    4.845864] e1000e 0000:05:00.0 eth2: (PCI Express:2.5GT/s:Width x1) 00:50:56:00:0c:03
[    4.845868] e1000e 0000:05:00.0 eth2: Intel(R) PRO/1000 Network Connection
[    4.845977] e1000e 0000:05:00.0 eth2: MAC: 3, PHY: 8, PBA No: 000000-000
[    4.956955] e1000e 0000:0b:00.0 eth3: (PCI Express:2.5GT/s:Width x1) 00:50:56:00:0a:04
[    4.956959] e1000e 0000:0b:00.0 eth3: Intel(R) PRO/1000 Network Connection
[    4.957087] e1000e 0000:0b:00.0 eth3: MAC: 3, PHY: 8, PBA No: 000000-000
[    5.070404] e1000e 0000:0c:00.0 eth4: (PCI Express:2.5GT/s:Width x1) 00:50:56:00:0b:04
[    5.070407] e1000e 0000:0c:00.0 eth4: Intel(R) PRO/1000 Network Connection
[    5.070531] e1000e 0000:0c:00.0 eth4: MAC: 3, PHY: 8, PBA No: 000000-000
[    5.182248] e1000e 0000:0d:00.0 eth5: (PCI Express:2.5GT/s:Width x1) 00:50:56:00:0c:04
[    5.182251] e1000e 0000:0d:00.0 eth5: Intel(R) PRO/1000 Network Connection
[    5.182374] e1000e 0000:0d:00.0 eth5: MAC: 3, PHY: 8, PBA No: 000000-000
[    5.294284] e1000e 0000:13:00.0 eth6: (PCI Express:2.5GT/s:Width x1) 00:50:56:00:0b:01
[    5.294287] e1000e 0000:13:00.0 eth6: Intel(R) PRO/1000 Network Connection
[    5.294410] e1000e 0000:13:00.0 eth6: MAC: 3, PHY: 8, PBA No: 000000-000
[    5.406343] e1000e 0000:14:00.0 eth7: (PCI Express:2.5GT/s:Width x1) 00:50:56:00:0c:01
[    5.406346] e1000e 0000:14:00.0 eth7: Intel(R) PRO/1000 Network Connection
[    5.406471] e1000e 0000:14:00.0 eth7: MAC: 3, PHY: 8, PBA No: 000000-000
[    5.518208] e1000e 0000:1b:00.0 eth8: (PCI Express:2.5GT/s:Width x1) 00:50:56:00:0b:02
[    5.518211] e1000e 0000:1b:00.0 eth8: Intel(R) PRO/1000 Network Connection
[    5.518335] e1000e 0000:1b:00.0 eth8: MAC: 3, PHY: 8, PBA No: 000000-000
[    5.631213] e1000e 0000:1c:00.0 eth9: (PCI Express:2.5GT/s:Width x1) 00:50:56:00:0c:02
[    5.631216] e1000e 0000:1c:00.0 eth9: Intel(R) PRO/1000 Network Connection
[    5.631408] e1000e 0000:1c:00.0 eth9: MAC: 3, PHY: 8, PBA No: 000000-000
[    9.627269] e1000e 0000:1c:00.0 eth9: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[    9.628754] IPv6: ADDRCONF(NETDEV_CHANGE): eth9: link becomes ready
[    9.729352] e1000e 0000:1b:00.0 eth8: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[    9.813525] IPv6: ADDRCONF(NETDEV_CHANGE): eth8: link becomes ready
[    9.817031] e1000e 0000:14:00.0 eth7: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[    9.913124] e1000e 0000:13:00.0 eth6: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[   10.001200] IPv6: ADDRCONF(NETDEV_CHANGE): eth7: link becomes ready
[   10.002240] IPv6: ADDRCONF(NETDEV_CHANGE): eth6: link becomes ready
[   10.004976] e1000e 0000:0d:00.0 eth5: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[   10.096982] e1000e 0000:0c:00.0 eth4: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[   10.099025] IPv6: ADDRCONF(NETDEV_CHANGE): eth5: link becomes ready
[   10.100038] IPv6: ADDRCONF(NETDEV_CHANGE): eth4: link becomes ready
[   10.204745] e1000e 0000:0b:00.0 eth3: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[   10.296669] e1000e 0000:05:00.0 eth2: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[   10.416662] e1000e 0000:04:00.0 eth1: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[   10.509219] e1000e 0000:03:00.0 eth0: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[   10.511526] IPv6: ADDRCONF(NETDEV_CHANGE): eth3: link becomes ready
[   10.512287] IPv6: ADDRCONF(NETDEV_CHANGE): eth2: link becomes ready
[   10.512964] IPv6: ADDRCONF(NETDEV_CHANGE): eth1: link becomes ready
[   10.513602] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready

ip a 的输出

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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:50:56:00:0a:01 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    altname ens160
    inet 192.168.100.3/24 brd 192.168.100.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::250:56ff:fe00:a01/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:50:56:00:0b:03 brd ff:ff:ff:ff:ff:ff
    altname enp4s0
    altname ens161
    inet6 fe80::250:56ff:fe00:b03/64 scope link 
       valid_lft forever preferred_lft forever
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:50:56:00:0c:03 brd ff:ff:ff:ff:ff:ff
    altname enp5s0
    altname ens162
    inet6 fe80::250:56ff:fe00:c03/64 scope link 
       valid_lft forever preferred_lft forever
5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:50:56:00:0a:04 brd ff:ff:ff:ff:ff:ff
    altname enp11s0
    altname ens192
    inet6 fe80::250:56ff:fe00:a04/64 scope link 
       valid_lft forever preferred_lft forever
6: eth4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:50:56:00:0b:04 brd ff:ff:ff:ff:ff:ff
    altname enp12s0
    altname ens193
    inet6 fe80::250:56ff:fe00:b04/64 scope link 
       valid_lft forever preferred_lft forever
7: eth5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:50:56:00:0c:04 brd ff:ff:ff:ff:ff:ff
    altname enp13s0
    altname ens194
    inet6 fe80::250:56ff:fe00:c04/64 scope link 
       valid_lft forever preferred_lft forever
8: eth6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:50:56:00:0b:01 brd ff:ff:ff:ff:ff:ff
    altname enp19s0
    altname ens224
    inet6 fe80::250:56ff:fe00:b01/64 scope link 
       valid_lft forever preferred_lft forever
9: eth7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:50:56:00:0c:01 brd ff:ff:ff:ff:ff:ff
    altname enp20s0
    altname ens225
    inet6 fe80::250:56ff:fe00:c01/64 scope link 
       valid_lft forever preferred_lft forever
10: eth8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:50:56:00:0b:02 brd ff:ff:ff:ff:ff:ff
    altname enp27s0
    altname ens256
    inet6 fe80::250:56ff:fe00:b02/64 scope link 
       valid_lft forever preferred_lft forever
11: eth9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:50:56:00:0c:02 brd ff:ff:ff:ff:ff:ff
    altname enp28s0
    altname ens257
    inet6 fe80::250:56ff:fe00:c02/64 scope link 
       valid_lft forever preferred_lft forever
12: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:6f:81:c9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
13: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:6f:81:c9 brd ff:ff:ff:ff:ff:ff
14: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:02:d0:f1:69 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

当我开始尝试时,我意识到没有安装网络管理器、cloud-init,也没有安装 /etc/network/interfaces(空)中的任何东西。

任何想法或解决方案都将不胜感激。

答案1

您是否使用类似选项net.ifnames=0 biosdevname=0来禁用可预测的网络接口名称?

在你的网络计划配置中,您使用的 ID 并不意味着接口将以与该 ID 匹配的方式命名。从netplan 参考

每个设备类型定义映射(如以太网:)下方的关键名称称为“ID”。

如果有 match: 规则,则 ID 字段是一个纯粹不透明的名称,仅用于引用

set-name使用这样的配置可能会更好

network:
  version: 2
  renderer: networkd
  ethernets:
    nic0:
      match:
        macaddress: 00:50:56:00:0a:01
      dhcp4: no
      addresses:
        - 192.168.100.3/24
      gateway4: 192.168.100.1
      nameservers:
        addresses: [1.1.1.1, 8.8.8.8]
      set-name: eth0
    nic1:
      match:
        macaddress: 00:50:56:00:0a:04
      dhcp4: yes
      set-name: eth1
...

我不确定配置将如何与您当前的设置交互,因为您的接口似乎已被命名ethX。这就是为什么我想知道您是否禁用了可预测的网络接口命名。

相关内容