V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
lichun
V2EX  ›  Linux

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

  •  
  •   lichun · 2015-11-21 15:45:45 +08:00 · 3964 次点击
    这是一个创建于 3090 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在内网机器上执行了

    $ ssh -NR 9601:localhost:22 [email protected]
    

    在公网机器的状态是

    $ ss -ant | grep 9601

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

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

    $ ssh [email protected] -p 9601

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

    $ ssh [email protected] -p 9601
    ssh: connect to host lichun.me port 9601: Connection refused

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

    第 1 条附言  ·  2015-11-21 16:53:47 +08:00

    问题解决了,从公网机器上再弄一个正向代理到本地端口

    ssh -NL *:1234:localhost:9601 localhost

    这样一来就得换另外一个端口去连接

    ssh [email protected] -p 1234

    第 2 条附言  ·  2015-11-21 17:18:05 +08:00

    刚试了一下,在 /etc/ssh/sshd_config 中修改 GatewayPorts yes 配置也是可以的

    $ ss -ant | grep 9601
    

    LISTEN 0 128 :9601 *:
    LISTEN 0 128 :::9601 :::*

    12 条回复    2015-11-21 18:27:57 +08:00
    missdeer
        1
    missdeer  
       2015-11-21 16:20:51 +08:00
    再在公网机器上做一个本地转发
    skydiver
        2
    skydiver  
       2015-11-21 16:23:06 +08:00 via iPad
    $ ssh -NR 9601:localhost:22 [email protected]
    因为你这句写了 localhost
    把它改成公网 IP ,或者写 0.0.0.0
    ylnbyttu
        3
    ylnbyttu  
       2015-11-21 16:38:41 +08:00 via Android
    公网的主机 sshd_config 里面增加 GatewayPorts yes ,重启 sshd 再试应该就可以了
    salmon5
        4
    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
        5
    lichun  
    OP
       2015-11-21 16:49:02 +08:00
    @missdeer 是的,我在公网机器上又做了个正向代理,可以访问了,>_<
    lichun
        6
    lichun  
    OP
       2015-11-21 16:50:35 +08:00
    @skydiver 改过的,不是这个的原因。
    skydiver
        7
    skydiver  
       2015-11-21 16:57:25 +08:00
    @lichun 看错了。
    skydiver
        8
    skydiver  
       2015-11-21 17:01:46 +08:00
    @lichun 按照 manpage 里所写,你第一条命令就已经绑定在所有的 interface 了,需要服务端开 GatewayPorts yes 就可以从外部访问了,同意楼上说的
    lxf1992521
        9
    lxf1992521  
       2015-11-21 17:14:23 +08:00
    ssh -NR 9601:localhost:22 [email protected]

    改成

    ssh -NR :9601:localhost:22 [email protected]

    这样就会监听在 0.0.0.0 上了,不然的话,只会监听在 127.0.0.1
    lichun
        10
    lichun  
    OP
       2015-11-21 17:19:04 +08:00
    @lxf1992521 这个真的没关系,
    lichun
        11
    lichun  
    OP
       2015-11-21 17:21:16 +08:00
    @skydiver @ylnbyttu @salmon5 的确是配置的原因,谢谢大家
    jimzhong
        12
    jimzhong  
       2015-11-21 18:27:57 +08:00
    原来也遇到过这个问题,一直没有搞定。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1529 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:16 · PVG 01:16 · LAX 10:16 · JFK 13:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.