OpenSSH
创建一个反向 ssh 隧道只需一个开关 -R 到原始命令。
命令行
假设你使用命令 ssh guest@example.com 作为用户 guest 连接到 example.com。打开反向隧道可能如下所示:
ssh -R 2222:localhost:22 guest@example.com
它将在远程服务器上打开一个端口 2222(仅限环回接口),并且该端口的每个连接都将转发到本地计算机 ssh 服务器(端口 22)。
这也假设你已在服务器上的 sshd_config 中允许选项 AllowTcpForwarding yes 和 PermitOpen any。否则它将失败并出错
open failed: administratively prohibited: open failed
如果要允许转发端口可以在其他网络地址(而不是 localhost)上访问,则还需要允许 GatewayPorts yes 并使用 IP 地址或主机名或 IP):
ssh -R 2222:example.com:22 guest@example.com
组态
此外,你可以在~/.ssh/config 中指定远程端口转发,以避免每次连接时键入相同的行。如果经常连接到主机并且不希望每次都启动端口转发,那么良好的做法可能是为主机设置别名,该主机将进行此转发:
Host example.com-R
Hostname example.com
User guest
RemoteForward 2222 localhost:22
然后使用 ssh example.com-R 创建远程端口转发
在后台运行
端口转发可以简单地在后台使用交换机 -N 运行(不要运行远程命令,只运行转发),-f(验证后转到后台),-T(禁用远程 TTY 分配)。把它们放在一起:
ssh -NTfR 2222:localhost:22 guest@example.com