LXD:如何避免 ubuntu 18.04 中的双重默认网关?

LXD:如何避免 ubuntu 18.04 中的双重默认网关?

更新2

因为需要应用建议的解决方案,所以将其列为首要帖子。

在该问题的第二个答案中发现:netplan 配置的名称服务器是否可以取代(而不是合并)DHCP 名称服务器

该问题已在 github 上的 netplan 存储库中修复,并且很可能会在某个时候出现在 Ubuntu 中。

添加了两个新选项,dhcp4-overrides 和 dhcp6-overrides。要忽略来自 DHCP 的 DNS 服务器,您可以执行以下操作

有人知道如何在 Ubuntu 18.04 中更新 netplan 吗?

原始问题

我有一个带有 2 个网络接口的 LXD vm。如果我使用 Ubuntu 16.04(接口),则有 1 个默认网关。如果我使用 Ubuntu 18.04(netplan),则有 2 个默认网关。我该如何摆脱第二个网关?

Ubuntu 16.04配置:

root@servizi:/etc/network/interfaces.d# ls -lha
total 3.5K
drwxr-xr-x 2 root root   4 Jul 31  2017 .
drwxr-xr-x 7 root root   8 Jul 12  2017 ..
-rw-r--r-- 1 root root 367 Jul 30  2017 50-cloud-init.cfg
-rw-r--r-- 1 root root  97 Jul 31  2017 60-locale.cfg
root@servizi:/etc/network/interfaces.d# cat 50-cloud-init.cfg 
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
root@servizi:/etc/network/interfaces.d# cat 60-locale.cfg 
auto eth1
iface eth1 inet dhcp
        up route add -net 192.168.99.0 netmask 255.255.255.0 gw 10.0.1.1
root@servizi:/etc/network/interfaces.d# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.220.90.1     0.0.0.0         UG    0      0        0 eth0
10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
10.220.90.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.99.0    10.0.1.1        255.255.255.0   UG    0      0        0 eth1
root@servizi:/etc/network/interfaces.d# 

Ubuntu 18.04 配置

root@servizi2:/etc/netplan# ls -lh
total 2.0K
-rw-r--r-- 1 root root 381 Jan 24 17:30 50-cloud-init.yaml
-rw-r--r-- 1 root root 177 Feb  1 20:25 60-locale.yaml
root@servizi2:/etc/netplan# cat 50-cloud-init.yaml 
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    version: 2
    ethernets:
        eth0:
            dhcp4: true
root@servizi2:/etc/netplan# cat 60-locale.yaml 
# seconda scheda
network:
    version: 2
    ethernets:
        eth1:
            dhcp4: true
            routes:
            - to: 192.168.99.0/24
            via: 10.0.1.1

root@servizi2:/etc/netplan# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.220.90.1     0.0.0.0         UG    100    0        0 eth0
0.0.0.0         10.0.1.1        0.0.0.0         UG    100    0        0 eth1
10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
10.0.1.1        0.0.0.0         255.255.255.255 UH    100    0        0 eth1
10.220.90.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.220.90.1     0.0.0.0         255.255.255.255 UH    100    0        0 eth0
root@servizi2:/etc/netplan#

更新

尝试了@slangasek 的建议,第二个文件现在是:

root@servizi2:/etc/netplan# cat 60-locale.yaml 
# seconda scheda
network:
    version: 2
    ethernets:
        eth1:
            dhcp4: true
            dhcp4-overrides:
                    use-routes: false
            routes:
                - to: 192.168.99.0/24
                  via: 10.0.1.1

但是 netplan 尝试给出错误“未知密钥 dhcp4-overrides”

root@servizi2:/etc/netplan# netplan try
Error in network definition /etc/netplan/60-locale.yaml line 5 column 12: unknown key dhcp4-overrides

An error occurred: the configuration could not be generated

Reverting.
root@servizi2:/etc/netplan#

我能做什么?

答案1

看来 eth0 和 eth1 上都有来自 dhcp 的默认路由;并且您只想使用通过 eth0 的默认路由。

要忽略 dhcp 服务器提供的默认路由,您需要使用dhcp4-overrides,如下所示:

network:
    version: 2
    ethernets:
        eth1:
            dhcp4: true
            dhcp4-overrides:
                use-routes: false
            routes:
                - to: 192.168.99.0/24
                  via: 10.0.1.1

该选项仅在 netplan 0.95 及更高版本中受支持,目前在 Ubuntu 18.04 LTS 及更高版本中可用。

答案2

cat /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
network: {config: disabled}

cat /etc/netplan/50-cloud-init.yaml
network:
    ethernets:
        ens3:
            dhcp4: true
            dhcp4-overrides:
                use-routes: false
            gateway4: 10.2.100.101
            match:
                macaddress: 52:54:00:0e:ad:cc
            set-name: ens3
    version: 2

答案3

如果您必须为第二个 NIC 定义第二个默认网关,则可以使用此示例:

# networks
network_1 ens3: 10.0.10.0/24
network_2 ens8: 10.0.20.0/24
 
# /etc/netplan/50-cloud-init.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: yes
    ens8:
      dhcp4: yes
      dhcp4-overrides:
        use-routes: false
      routes:
        - to: default
          via: 10.0.20.1
          table: 200
        - to: 10.0.20.0/24
          via: 10.0.20.1
          table: 200
      routing-policy:
        - from: 10.0.20.0/24
          table: 200

也可以看看: https://netplan.readthedocs.io/en/latest/examples/#how-to-configure-source-routing

相关内容