ubuntu 双网卡 默认 路由 问题

问题描述

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

你可能感兴趣的:(网络,ubuntu,网卡)