問題描述
Linux系統有兩塊網卡,一塊A(enp0s3,NAT,DHCP自動獲取)負責對外請求,一塊B(enp0s8,靜態IP,內網)負責對內.當只有A的時候,可以正常上網,但加入B以後,則無法上網.
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 1.1.1.8 netmask 255.255.255.0 broadcast 1.1.1.255
inet6 fe80::a00:27ff:fe58:72e6 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:58:72:e6 txqueuelen 1000 (Ethernet)
RX packets 741 bytes 57262 (57.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 636 bytes 88242 (88.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.3.15 netmask 255.255.255.0 broadcast 10.0.3.255
inet6 fe80::a00:27ff:fe5c:c020 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:5c:c0:20 txqueuelen 1000 (Ethernet)
RX packets 642 bytes 600938 (600.9 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 368 bytes 27118 (27.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
問題解決
使用route命令,發現系統多添加了一條默認路由
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 1.1.1.1 0.0.0.0 UG 0 0 0 enp0s3
default 10.0.3.2 0.0.0.0 UG 100 0 0 enp0s8
1.1.1.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
10.0.3.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s8
10.0.3.2 0.0.0.0 255.255.255.255 UH 100 0 0 enp0s8
第一條就是多添加的路由,而且他的優先級是0,比我需要的第二條的優先級100,要高
經過查資料發現,需要把網卡A enp0s8對應的網卡,設置Gateway,我一查,果然沒有
所以修改配置文件
如下,10.0.3.2是根據route命令查詢得來的
# 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:
ethernets:
enp0s8:
dhcp4: true
gateway4: 10.0.3.2
enp0s3:
addresses:
- 1.1.1.8/24
gateway4: 1.1.1.1
nameservers: {}
version: 2
保存,執行 netplan apply
再次執行,route命令,顯示如下,問題解決
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default one.one.one.one 0.0.0.0 UG 0 0 0 enp0s3
default _gateway 0.0.0.0 UG 0 0 0 enp0s8
default _gateway 0.0.0.0 UG 100 0 0 enp0s8
1.1.1.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
10.0.3.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s8
_gateway 0.0.0.0 255.255.255.255 UH 100 0 0 enp0s8