如何让wlan始终成为默认路由? (ip路由列表)

如何让wlan始终成为默认路由? (ip路由列表)

每当我将 4G 调制解调器添加到树莓派时,它都会优先于默认路由 ou ip route list,但是我希望所有内容都通过 wlan,并且仅使用 4G 调制解调器接收 SSH 连接。

我发现了这个答案关于如何禁用默认路由。

但是,重新启动后,4G 调制解调器又回到顶部。

如何使wlan0默认规则始终成为第一条规则?

更新:

这是连接 USB 加密狗时的 dmesg 输出:

[426102.910168] usb 1-1.5.1: new full-speed USB device number 6 using dwc_otg
[426103.046670] usb 1-1.5.1: not running at top speed; connect to a high speed hub
[426103.056674] usb 1-1.5.1: New USB device found, idVendor=12d1, idProduct=1f01
[426103.056693] usb 1-1.5.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[426103.056704] usb 1-1.5.1: Product: HUAWEI_MOBILE
[426103.056714] usb 1-1.5.1: Manufacturer: HUAWEI_MOBILE
[426103.056724] usb 1-1.5.1: SerialNumber: 0123456789ABCDEF
[426103.121355] usb-storage 1-1.5.1:1.0: USB Mass Storage device detected
[426103.122875] scsi host0: usb-storage 1-1.5.1:1.0
[426103.987177] usb 1-1.5.1: USB disconnect, device number 6
[426105.470211] usb 1-1.5.1: new full-speed USB device number 7 using dwc_otg
[426105.606666] usb 1-1.5.1: not running at top speed; connect to a high speed hub
[426105.615673] usb 1-1.5.1: New USB device found, idVendor=12d1, idProduct=14dc
[426105.615692] usb 1-1.5.1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[426105.615703] usb 1-1.5.1: Product: HUAWEI_MOBILE
[426105.615713] usb 1-1.5.1: Manufacturer: HUAWEI_MOBILE
[426105.766297] usb-storage 1-1.5.1:1.2: USB Mass Storage device detected
[426105.766768] scsi host0: usb-storage 1-1.5.1:1.2
[426105.855053] cdc_ether 1-1.5.1:1.0 eth1: register 'cdc_ether' at usb-3f980000.usb-1.5.1, CDC Ethernet Device, 0c:5b:8f:27:9a:64
[426105.855593] usbcore: registered new interface driver cdc_ether
[426106.785653] scsi 0:0:0:0: Direct-Access     HUAWEI   TF CARD Storage  2.31 PQ: 0 ANSI: 2
[426106.803758] sd 0:0:0:0: Attached scsi generic sg0 type 0
[426106.820687] sd 0:0:0:0: [sda] Attached SCSI removable disk

这是ip addr

eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 0c:5b:8f:27:9a:64 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::584f:751f:bb3e:e26b/64 scope link 
       valid_lft forever preferred_lft forever

更新2

我又贴了几次,直到它显示了eth1路线:

[10787.229141] usb 1-1.5: new full-speed USB device number 7 using dwc_otg
[10787.363515] usb 1-1.5: New USB device found, idVendor=05e3, idProduct=0606
[10787.363533] usb 1-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[10787.363544] usb 1-1.5: Product: USB Hub 2.0
[10787.363555] usb 1-1.5: Manufacturer: ALCOR
[10787.365166] hub 1-1.5:1.0: USB hub found
[10787.369831] hub 1-1.5:1.0: 4 ports detected
[10797.419094] usb 1-1.5.1: new full-speed USB device number 8 using dwc_otg
[10797.555636] usb 1-1.5.1: not running at top speed; connect to a high speed hub
[10797.565759] usb 1-1.5.1: New USB device found, idVendor=12d1, idProduct=1f01
[10797.565777] usb 1-1.5.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[10797.565789] usb 1-1.5.1: Product: HUAWEI_MOBILE
[10797.565799] usb 1-1.5.1: Manufacturer: HUAWEI_MOBILE
[10797.565808] usb 1-1.5.1: SerialNumber: 0123456789ABCDEF
[10797.630477] usb-storage 1-1.5.1:1.0: USB Mass Storage device detected
[10797.631101] scsi host0: usb-storage 1-1.5.1:1.0
[10798.472745] usb 1-1.5.1: USB disconnect, device number 8
[10799.469081] usb 1-1.5.1: new full-speed USB device number 9 using dwc_otg
[10799.630768] usb 1-1.5.1: not running at top speed; connect to a high speed hub
[10799.646891] usb 1-1.5.1: New USB device found, idVendor=12d1, idProduct=14dc
[10799.646909] usb 1-1.5.1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[10799.646920] usb 1-1.5.1: Product: HUAWEI_MOBILE
[10799.646930] usb 1-1.5.1: Manufacturer: HUAWEI_MOBILE
[10799.814489] usb-storage 1-1.5.1:1.2: USB Mass Storage device detected
[10799.815008] scsi host0: usb-storage 1-1.5.1:1.2
[10799.897788] cdc_ether 1-1.5.1:1.0 eth1: register 'cdc_ether' at usb-3f980000.usb-1.5.1, CDC Ethernet Device, 0c:5b:8f:27:9a:64
[10799.898127] usbcore: registered new interface driver cdc_ether
[10800.889652] scsi 0:0:0:0: Direct-Access     HUAWEI   TF CARD Storage  2.31 PQ: 0 ANSI: 2
[10800.910585] sd 0:0:0:0: Attached scsi generic sg0 type 0
[10800.923297] sd 0:0:0:0: [sda] Attached SCSI removable disk

