Reverse SSH Tunnel

這一招很好用,

-R Reverse SSH tunnel
-N 不要執行遠端命令列
-R [bind_address:]port:host:hostport] : bind_address 遠端要聽的 IP, Port, 本地要連到的 IP Port

ssh -N -R 10.102.81.1:40000:localhost:40000 user@remote.host.domain

例如遠端機器的內網 IP 是 192.168.1.254 , 想在那個 IP 上開個 port 40000 連回來 ssh 服務,可以用以下的 command
加上 -N 是不希望建 tunnel 之後可以使用這個 tunnel 在遠端可以使用 command mode, 如果想要用 command mode 可以將 -N 拿掉

ssh -N -R 192.168.1.254:40000:localhost:22 user@remote.host.domain

不過有時候會有對方 SSH 沒有辦法 bind 在其他 IP 上, 這時候執行 netstat -na | grep 40000 會顯示

tcp        0      0 127.0.0.1:40000          0.0.0.0:*               LISTEN

這要打開遠端 ssh server 的 GatewayPorts, 編輯 /etc/ssh/sshd_config, 加上

GatewayPorts on

重新啟動 sshd 就 OK 了.

Related posts

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>