ssh 隧道仅在登录代理机器时有效

ssh 隧道仅在登录代理机器时有效

Ubuntu 18,防火墙未启用,并且我已配置 sshd_config 以允许转发和网关:

AllowTcpForwarding yes
GatewayPorts yes 

如果我创建一个在所有端口上监听的隧道:

ssh -f -N -L 222:0.0.0.0:22 [email protected]

如果我登录到该机器并使用本地主机连接,这可以正常工作:

ssh root@localhost -p 222

但是当我从另一台机器连接到代理机器时连接失败:

ssh [email protected] -p 222
ssh: connect to host 192.168.0.20 port 222: Connection refused

我究竟做错了什么 ??

编辑

# lsof -n -i:222
COMMAND  PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
ssh     4937 root    4u  IPv6 5877871      0t0  TCP [::1]:222 (LISTEN)
ssh     4937 root    5u  IPv4 5877872      0t0  TCP 127.0.0.1:222 (LISTEN)

即使我终止原始进程并创建一个新进程并指定 IP 地址,lsof 仍然显示它仅在本地主机上监听!

ssh -f -N -L 222:192.168.0.20:22 [email protected]

答案1

默认情况下仅sshd将转发端口绑定到localhost。可以使用通配符或指定地址来覆盖此行为。


与通配符一起使用,:222::22相当于:222::22

user@client$ ssh -L :222::22 server

这将在所有将转发到的接口上client打开一个监听器。port 222server:22


如果要绑定监听器和转发地址,可以使用相应主机的IP地址或主机名。

user@client$ ssh -L client:222:localhost:22 server

这将在client:22解析为 的 IP 地址上打开一个监听器client,并将转发serverlocalhost:22


GatewayPorts=yes另一个选择是在命令中使用选项ssh,这也将打开通配符监听器client

user@client$ ssh -o GatewayPorts=yes -L 222::22 server

还可以将其添加到~/.ssh/config文件中,以使该设置对于特定主机永久有效。

Host server
    GatewayPorts yes

相关内容