这是route -n

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 wlan0
0.0.0.0         192.168.8.1     0.0.0.0         UG    207    0        0 eth1
0.0.0.0         192.168.1.1     0.0.0.0         UG    303    0        0 wlan0
169.254.0.0     0.0.0.0         255.255.0.0     U     202    0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     204    0        0 docker0
169.254.0.0     0.0.0.0         255.255.0.0     U     206    0        0 veth4557ad2
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0
192.168.1.0     0.0.0.0         255.255.255.0   U     303    0        0 wlan0
192.168.8.0     0.0.0.0         255.255.255.0   U     207    0        0 eth1

看到我这样做是ifmetric wlan0为了能够在我的树莓派中wlan0使用ssh

2010 年 9 月更新:

allow-hotplug wlan0
iface wlan0 inet dhcp
    up ifmetric wlan0 0
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

这不会使我的wlan0指标为 0。我做错了什么?

答案1

要更改接口的路由优先级,您可以更改指标。

默认情况下全部为0,即最高优先级。所以,你可以这样做:

allow-hotplug eth1
iface eth1 inet dhcp
    up ifmetric eth1 30

要在 Debian 中使用ifmetric,您必须安装它:

sudo apt-get install ifmetric

如果度量

设置网络接口的路由指标

ifmetric 是一个 Linux 工具,用于一次性设置附加到给定网络接口的所有 IPv4 路由的度量。这可用于更改通过接口路由 IPv4 流量的优先级。较低的指标与较高的优先级相关。

度量值 0 表示最高优先级路由,并且是默认路由。度量值越大,意味着优先级越低的路由。具有最低度量值的活动接口的 IP 地址将成为原始接口。请参阅 ifmetric(8)。

答案2

这是如何覆盖默认路由的老问题。

IPv4 最简单的方法不是尝试更改度量/优先级,而是分割路由:

ip route add 0.0.0.0/1 via wlan0-gw
ip route add 128.0.0.0/1 via wlan0-gw

这样做的原因是路由表查找是使用最长匹配前缀搜索来执行的。仅当存在两条等效路由时,路由度量才会发挥作用。在这种情况下,这两条路由的 1 位前缀比默认路由 (0.0.0.0/0) 的 0 位前缀长,因此将首先协商。由于这两条路由覆盖了整个 0.0.0.0/0,因此它们始终优先于默认路由。

另一种方法是使用单独的路由表和您喜欢的默认路由,并添加规则以将所有流量发送到那里。我从未见过任何与 ip 规则相混淆的自动化,所以他们添加什么其他路由并不重要。这里的问题是您的自动化将无法插入额外的路线。

所以,长话短说:

iface wlan0 inet manual
    up ip route add 0.0.0.0/1 via wlan0-gw
    up ip route add 128.0.0.0/1 via wlan0-gw
    ....

或者(但您还必须将所有其他路由添加到“表 5”):

iface wlan0 inet manual
    up ip rule add to 0.0.0.0/0 table 5
    up ip route add default via wlan0-gw table 5
    ....

答案3

在 debian 中保留的一种方法ip route add/del是将它们写成/etc/network/interfaces如下所示:

allow-hotplug eth1
iface eth1 inet dhcp
    post-up ip route del default
    post-up ip route add default via <gateway IP> dev wlan0

答案4

我认为默认网关的问题。这是教程。

我简单解释一下步骤。首先,您需要删除默认网关并添加您要添加​​的默认网关。然后,编辑“/etc/network/interfaces”以进行永久更改。最后,重新启动该服务。

https://www.wikihow.com/Add-or-Change-the-Default-Gateway-in-Linux

相关内容