原因
在机器上插了一个智能网卡后,在netplan中设置了静态IP,如下:
cat /etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:version: 2renderer: NetworkManagerethernets:eth1:dhcp4: falseaddresses: [172.21.1.64/24]gateway4: 172.21.1.1nameservers:addresses: [219.141.136.10,172.21.1.1]ens4f4d1:dhcp4: falseaddresses: [192.168.2.3/24]gateway4: 192.168.2.1nameservers:addresses: [192.168.2.1]
netplan apply后IP正常,但是访问外网不通,检查DNS
# nmcli
ens4f4d1: connected to netplan-ens4f4d1"Chelsio T62100-608a"ethernet (cxgb4), 00:01:02:03:04:18, hw, mtu 1500ip4 defaultinet4 192.168.2.3/24route4 192.168.2.0/24route4 169.254.0.0/16route4 0.0.0.0/0inet6 fe80::201:2ff:fe03:418/64route6 fe80::/64enxb03af2b6059f: connected to Wired connection 4"Insyde Software RNDIS/Ethernet Gadget"ethernet (rndis_host), B0:3A:F2:B6:05:9F, hw, mtu 1500inet4 169.254.3.1/24route4 169.254.3.0/24inet6 fe80::44db:3433:3832:8104/64route6 fe80::/64eth1: connected to netplan-eth1"Aquantia C620 Chipset Root Port"ethernet (atlantic), 3C:EC:EF:98:7D:F2, hw, mtu 1500inet4 172.21.1.64/24route4 172.21.1.0/24route4 0.0.0.0/0inet6 fe80::3eec:efff:fe98:7df2/64route6 fe80::/64
# nmcli dev show
GENERAL.DEVICE: ens4f4d1
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:01:02:03:04:18
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: netplan-ens4f4d1
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 192.168.2.3/24
IP4.GATEWAY: 192.168.2.1
IP4.ROUTE[1]: dst = 192.168.2.0/24, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]: dst = 169.254.0.0/16, nh = 0.0.0.0, mt = 1000
IP4.ROUTE[3]: dst = 0.0.0.0/0, nh = 192.168.2.1, mt = 20100
IP4.DNS[1]: 192.168.2.1
IP6.ADDRESS[1]: fe80::201:2ff:fe03:418/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 256
提示ens4f4d1是默认网卡,有 ip4 default ,但我真正访问外网的主网卡应该是eth1
检查路由
# ip route show
default via 192.168.2.1 dev ens4f4d1 proto static metric 20100
default via 172.21.1.1 dev eth1 proto static metric 20102
169.254.0.0/16 dev ens4f4d1 scope link metric 1000
169.254.3.0/24 dev enxb03af2b6059f proto kernel scope link src 169.254.3.1 metric 101
172.21.1.0/24 dev eth1 proto kernel scope link src 172.21.1.64 metric 102
192.168.2.0/24 dev ens4f4d1 proto kernel scope link src 192.168.2.3 metric 100
可以看到192.168网段比172.21网段的eth1网卡优先级更高
此时可以在netplan中主网卡下指定优先级和路由
cat /etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:version: 2renderer: NetworkManagerethernets:eth1:dhcp4: falseaddresses: [172.21.1.64/24]gateway4: 172.21.1.1nameservers:addresses: [219.141.136.10,172.21.1.1]routes:- to: 0.0.0.0/0via: 172.21.1.1metric: 100ens4f4d1:dhcp4: falseaddresses: [192.168.2.3/24]gateway4: 192.168.2.1nameservers:addresses: [192.168.2.1]
再看路由
# ip route show
default via 172.21.1.1 dev eth1 proto static metric 100
default via 172.21.1.1 dev eth1 proto static metric 101
default via 192.168.2.1 dev ens4f4d1 proto static metric 20102
169.254.0.0/16 dev eth1 scope link metric 1000
169.254.3.0/24 dev enxb03af2b6059f proto kernel scope link src 169.254.3.1 metric 100
172.21.1.0/24 dev eth1 proto kernel scope link src 172.21.1.64 metric 101
192.168.2.0/24 dev ens4f4d1 proto kernel scope link src 192.168.2.3 metric 102
# nmcli device show
GENERAL.DEVICE: eth1
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 3C:EC:EF:98:7D:F2
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: netplan-eth1
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/2
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 172.21.1.64/24
IP4.GATEWAY: 172.21.1.1
IP4.ROUTE[1]: dst = 172.21.1.0/24, nh = 0.0.0.0, mt = 101
IP4.ROUTE[2]: dst = 169.254.0.0/16, nh = 0.0.0.0, mt = 1000
IP4.ROUTE[3]: dst = 0.0.0.0/0, nh = 172.21.1.1, mt = 101
IP4.ROUTE[4]: dst = 0.0.0.0/0, nh = 172.21.1.1, mt = 100
IP4.DNS[1]: 219.141.136.10
IP4.DNS[2]: 172.21.1.1
IP6.ADDRESS[1]: fe80::3eec:efff:fe98:7df2/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 256
eth1已经变成主网卡了,此时外网访问正常