![为具有各种指标/优先级的单独物理接口添加多个网关](https://linux22.com/image/46171/%E4%B8%BA%E5%85%B7%E6%9C%89%E5%90%84%E7%A7%8D%E6%8C%87%E6%A0%87%2F%E4%BC%98%E5%85%88%E7%BA%A7%E7%9A%84%E5%8D%95%E7%8B%AC%E7%89%A9%E7%90%86%E6%8E%A5%E5%8F%A3%E6%B7%BB%E5%8A%A0%E5%A4%9A%E4%B8%AA%E7%BD%91%E5%85%B3.png)
我的系统有 3 个接口。它们是 eth0、eth1 和 usb0。
eth0 链接到我的路由器,并且是我希望默认使用的线路。 eth1 未使用。 usb0 是一部联网的 3G 手机。
我的问题是我不希望能够同时通过 eth0 和 usb0 访问互联网。目前,一旦我更改网关以匹配每个接口,另一个连接就不再响应。
我尝试在接口文件中添加指标值:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo eth0 usb0
iface lo inet loopback
allow-hotplug eth0 usb0
# The primary network interface
iface eth0 inet static
address 192.168.1.67
netmask 255.255.255.0
broadcast 192.168.1.254
network 192.168.1.0
post-up route add default gw 192.168.1.254 metric 1
pre-down route del default gw 192.168.1.254
iface usb0 inet static
address 192.168.42.86
netmask 255.255.255.0
broadcast 192.168.42.254
network 192.168.42.0
post-up route add default gw 192.168.42.129 metric 2
pre-down route del default gw 192.168.42.129
不幸的是,当它启动时,实际上只有 usb0 可以访问。当我检查路由时,我可以看到 eth0 没有网关。
~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.42.129 0.0.0.0 UG 0 0 0 usb0
0.0.0.0 192.168.42.129 0.0.0.0 UG 2 0 0 usb0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.42.0 0.0.0.0 255.255.255.0 U 0 0 0 usb0
当我尝试通过它们访问互联网时。 eth0 给了我以下内容:
~# ping -I eth0 8.8.8.8
PING 8.8.8.8 (8.8.8.8) from 192.168.1.67 eth0: 56(84) bytes of data.
From 192.168.1.67 icmp_seq=1 Destination Host Unreachable
From 192.168.1.67 icmp_seq=2 Destination Host Unreachable
From 192.168.1.67 icmp_seq=3 Destination Host Unreachable
From 192.168.1.67 icmp_seq=4 Destination Host Unreachable
From 192.168.1.67 icmp_seq=5 Destination Host Unreachable
From 192.168.1.67 icmp_seq=6 Destination Host Unreachable
^C
--- 8.8.8.8 ping statistics ---
7 packets transmitted, 0 received, +6 errors, 100% packet loss, time 6030ms
但是,usb0 可以访问互联网。
~# ping -I usb0 8.8.8.8
PING 8.8.8.8 (8.8.8.8) from 192.168.42.86 usb0: 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_req=1 ttl=43 time=188 ms
64 bytes from 8.8.8.8: icmp_req=2 ttl=43 time=127 ms
64 bytes from 8.8.8.8: icmp_req=3 ttl=43 time=108 ms
64 bytes from 8.8.8.8: icmp_req=4 ttl=43 time=98.3 ms
64 bytes from 8.8.8.8: icmp_req=5 ttl=43 time=96.9 ms
64 bytes from 8.8.8.8: icmp_req=6 ttl=43 time=165 ms
64 bytes from 8.8.8.8: icmp_req=7 ttl=43 time=226 ms
^C
--- 8.8.8.8 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6002ms
rtt min/avg/max/mdev = 96.981/144.575/226.027/46.357 ms
我看不出哪里出了问题,在某些情况下(尽管不常见),我的 ping 中会出现以下内容:
64 bytes from 8.8.8.8: icmp_req=3 ttl=43 time=132 ms
64 bytes from 8.8.8.8: icmp_req=3 ttl=43 time=672 ms (DUP!)
64 bytes from 8.8.8.8: icmp_req=3 ttl=43 time=1212 ms (DUP!)
任何帮助将不胜感激。