Ubuntu实现双网卡外网访问

使用场景:办公网络用来处理日常工作事物,同时使用无线访问外网,搜索查询所需的材料。

由于内外网段是确定可控的,因此考虑默认路由走外网,需要访问的内网网段通过静态路由投递到对应的网关,这些设置中Metric是关键,表示通信的代价,数据包选择最小的值进行转发。

执行ifconfig查看当前网卡的ip状况。

enp0s31f6: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 100.101.44.100  netmask 255.255.255.0  broadcast 100.101.44.255
        inet6 fe80::c687:b2a5:27e8:ebd4  prefixlen 64  scopeid 0x20<link>
        ether 8c:16:45:6e:15:ff  txqueuelen 1000  (Ethernet)
        RX packets 1544  bytes 166833 (166.8 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2885  bytes 282031 (282.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 16  memory 0xf1300000-f1320000  

wlp5s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.100  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::ba5f:53e8:daa6:ccea  prefixlen 64  scopeid 0x20<link>
        ether 94:b8:6d:3b:e1:ff  txqueuelen 1000  (Ethernet)
        RX packets 90437  bytes 83894639 (83.8 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 83414  bytes 12013802 (12.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

其中的一些虚拟网卡忽略即可,enp0s31f6设备就是配置的重点。

执行route查看当前的路由表情况。

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         _gateway        0.0.0.0         UG    600    0        0 wlp5s0
default         _gateway        0.0.0.0         UG    20100  0        0 enp0s31f6
100.101.44.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s31f6
link-local      0.0.0.0         255.255.0.0     U     1000   0        0 enp0s31f6
192.168.43.0    0.0.0.0         255.255.255.0   U     600    0        0 wlp5s0

添加三条示例性的静态路由。

sudo route add -net 100.100.54.0/24 gw 100.101.44.1 dev enp0s31f6
sudo route add -net 100.100.57.0/24 gw 100.101.44.1 dev enp0s31f6
sudo route add -net 100.101.43.0/24 gw 100.101.44.1 dev enp0s31f6

之后的路由表如下所示。

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         _gateway        0.0.0.0         UG    600    0        0 wlp5s0
default         _gateway        0.0.0.0         UG    20100  0        0 enp0s31f6
100.100.54.0    _gateway        255.255.255.0   UG    0      0        0 enp0s31f6
100.100.57.0    _gateway        255.255.255.0   UG    0      0        0 enp0s31f6
100.101.43.0    _gateway        255.255.255.0   UG    0      0        0 enp0s31f6
100.101.44.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s31f6
link-local      0.0.0.0         255.255.0.0     U     1000   0        0 enp0s31f6
192.168.43.0    0.0.0.0         255.255.255.0   U     600    0        0 wlp5s0

由此实现双网卡的静态路由。

猜你喜欢

转载自blog.csdn.net/mscf/article/details/82218878