使用 iptables 將除一個連接埠之外的所有內容重新路由到另一台伺服器

使用 iptables 將除一個連接埠之外的所有內容重新路由到另一台伺服器

我已經轉移到一個新的網頁伺服器,現在我將所有流量重新導向到新伺服器

echo "1" > /proc/sys/net/ipv4/ip_forward
#clear old rules:
iptables -F
iptables -t nat -F
#masquerade and redirect everything:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -A PREROUTING -i eth0 -d 1.1.1.1 -j DNAT --to-destination 2.2.2.2

直到所有 DNS 條目都廣泛傳播之前,它運作得很好。但現在我無法再登入舊機器了。

如何在舊伺服器 1.1.1.1 上保持開啟 SSH 連接埠?

答案1

在 iptables 中,第一個匹配的規則獲勝。因此,規則的正確排序很重要。

在這種情況下,必須制定對連接埠 22 的流量進行處理的規則規定如何處理「其他一切」的規則。雖然如果鏈的策略與預設值相同,則 RETURN 會起作用,但明確地使用 ACCEPT 作為「正常處理」的同義詞可能會更清楚:

echo "1" > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -A PREROUTING -i eth0 -m tcp -p tcp --dport 22 -j ACCEPT
iptables -t nat -A PREROUTING -i eth0 -d 1.1.1.1 -j DNAT --to-destination 2.2.2.2

相關內容