如何排除 Linux iptable 連接埠轉送故障

如何排除 Linux iptable 連接埠轉送故障

已經有大量關於如何執行此操作的幫助和指南。但由於某種原因,我無法讓它工作,並且不知道如何解決它。
我有一個帶有私有 IP 的 RDS postgres 實例10.0.122.220。我還有一個有(是的)私人 IP 的堡壘主機10.0.94.67。我可以連接到堡壘主機上的端口,但不能連接到 RDS。因此,我嘗試將5432堡壘主機的連接埠轉送到5432RDS 實例的連接埠。
這是堡壘主機的狀態:

bastion$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc mq state UP group default qlen 1000
    inet 10.0.94.67/19 brd 10.0.95.255 scope global dynamic eth0
...

bastion$ ip route show | grep default
default via 10.0.64.1 dev eth0

bastion$ cat /proc/sys/net/ipv4/ip_forward
1

然後我加入了兩條NAT規則:

bastion# iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 5432 -j DNAT --to-destination 10.0.122.220

bastion# iptables -t nat -A POSTROUTING -o eth0 -p tcp --dport 5432 -d 10.0.122.220 -j SNAT --to-source 10.0.94.67

bastion# iptables -v -t nat -L -n
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DNAT       tcp  --  eth0   *       0.0.0.0/0            0.0.0.0/0            tcp dpt:5432 to:10.0.122.220

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 443 packets, 32660 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (policy ACCEPT 443 packets, 32660 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 SNAT       tcp  --  *      eth0    0.0.0.0/0            10.0.122.220         tcp dpt:5432 to:10.0.94.67

但我仍然無法借助 SSH 隧道連接到 RDS 實例:

my-machine$ ssh -v -NL 5432:10.0.94.67:5432 -i my-key [email protected]
debug1: Connection to port 5432 forwarding to 10.0.94.67 port 5432 requested.
debug1: channel 2: new [direct-tcpip]
channel 2: open failed: connect failed: Connection refused
debug1: channel 2: free: direct-tcpip: listening port 5432 for 10.0.94.67 port 5432, connect from 127.0.0.1 port 57447 to 127.0.0.1 port 5432, nchannels 3
debug1: Connection to port 5432 forwarding to 10.0.94.67 port 5432 requested.
debug1: channel 2: new [direct-tcpip]
channel 2: open failed: connect failed: Connection refused
debug1: channel 2: free: direct-tcpip: listening port 5432 for 10.0.94.67 port 5432, connect from 127.0.0.1 port 57448 to 127.0.0.1 port 5432, nchannels 3
... keeps repeating the above

我可以確認的是,RDS 已啟動、運行並回應,並且堡壘主機可以存取它,因為透過以下 SSH 隧道我可以連接到資料庫:

my-machine$ ssh -v -NL 5432:10.0.122.220:5432 -i my-key [email protected]

我錯過了什麼?我該如何排除故障?謝謝。

答案1

我有種感覺,你把事情過於複雜化了。當您使用 SSH 將連線轉送到資料庫時,請忘記 iptables 和 NAT,只需使用 SSH 直接轉送到資料庫伺服器:

使用:

my-machine$ ssh -v -NL 5432:10.0.122.220:5432 -i my-key [email protected]

代替:

my-machine$ ssh -v -NL 5432:10.0.94.67:5432 -i my-key [email protected]

解釋為什麼您的解決方案不起作用:本地產生的流量不經過 NAT 表的 PREROUTING 鏈,因此它不是 DNATted。使用 OUTPUT 表對本地產生的流量進行 DNAT:

bastion# iptables -t nat -A OUTPUT -p tcp --dport 5432 -j DNAT --to-destination 10.0.122.220

但正如我所說 - 這讓事情變得過於複雜。如果您選擇使用它,您可能還想在上述規則中匹配目標地址。

相關內容