我使用以下命令在 Linux 筆記型電腦和遠端伺服器之間設定了反向 ssh 隧道:
ssh -4nNT -R 2222:localhost:22 somehost.com
也就是說,可以使用以下命令透過 ssh 存取位於防火牆後面的筆記型電腦:
ssh -p 2222 -l joe somehost.com
在 somehost.com 的 sshd_config 上,我啟用了 Gatewayports=yes。
我很高興地說所有這些工作都很好。然而,有一件事打敗了我:somehost.com 上有一個iptables
運行程式沒有打開連接埠 2222。儘管這條隧道有效,但怎麼可能呢?遠端 ssh 隧道在幕後如何運作?誰能解釋一下嗎?
這是 iptables -L 的輸出:
target prot opt source destination
ACCEPT icmp -- anywhere anywhere icmp destination-unreachable
ACCEPT icmp -- anywhere anywhere icmp time-exceeded
ACCEPT icmp -- anywhere anywhere icmp echo-request
ACCEPT icmp -- anywhere anywhere icmp echo-reply
DROP tcp -f anywhere anywhere
DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/FIN,SYN,RST,PSH,ACK,URG
DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/NONE
DROP tcp -- anywhere anywhere tcp flags:FIN,SYN/FIN,SYN
DROP tcp -- anywhere anywhere tcp flags:FIN,ACK/FIN
DROP tcp -- anywhere anywhere tcp flags:SYN,RST/SYN,RST
DROP tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,PSH,ACK,URG/FIN,SYN,RST,ACK,URG
DROP udp -- anywhere anywhere udp spt:bootps dpt:bootpc
DROP tcp -- anywhere anywhere tcp dpt:kazaa
DROP udp -- anywhere anywhere udp dpt:kazaa
LOG tcp -- anywhere somehost.com tcp dpt:ssh state NEW LOG level warning tcp-options ip-options prefix "firewall-> ssh1: "
ACCEPT tcp -- anywhere somehost.com tcp dpt:ssh
LOG tcp -- anywhere somehost.com tcp dpt:2023 state NEW LOG level warning tcp-options ip-options prefix "firewall-> Check: "
ACCEPT tcp -- anywhere somehost.com tcp dpt:2023
LOG tcp -- anywhere somehost.com tcp dpt:http state NEW LOG level warning tcp-options ip-options prefix "firewall-> HTTP: "
ACCEPT tcp -- anywhere somehost.com tcp dpt:http
LOG tcp -- anywhere somehost.com tcp dpt:https state NEW LOG level warning tcp-options ip-options prefix "firewall-> HTTPS: "
ACCEPT tcp -- anywhere somehost.com tcp dpt:https
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp spt:http
答案1
在 somehost.com 上執行一個名為 sshd 的 ssh 守護程式。您使用-R 2222:localhost:22 呼叫ssh 告訴somehost.com 上的sshd,它應該透過連接埠22 將發送到連接埠2222 的流量透過隧道傳輸到防火牆後面的筆記型電腦。 sshd 會傳送所有來自其他主機的流量透過連接埠 22 上的隧道傳送至連接埠 2222 到您的筆記型電腦。
如需進一步閱讀,我參考https://unix.stackexchange.com/questions/46235/how-does-reverse-ssh-tunneling-work。