我正在使用 Ubuntu Focal 20
uname -a
Linux domaintf 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
带2个网络接口
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: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc fq_codel state UP group default qlen 1000
link/ether fa:16:3e:f3:a6:ef brd ff:ff:ff:ff:ff:ff
inet 10.20.10.226/24 brd 10.20.10.255 scope global dynamic ens3
valid_lft 85607sec preferred_lft 85607sec
inet6 fe80::f816:3eff:fef3:a6ef/64 scope link
valid_lft forever preferred_lft forever
3: ens4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc fq_codel state UP group default qlen 1000
link/ether fa:16:3e:63:bc:b5 brd ff:ff:ff:ff:ff:ff
inet 10.20.20.169/24 brd 10.20.20.255 scope global dynamic ens4
valid_lft 85607sec preferred_lft 85607sec
inet6 fe80::f816:3eff:fe63:bcb5/64 scope link
valid_lft forever preferred_lft forever
默认的 netplan 内容如下
cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. 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:
ens3:
dhcp4: true
match:
macaddress: fa:16:3e:f3:a6:ef
mtu: 1450
set-name: ens3
ens4:
dhcp4: true
match:
macaddress: fa:16:3e:63:bc:b5
mtu: 1450
set-name: ens4
默认情况下,我得到了两个具有相同度量的默认路由,这阻止我连接到可以由两者通告的地址
ip r
**default via 10.20.20.1 dev ens4 proto dhcp src 10.20.20.169 metric 100
default via 10.20.10.1 dev ens3 proto dhcp src 10.20.10.226 metric 100**
10.20.10.0/24 dev ens3 proto kernel scope link src 10.20.10.226
10.20.20.0/24 dev ens4 proto kernel scope link src 10.20.20.169
169.254.169.254 via 10.20.20.2 dev ens4 proto dhcp src 10.20.20.169 metric 100
169.254.169.254 via 10.20.10.1 dev ens3 proto dhcp src 10.20.10.226 metric 100
WA 就像修改 netplan 指标一样简单
s
udo cat /etc/netplan/50-cloud-init.yaml
sudo netpl# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. 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:
ens3:
dhcp4: true
**dhcp4-overrides:
route-metric: 100**
match:
macaddress: fa:16:3e:f3:a6:ef
mtu: 1450
set-name: ens3
ens4:
dhcp4: true
**dhcp4-overrides:**
**route-metric: 300**
match:
macaddress: fa:16:3e:63:bc:b5
mtu: 1450
set-name: ens4
ip r
default via 10.20.10.1 dev ens3 proto dhcp src 10.20.10.226 metric 100
**default via 10.20.20.1 dev ens4 proto dhcp src 10.20.20.169 metric 300**
10.20.10.0/24 dev ens3 proto kernel scope link src 10.20.10.226
10.20.20.0/24 dev ens4 proto kernel scope link src 10.20.20.169
169.254.169.254 via 10.20.10.1 dev ens3 proto dhcp src 10.20.10.226 metric 100
**169.254.169.254 via 10.20.20.2 dev ens4 proto dhcp src 10.20.20.169 metric 300**
最终,应该通过默认不允许两个具有相同度量的默认网关来修复该错误;那么作为临时的解决方法,我想请你帮助我如何通过 cloud init 进行配置
我尝试了以下配置,但没有效果
cat cloudinit2.txt
#cloud-config
password: password
chpasswd: { expire: False }
ssh_pwauth: True
write_files:
- path: /etc/netplan/50-cloud-init.yaml
permissions: '0644'
content: |
network:
version: 2
ethernets:
ens3:
dhcp4: true
dhcp4-overrides:
route-metric: 100
mtu: 1450
set-name: ens3
ens4:
dhcp4: true
dhcp4-overrides:
route-metric: 200
mtu: 1450
set-name: ens4
runcmd:
- sudo netplan apply
答案1
非常感谢 Lukas,我按照你的建议完成了配置
cat netcloud
#cloud-config
password: password
chpasswd: { expire: False }
ssh_pwauth: True
write_files:
- path: /etc/netplan/90-custom-config.yaml
permissions: '0644'
content: |
network:
version: 2
ethernets:
ens3:
dhcp4: true
dhcp4-overrides:
route-metric: 100
mtu: 1450
set-name: ens3
ens4:
dhcp4: true
dhcp4-overrides:
route-metric: 200
mtu: 1450
set-name: ens4
runcmd:
- sudo netplan apply