SSH 反向端口转发绑定到地址

Cra*_*ige 2 ssh ssh-tunnel

所以,我有一个脚本设置来自动打开到我的服务器的 SSH 连接,并将端口反向转发给我,以便 SSH 连接到我的家庭桌面。这个脚本一直有效,直到我迁移了服务器。

简而言之,我无法将此连接绑定到外部 IP 地址,我不确定为什么。

该脚本基本上运行这个命令(我可以确认这个带有我的值的命令在从 shell 运行时具有相同的结果):

sudo autossh -M 5114 -D 7474 -R servers_public_ip:2695:localhost:22 -i /home/user/.ssh/id_rsa2 remote@servers_public_ip

当我检查时lsof,我看到为remote用户绑定了以下连接:

sshd      27570   remote    3u  IPv4 161761      0t0  TCP servers_public_ip:22->67-213-103-227.eastlink.ca:62812 (ESTABLISHED)
sshd      27570   remote    7u  IPv6 161773      0t0  TCP ip6-localhost:5114 (LISTEN)
sshd      27570   remote    8u  IPv4 161774      0t0  TCP localhost:5114 (LISTEN)
sshd      27570   remote    9u  IPv6 161777      0t0  TCP ip6-localhost:2695 (LISTEN)
sshd      27570   remote   10u  IPv4 161778      0t0  TCP localhost:2695 (LISTEN)
Run Code Online (Sandbox Code Playgroud)

这些绑定都不在服务器的公共 ip 上,除了我与服务器的 ssh 连接以运行 lsof。这在我的旧服务器上运行良好。我已经检查了 sshd_config 并AllowTcpForwarding显式打开。我的想法不多了。

Cra*_*ige 9

我讨厌这种情况发生,但经过更多的研究,我能够回答我自己的问题。

似乎sshd_config需要GatewayPorts yes绑定到外部地址。在移植配置时,我一定错过了这一点。


小智 6

为了使其绑定到所有接口,请使用:

ssh -N -R 0.0.0.0:2695:localhost:22  root@example.com
Run Code Online (Sandbox Code Playgroud)

不要忘记编辑/etc/ssh/sshd_config并启用GatewayPorts并将其设置为yes

  • 你几乎不想将其设置为“启用”。如果您将 GatewayPorts 设置为 yes,这将使 sshd 将您的转发绑定到任何接口 - 无论客户端配置如何(-R 等)。如果客户端认为她已将转发限制为本地主机,这可能会成为一个相当大的安全问题。因此,将 GatewayPorts 设置为 clientspecified 通常就是您想要的。 (2认同)