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

SSH 端口转发配置问题

  •  
  •   uutoto · 2015-06-29 18:54:45 +08:00 · 2384 次点击
    这是一个创建于 3230 天前的主题,其中的信息可能已经有所发展或是发生改变。

    SSH端口转发,想把http://hostA:3555 映射到hostB的 http://127.0.0.1:8090

    hostA的用户名是:tt
    hostA上SSH服务器端口号为:44444

    hostB上执行:

    ssh -C -f -N -g -R 3555:127.0.0.1:8090 -p 44444 tt@hostA

    可就是没法通过http://hostA:3555访问到127.0.0.1:8090的一个网页啊。。
    网页代码是:

    import tornado.web
    import tornado.ioloop
    import time
    
    class MainHandle(tornado.web.RequestHandler):
        def get(self):
            print("hello!",time.asctime())
            self.write("hello!")
    
    if __name__ == "__main__":
        app = tornado.web.Application([(".*", MainHandle)])
        app.listen(8090)
        tornado.ioloop.IOLoop.instance().start()
    

    本地都可以访问的

    指点一下,万分感激!

    8 条回复    2015-07-01 21:38:03 +08:00
    nekoyaki
        1
    nekoyaki  
       2015-06-29 19:00:21 +08:00   ❤️ 1
    HostA的ssh配置文件里加一条: GatewayPorts yes
    uutoto
        2
    uutoto  
    OP
       2015-06-30 10:03:58 +08:00
    @nekoyaki O(∩_∩)O谢谢啦,昨天没有root权限,改不了。后来在本地装了虚拟机试了。
    yingluck
        3
    yingluck  
       2015-06-30 18:59:53 +08:00
    楼主 这个映射是不会暴露到公网上的

    你去 HostA 上面 curl 一下 127.0.0.1:3555 就知道了
    cattyhouse
        4
    cattyhouse  
       2015-07-01 20:58:41 +08:00 via iPhone
    Host A:

    ssh -N -q -R 8090:hostA:44444 user@hostB

    然后在hostB上:

    ssh [email protected] -p 44444 就能进入hostA,3555不需要是摆设。
    cattyhouse
        5
    cattyhouse  
       2015-07-01 21:00:52 +08:00 via iPhone
    修改:
    在host B上 :

    ssh [email protected] -p 8090
    cattyhouse
        6
    cattyhouse  
       2015-07-01 21:03:16 +08:00 via iPhone
    你到底要映射ssh还是映射端口?如果映射ssh的话,就像楼上的,
    映射端口的话,不需要ssh了,在hostB上运行iptables就可以了
    cattyhouse
        7
    cattyhouse  
       2015-07-01 21:27:38 +08:00
    我觉得ssh应该在hostA上执行吧?试试这个:

    在HostA上执行:
    ssh -NT -q -R 8090:ip_of_hostA:3555 username_of_hostB@ip_of_hostB
    然后在HostB上:
    telnet 127.0.0.1 8090
    cattyhouse
        8
    cattyhouse  
       2015-07-01 21:38:03 +08:00
    Sorry 忽略我以上的回答:


    -R
    .....
    By default, the listening socket on the server will be bound to the loopback interface only. This may be overridden by specifying a bind_address. An empty bind_address, or the address `*', indicates that the remote socketshould listen on all interfaces. Specifying a remote bind_address will only succeed if the server's GatewayPorts option is enabled (see sshd_config(5)).
    .....
    根据以上的man ssh的信息:所以楼主要做的就是:

    1,编辑HostA的 /etc/ssh/ssd_config 然后加入
    GatewayPorts yes

    2. 在HostB上执行:

    ssh -N -T -q -R *:3555:127.0.0.1:8090 -p 44444 tt@hostA
    或者
    ssh -N -T -q -R :3555:127.0.0.1:8090 -p 44444 tt@hostA
    或者
    ssh -N -T -q -R hostA_ip_address:3555:127.0.0.1:8090 -p 44444 tt@hostA

    注意要在3555前面加上 *: 或者只加 : 或者将 * 替换成 hostA的IP地址,因为不加这个的话,默认绑定到hostA的127.0.0.1上。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2254 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 08:56 · PVG 16:56 · LAX 01:56 · JFK 04:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.