Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
lichun
V2EX  ›  Linux

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

  •  
  •   lichun · Nov 21, 2015 · 4796 views
    This topic created in 3873 days ago, the information mentioned may be changed or developed.

    在内网机器上执行了

    $ 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 只监听了本地端口,没有暴露给公网, 这样没法连接

    Supplement 1  ·  Nov 21, 2015

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

    ssh -NL *:1234:localhost:9601 localhost

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

    ssh [email protected] -p 1234

    Supplement 2  ·  Nov 21, 2015

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

    $ ss -ant | grep 9601
    

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

    12 replies    2015-11-21 18:27:57 +08:00
    missdeer
        1
    missdeer  
       Nov 21, 2015
    再在公网机器上做一个本地转发
    skydiver
        2
    skydiver  
       Nov 21, 2015 via iPad
    $ ssh -NR 9601:localhost:22 [email protected]
    因为你这句写了 localhost
    把它改成公网 IP ,或者写 0.0.0.0
    ylnbyttu
        3
    ylnbyttu  
       Nov 21, 2015 via Android
    公网的主机 sshd_config 里面增加 GatewayPorts yes ,重启 sshd 再试应该就可以了
    salmon5
        4
    salmon5  
       Nov 21, 2015
    sshd_config 里面默认 GatewayPorts yes 是注释的,注释掉重启 sshd 。

    加上几个优化参数:
    AllowAgentForwarding yes
    AllowTcpForwarding yes
    GatewayPorts yes
    TCPKeepAlive yes
    ClientAliveInterval 60
    ClientAliveCountMax 1440
    lichun
        5
    lichun  
    OP
       Nov 21, 2015
    @missdeer 是的,我在公网机器上又做了个正向代理,可以访问了,>_<
    lichun
        6
    lichun  
    OP
       Nov 21, 2015
    @skydiver 改过的,不是这个的原因。
    skydiver
        7
    skydiver  
       Nov 21, 2015
    @lichun 看错了。
    skydiver
        8
    skydiver  
       Nov 21, 2015
    @lichun 按照 manpage 里所写,你第一条命令就已经绑定在所有的 interface 了,需要服务端开 GatewayPorts yes 就可以从外部访问了,同意楼上说的
    lxf1992521
        9
    lxf1992521  
       Nov 21, 2015
    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
       Nov 21, 2015
    @lxf1992521 这个真的没关系,
    lichun
        11
    lichun  
    OP
       Nov 21, 2015
    @skydiver @ylnbyttu @salmon5 的确是配置的原因,谢谢大家
    jimzhong
        12
    jimzhong  
       Nov 21, 2015
    原来也遇到过这个问题,一直没有搞定。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1131 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 23:22 · PVG 07:22 · LAX 16:22 · JFK 19:22
    ♥ Do have faith in what you're doing.