如何在 Ubuntu 18.04 中配置 netplan 以实现 DHCP 和静态路由

如何在 Ubuntu 18.04 中配置 netplan 以实现 DHCP 和静态路由

我有一台运行 Ubuntu 18.04 的服务器,带有 2 个 NIC。每个 NIC 都连接到不同的网络。我当前的配置如下:

cat /etc/netplan/01-netcfg.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses: [10.3.1.102/24]
      gateway4: 10.3.1.20
      nameservers:
        addresses: [8.8.8.8, 10.3.1.5]
    eth1:
      dhcp4: yes
      dhcp6: no
      nameservers:
        addresses: [10.127.112.140, 10.126.11.1]
      routes:
        - to: 10.127.113.0/24
          via: 10.127.113.254
          metric: 100
        - to: 10.127.112.0/24
          via: 10.127.113.254
          metric: 100

但是,使用此配置,eth1 无法获取 IP。如果我将其修改为:

eth1:
  dhcp4: no
  dhcp6: no
  addresses: [10.127.113.253/24]
  nameservers:
    addresses: [10.127.112.140, 10.126.11.1]
  routes:
    - to: 10.127.113.0/24
      via: 10.127.113.254
      metric: 100
    - to: 10.127.112.0/24
      via: 10.127.113.254
      metric: 100

然后我得到了预期的 IP 和路由。但是,eth1 需要是 dhcp。关于如何在 netplan 中将 eth1 配置为 dhcp 并具有静态路由,有什么想法吗?

答案1

我们来尝试一下(添加“on-link: true”)

eth1:
  dhcp4: yes
  dhcp6: no
  nameservers:
    addresses: [10.127.112.140, 10.126.11.1]
  routes:
    - to: 10.127.113.0/24
      via: 10.127.113.254
      metric: 100
      on-link: true
    - to: 10.127.112.0/24
      via: 10.127.113.254
      metric: 100
      on-link: true

答案2

如果使用静态,请尝试使用此 netplan 配置:

    network:
      ethernets:
        ens18:
          dhcp4: false
          addresses: [10.0.15.122/24] # replace with your ip  
          gateway4: 10.0.15.1 # replace with your gateway
          nameservers:
            addresses:
            -10.2.1.2 # replace with yours
      version:2

sudo netplan generate
sudo netplan apply

相关内容