Ubuntu16双网卡上网设置

前言

现在有两个网络:

  • 公司的局域网192.168.3.x, 通过有线连接, 可以访问公司内网.
  • 无线连接的192.168.1.x, 可以访问外网.

想要无线主力, 只有访问内网东西时采用有线, 常见做法是用到内网插有线(会自动顶掉无线), 否则拔掉网线用无线. 有没有简单点的办法?

route

先连上无线, 再插上有线(此时网络连接自动顶掉无线), 输入ifconfig:

$ ifconfig
enp0s31f6 Link encap:Ethernet  HWaddr 78:d0:04:26:ab:61
          inet addr:192.168.3.95  Bcast:192.168.3.255  Mask:255.255.255.0
          inet6 addr: fe80::6280:ab07:52a5:50cd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:19120 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7090 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:9736204 (9.7 MB)  TX bytes:853531 (853.5 KB)
          Interrupt:16 Memory:c3800000-c3820000

wlx00117f1391ba Link encap:Ethernet  HWaddr 00:11:7f:13:91:ba
          inet addr:192.168.1.55  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::3070:d0c1:1e6e:9889/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:25259 errors:0 dropped:0 overruns:0 frame:0
          TX packets:20818 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:17369532 (17.3 MB)  TX bytes:3411081 (3.4 MB)

上面enp0s31f6是有线网络, 下面wlx00117f1391ba是USB WiFi. 都是DHCP获取的网络, 输入route查看路由表:

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         localhost       0.0.0.0         UG    100    0        0 enp0s31f6
default         bogon           0.0.0.0         UG    600    0        0 wlx00117f1391ba
link-local      *               255.255.0.0     U     1000   0        0 wlx00117f1391ba
192.168.1.0     *               255.255.255.0   U     600    0        0 wlx00117f1391ba
192.168.3.0     *               255.255.255.0   U     100    0        0 enp0s31f6

默认网关有两个, 发现enp0s31f6Metric(跃点数)为100, 比wlx00117f1391baMetric值600低, Metric低的默认路由生效, 所以有线顶替了无线, Ubuntu访问不到无线的东西了.

方法一. 删网关

删掉有线的网关(一般是x.x.x.1):

sudo route del default gw 192.168.3.1

重连无线网络, 再用route看一下路由表:

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         router.asus.com 0.0.0.0         UG    600    0        0 wlx00117f1391ba
link-local      *               255.255.0.0     U     1000   0        0 wlx00117f1391ba
192.168.1.0     *               255.255.255.0   U     600    0        0 wlx00117f1391ba
192.168.3.0     *               255.255.255.0   U     100    0        0 enp0s31f6

默认网关变成了一个, 无线的生效, 此时访问内网外网均可以. ssh通过有线网段连接192.168.3.95, 或者通过无线网段连接192.168.1.55, 都没有问题.

其实也可以route del 0.0.0.0, 把默认网关都删除, 然后用route add新增一个网关就可以了.

方法二. 增大Metric

把上面方法删掉的有线网关加回来:

sudo route add default gw 192.168.3.1

把有线的Metric(跃点数)加大:

sudo route del default gw 192.168.3.1 metric 100	## 先删除默认的
sudo route add default gw 192.168.3.1 metric 1000	## 添加加大metric后的网关

再用route看一下:

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         localhost       0.0.0.0         UG    600    0        0 wlx00117f1391ba
default         localhost       0.0.0.0         UG    1000   0        0 enp0s31f6
link-local      *               255.255.0.0     U     1000   0        0 wlx00117f1391ba
192.168.1.0     *               255.255.255.0   U     600    0        0 wlx00117f1391ba
192.168.3.0     *               255.255.255.0   U     100    0        0 enp0s31f6

无线的Metric小, 顶到了前面, 无线路由生效.

注意

重新插拔网线后配置消失… 有不消失的办法么? 应该有的吧. 也可以配置内网的为静态IP, 配置IP地址, 子网掩码, 留下网关悬空. 外网还是DHCP.

微信公众号

欢迎扫描关注我的微信公众号, 及时获取最新文章:
在这里插入图片描述

发布了203 篇原创文章 · 获赞 105 · 访问量 38万+

猜你喜欢

转载自blog.csdn.net/weifengdq/article/details/103758290