SSH 端口转发:动态转发、本地转发、远程转发

2023-11-23 12:03:13 来源/作者: 这里教程网整理 /

(目录)

动态转发

$ ssh -D local-port tunnel-host -N

参数说明

-D:动态转发 -N:不发送任何命令,只用来建立连接。没有这个参数,会在 SSH 服务器打开一个 Shell local-port 是本地端口 tunnel-host 是 SSH 服务器

举例

# 开启本地端口2121 动态转发 $ ssh -D 2121 root@127.0.0.1 -N # curl 的-x参数指定代理服务器 # 通过 SOCKS5 协议的本地2121端口,访问 https://httpbin.org/get $ curl -x socks5://localhost:2121 https://httpbin.org/get

本地转发

$ ssh -L -N local-port:target-host:target-port tunnel-host

参数说明

-L:转发本地端口

远程转发

$ ssh -R remote-port:target-host:target-port -N remotehost

参数说明

-R 表示远程端口转发

参考文章

SSH 端口转发:https://wangdoc.com/ssh/port-forwarding

https://www.man7.org/linux/man-pages/man1/ssh.1.html