如何強制 ssh 使用具有更高指標的第二個介面?

如何強制 ssh 使用具有更高指標的第二個介面?

我有一個 Crunchbang VM,有兩個介面eth0eth1,每個介面都連接到一個 OpenWRT VM(eth0being10.232.64.20eth1being 10.232.65.20)。我正在使用網路管理器和 DHCP。我的總體目標是擁有多個ssh連接,並將它們與ifenslave.

預設情況下,eth1(由於某種原因)是預設網關:

user@crunchbang:~$ ip ro
default via 10.232.65.1 dev eth1  proto static
10.232.64.0/24 dev eth0  proto kernel  scope link  src 10.232.64.20
10.232.65.0/24 dev eth1  proto kernel  scope link  src 10.232.65.20

我新增了一條路線eth0

user@crunchbang:~$ sudo ip route add default via 10.232.64.1 dev eth0  proto static metric 1

那我有兩條路線:

user@crunchbang:~$ ip ro
default via 10.232.65.1 dev eth1  proto static
default via 10.232.64.1 dev eth0  proto static  metric 1
10.232.64.0/24 dev eth0  proto kernel  scope link  src 10.232.64.20
10.232.65.0/24 dev eth1  proto kernel  scope link  src 10.232.65.20

但是,ssh只能透過以下方式出去eth1

user@crunchbang:~$ ssh -b 10.232.64.20 [email protected]
ssh: connect to host 1.2.3.4 port 22: Connection timed out

user@crunchbang:~$ ssh -b 10.232.65.20 [email protected]
Enter passphrase for key '/home/user/.ssh/id_rsa': 

更改eth0指標後我有:

user@crunchbang:~$ ip ro
default via 10.232.64.1 dev eth0  proto static  metric 1
default via 10.232.65.1 dev eth1  proto static  metric 2
10.232.64.0/24 dev eth0  proto kernel  scope link  src 10.232.64.20
10.232.65.0/24 dev eth1  proto kernel  scope link  src 10.232.65.20

現在ssh只能用以下方式出去eth0

user@crunchbang:~$ ssh -b 10.232.64.20 [email protected]
Enter passphrase for key '/home/user/.ssh/id_rsa': 

user@crunchbang:~$ ssh -b 10.232.65.20 [email protected]
ssh: connect to host 1.2.3.4 port 22: Connection timed out

如何強制ssh使用具有更高指標的介面?

編輯

我已經實現並測試了配置4.2.多個上行鏈路/提供者的路由Linux 進階路由和流量控制 HOWTO 的部分。鑑於配置很簡單,並且我沒有遇到錯誤,我將只顯示程式碼和結果,並進行最少的解釋。

root@crunchbang:~# ip route add 10.232.64.0/24 dev eth0 src 10.232.64.20 table T0
root@crunchbang:~# ip route add default via 10.232.64.1 table T0
root@crunchbang:~# ip route add 10.232.65.0/24 dev eth1 src 10.232.65.20 table T1
root@crunchbang:~# ip route add default via 10.232.65.1 table T1
root@crunchbang:~# ip route flush table main
root@crunchbang:~# ip route add 10.232.64.0/24 dev eth0 src 10.232.64.20
root@crunchbang:~# ip route add 10.232.65.0/24 dev eth1 src 10.232.65.20
root@crunchbang:~# ip rule add from 10.232.64.20 table T0
root@crunchbang:~# ip rule add from 10.232.65.20 table T1
root@crunchbang:~# ip route add default scope global nexthop via 10.232.64.1 dev eth0 weight 1 nexthop via 10.232.65.1 dev eth1 weight 1

以下是產生的路由表:

root@crunchbang:~# ip route show table T0
default via 10.232.64.1 dev eth0 
10.232.64.0/24 dev eth0  scope link  src 10.232.64.20 

root@crunchbang:~# ip route show table T1
default via 10.232.65.1 dev eth1 
10.232.65.0/24 dev eth1  scope link  src 10.232.65.20 

root@crunchbang:~# ip ro
default 
    nexthop via 10.232.64.1  dev eth0 weight 1
    nexthop via 10.232.65.1  dev eth1 weight 1
10.232.64.0/24 dev eth0  scope link  src 10.232.64.20 
10.232.65.0/24 dev eth1  scope link  src 10.232.65.20 

透過此配置,ssh 透過兩個介面進行連接:

user@crunchbang:~$ ssh -b 10.232.64.20 [email protected]
Enter passphrase for key '/home/user/.ssh/id_rsa': 

user@crunchbang:~$ ssh -b 10.232.65.20 [email protected]
Enter passphrase for key '/home/user/.ssh/id_rsa': 

但是,看來我確實需要失去網路管理器。如果有人能解釋為什麼這是一個壞主意,或警告陷阱,我將不勝感激。

編輯2

刪除網路管理器進展順利。我只有最後一個問題。當前啟動時載入配置的標準方法是什麼?

答案1

首先,你的問題解決方案很好。其次,這取決於作業系統。 Crunchbag 是基於 Debian,因此這個解決方案可以完成這項工作:

https://serverfault.com/questions/487939/permanently-adding-source-policy-routing-rules

在基於 RHEL 的系統上,也可以新增<ifname>-rule<ifname>-route

相關內容