Ssh

帶有 DNAT 和多個網關的 iptables:如何將回复路由到正確的網關?

  • May 16, 2013

對於具有 DNAT 和多個網關的場景,我在 Linux 客戶端上設置 iptables 規則和路由時遇到問題:

我們有兩個通往 Internet 的網關。第一個有固定IP,第二個提供更好的頻寬。兩個網關都對來自我們專用網路的傳出流量進行 SNAT。

在第一個具有固定 IP 的網關上,我為埠 22 設置了埠轉發,以便所有 SSH 流量都將轉發到我的 Linux 客戶端。

這很好用。

但前提是我將第一個網關用作 Linux 客戶端上的預設路由。

當我將 Linux 客戶端切換到第二個網關作為預設路由時,傳入的 SSH 連接不再起作用。

如何設置 Linux 客戶端將與傳入 SSH 連接相關的回複數據包發送到第一個網關,但將所有其他流量發送到第二個網關?

我在“根據服務將返回流量路由到正確網關”這個問題的已接受答案中找到了解決方案。

我已經在我的 Linux 客戶端上實現了這些規則:

# Default route is second gateway:
ip route add default via 10.0.0.2

# Create a routing table "FIXED" using our fixed IP gateway
echo "200 FIXED" >>/etc/iproute2/rt_tables
ip route add default table FIXED via 10.0.0.1

# Create a rule to route any packets marked "42" through FIXED:
ip rule add fwmark 42 table FIXED

# Finally, the iptables rule:
# Any outgoing traffic from source port 22 of my Linux client
# that has a destination inside our private network (10.0.0.0/24)
# is marked "42" (and therefore goes to FIXED):
iptables -t mangle -A OUTPUT ! -d 10.0.0.0/24 \
                            -p tcp -m tcp --sport 22 \
                            -j MARK --set-mark 42

引用自:https://serverfault.com/questions/508023