内网机器通过 SSH 反向代理到公网机器之后,公网机器只监听了本地端口

2015-11-21 15:45:45 +08:00
 lichun

在内网机器上执行了

$ ssh -NR 9601:localhost:22 lichun@lichun.me

在公网机器的状态是

$ ss -ant | grep 9601

LISTEN 0 128 127.0.0.1:9601 :
LISTEN 0 128 ::1:9601 :::*

登陆到公网机器后,可以在公网机器上连接到内网机器,

$ ssh lichun@127.0.0.1 -p 9601

但是我想不登陆公网机器,直接连接到内网机器该怎么做?

$ ssh lichun@lichun.me -p 9601
ssh: connect to host lichun.me port 9601: Connection refused

因为 lichun.me 只监听了本地端口,没有暴露给公网, 这样没法连接

4001 次点击
所在节点    Linux
12 条回复
missdeer
2015-11-21 16:20:51 +08:00
再在公网机器上做一个本地转发
skydiver
2015-11-21 16:23:06 +08:00
$ ssh -NR 9601:localhost:22 lichun@lichun.me
因为你这句写了 localhost
把它改成公网 IP ,或者写 0.0.0.0
ylnbyttu
2015-11-21 16:38:41 +08:00
公网的主机 sshd_config 里面增加 GatewayPorts yes ,重启 sshd 再试应该就可以了
salmon5
2015-11-21 16:44:44 +08:00
sshd_config 里面默认 GatewayPorts yes 是注释的,注释掉重启 sshd 。

加上几个优化参数:
AllowAgentForwarding yes
AllowTcpForwarding yes
GatewayPorts yes
TCPKeepAlive yes
ClientAliveInterval 60
ClientAliveCountMax 1440
lichun
2015-11-21 16:49:02 +08:00
@missdeer 是的,我在公网机器上又做了个正向代理,可以访问了,>_<
lichun
2015-11-21 16:50:35 +08:00
@skydiver 改过的,不是这个的原因。
skydiver
2015-11-21 16:57:25 +08:00
@lichun 看错了。
skydiver
2015-11-21 17:01:46 +08:00
@lichun 按照 manpage 里所写,你第一条命令就已经绑定在所有的 interface 了,需要服务端开 GatewayPorts yes 就可以从外部访问了,同意楼上说的
lxf1992521
2015-11-21 17:14:23 +08:00
ssh -NR 9601:localhost:22 lichun@lichun.me

改成

ssh -NR :9601:localhost:22 lichun@lichun.me

这样就会监听在 0.0.0.0 上了,不然的话,只会监听在 127.0.0.1
lichun
2015-11-21 17:19:04 +08:00
@lxf1992521 这个真的没关系,
lichun
2015-11-21 17:21:16 +08:00
@skydiver @ylnbyttu @salmon5 的确是配置的原因,谢谢大家
jimzhong
2015-11-21 18:27:57 +08:00
原来也遇到过这个问题,一直没有搞定。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/237870

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX