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

有办法用 SSH 给跳板机后的机器做端口映射吗?

  •  
  •   xi_lin · 2017-09-08 17:32:22 +08:00 · 7772 次点击
    这是一个创建于 2420 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有台测试的机器通过跳板机可以进行 ssh 登录

    有办法在本地给远程这台测试机做一个端口映射吗?

    第 1 条附言  ·  2017-09-08 19:03:46 +08:00
    跳板机用的是这个 http://www.jumpserver.org/
    第 2 条附言  ·  2017-09-08 21:31:58 +08:00
    找到一个 issue https://github.com/jumpserver/jumpserver/issues/130
    里面有不少相关链接讨论了这个话题
    第 3 条附言  ·  2017-09-08 23:14:51 +08:00

    https://serverfault.com/a/701884/280534 这个看起来是我想要的,但是不知道为什么我配置完并不能连接上,加了-vvv的输出如下

    OpenSSH_7.4p1, LibreSSL 2.5.0
    debug1: Reading configuration data /Users/xxxxx/.ssh/config
    debug1: /Users/xxxxx/.ssh/config line 13: Applying options for C
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: Executing proxy command: exec ssh -o 'ForwardAgent yes' B 'ssh-add && nc xxx.xxx.xxx.xxx xxxxx'
    debug1: identity file /Users/xxxxx/.ssh/id_rsa type 1
    debug1: key_load_public: No such file or directory
    debug1: identity file /Users/xxxxx/.ssh/id_rsa-cert type -1
    debug1: key_load_public: No such file or directory
    debug1: identity file /Users/xxxxx/.ssh/id_dsa type -1
    debug1: permanently_drop_suid: 501
    debug1: key_load_public: No such file or directory
    debug1: identity file /Users/xxxxx/.ssh/id_dsa-cert type -1
    debug1: key_load_public: No such file or directory
    debug1: identity file /Users/xxxxx/.ssh/id_ecdsa type -1
    debug1: key_load_public: No such file or directory
    debug1: identity file /Users/xxxxx/.ssh/id_ecdsa-cert type -1
    debug1: key_load_public: No such file or directory
    debug1: identity file /Users/xxxxx/.ssh/id_ed25519 type -1
    debug1: key_load_public: No such file or directory
    debug1: identity file /Users/xxxxx/.ssh/id_ed25519-cert type -1
    debug1: Enabling compatibility mode for protocol 2.0
    debug1: Local version string SSH-2.0-OpenSSH_7.4
    
    35 条回复    2020-05-15 22:19:12 +08:00
    terry0314
        1
    terry0314  
       2017-09-08 17:41:57 +08:00 via Android
    xi_lin
        2
    xi_lin  
    OP
       2017-09-08 18:00:22 +08:00
    @terry0314 对,类似这样,但是在跳板机后面没法直连目标机器
    liyvhg
        3
    liyvhg  
       2017-09-08 18:05:12 +08:00 via Android
    映射两次吧,目标机 22 端口→跳板机 1022 端口→本机 2022 端口
    ysicing
        4
    ysicing  
       2017-09-08 18:33:54 +08:00   ❤️ 1
    proxifier 这个软件可能有用
    xi_lin
        5
    xi_lin  
    OP
       2017-09-08 19:03:52 +08:00
    @liyvhg 跳板机我没有操作权限。。
    xi_lin
        6
    xi_lin  
    OP
       2017-09-08 19:05:16 +08:00
    @ysicing 应该不行,我没法操作跳板机
    nanpuyue
        7
    nanpuyue  
       2017-09-08 19:09:32 +08:00
    Keyes
        8
    Keyes  
       2017-09-08 19:39:17 +08:00 via iPhone
    欢迎使用商业版

    我们的产品也是类似,但是支持 ssh 端口转发,也支持 proxycommand
    wangmingbuhaoqi
        9
    wangmingbuhaoqi  
       2017-09-08 19:50:23 +08:00
    有 2 种方式 iptables nat 可行不?

    ssh 的端口映射,尝试下,不过有条件,需要有个外网机
    http://www.ruanyifeng.com/blog/2011/12/ssh_port_forwarding.html
    xi_lin
        10
    xi_lin  
    OP
       2017-09-08 21:17:02 +08:00
    @nanpuyue
    @wangmingbuhaoqi
    前面说了我没有跳板机的操作权限。。。
    跳板机是有一层屏蔽的,不是单纯的中间代理,连上后有菜单要选择下一步跳转的机器
    没法直接用原生 ssh 做转发的
    xi_lin
        11
    xi_lin  
    OP
       2017-09-08 21:18:57 +08:00
    @Keyes 谢谢指导,我研究下 proxycommand
    joyqi
        12
    joyqi  
       2017-09-08 21:53:06 +08:00
    xi_lin
        13
    xi_lin  
    OP
       2017-09-08 23:00:29 +08:00
    @Keyes
    @terry0314
    @liyvhg
    @nanpuyue
    @wangmingbuhaoqi
    我想错了,ssh 转发应该行。只是我这里跳板机上自配了一层认证信息,我好像没法传递这个认证

    我的机器 A,跳板机 B,目标机 C
    A 连接 B,靠认证信息 x
    B 连接 C,靠认证信息 y
    我试着直接靠 B 做代理转发,会在 B 的输出上看到提示
    channel 3: open failed: administratively prohibited: open failed
    liyvhg
        14
    liyvhg  
       2017-09-08 23:03:06 +08:00
    @xi_lin #13 监听高位端口(端口号>1024)就不需要特权, 例如 11022
    xi_lin
        15
    xi_lin  
    OP
       2017-09-08 23:11:54 +08:00
    @liyvhg 好像还没到监听端口这一步,我权限验证还没传递过去,连不上目标机 C
    johnlui
        16
    johnlui  
       2017-09-08 23:20:33 +08:00
    感谢楼主提供的好工具。

    可以试试 Nginx TCP 反向代理,相当好用呦
    phenix77
        17
    phenix77  
       2017-09-09 00:38:28 +08:00
    使用 ssh 开隧道不行么,A 机器端口:111 (随意设),目标机 C 端口:222,跳板机 B
    ssh -L 111:C:222 B
    在机器 A 访问 localhost:111 即可
    ryd994
        18
    ryd994  
       2017-09-09 03:05:18 +08:00
    后面的机器联网么?联网的话用 ngrok 之类的直接打出来,虽然明显很不安全
    cxbig
        19
    cxbig  
       2017-09-09 03:17:45 +08:00
    @xi_lin
    B 和 C 的授权都是什么类型?用不同 ssh 私钥都不是问题,本地做一个 ForwardAgent yes,在~/.ssh/config 配置好即可

    Host *
    ForwardAgent yes

    Host B
    HostName x.x.x.x
    IdentityFile ~/.ssh/key-a
    User aaa
    ....

    Host C
    HostName y.y.y.y
    IdentityFile ~/.ssh/key-b
    User bbb
    ProxyCommand ssh -q -W %h:%p B
    ...

    只要 B 到 C 的登录信息是固定的,就可以这样配置

    然后应该是可以直接在本地登录 C:
    ssh C
    能登录就可以做通道:( 9000 为例)
    ssh -f -N -L 9000:127.0.0.1:9000 C
    nadoo
        20
    nadoo  
       2017-09-09 11:42:59 +08:00
    tmate 这个软件是否可以?共享 ssh
    wizardoz
        21
    wizardoz  
       2017-09-09 14:03:13 +08:00
    不知道这个跳板机是什么玩法,我一般是两种方式
    1:
    ssh -L 10022:host_c:22 user@host_b
    然后:
    ssh -p 10022 user@localhost

    2:
    打开 ForwardAgent
    然后 ssh user@host_b 'ssh user@host_c'
    xi_lin
        22
    xi_lin  
    OP
       2017-09-09 21:57:43 +08:00
    @johnlui nginx 反代应该是别的话题?
    xi_lin
        23
    xi_lin  
    OP
       2017-09-09 21:59:32 +08:00
    @cxbig 感谢回复,C 的认证信息是存在 B 上的,我这 A 上没有认证信息
    xi_lin
        24
    xi_lin  
    OP
       2017-09-09 22:30:10 +08:00
    @wizardoz
    方法 1 不行,见我 append 的信息

    方法 2 试了下,-vvv 里看到到 hostB 的认证是没问题的,但是再执行下一个 command 的时候过不去

    debug3: send packet: type 50
    debug3: receive packet: type 52
    debug1: Authentication succeeded (publickey).
    Authenticated to xxx.xx.xx.xx ([xxx.xx.xx.xx]:22). #hostB
    debug1: channel 0: new [client-session]
    debug3: ssh_session2_open: channel_new: 0
    debug2: channel 0: send open
    debug3: send packet: type 90
    debug1: Requesting [email protected]
    debug3: send packet: type 80
    debug1: Entering interactive session.
    debug1: pledge: network
    debug3: receive packet: type 91
    debug2: callback start
    debug1: Requesting authentication agent forwarding.
    debug2: channel 0: request [email protected] confirm 0
    debug3: send packet: type 98
    debug2: fd 3 setting TCP_NODELAY
    debug3: ssh_packet_set_tos: set IP_TOS 0x08
    debug2: client_session2_setup: id 0
    debug1: Sending command: ssh hostC
    debug2: channel 0: request exec confirm 1
    debug3: send packet: type 98
    debug2: callback done
    debug2: channel 0: open confirm rwindow 0 rmax 32768
    debug2: channel 0: rcvd adjust 2097152
    debug3: receive packet: type 99
    debug2: channel_input_status_confirm: type 99 id 0
    debug2: exec request accepted on channel 0
    xi_lin
        25
    xi_lin  
    OP
       2017-09-09 22:32:11 +08:00
    @nadoo 这个看起来有点像 ngrok 一样在 C 上开第三方服务做中转了
    xi_lin
        26
    xi_lin  
    OP
       2017-09-09 22:32:44 +08:00
    @ryd994 嗯,是联网的。能不走第三方最好了。。。
    pynix
        27
    pynix  
       2017-09-09 23:49:22 +08:00
    @wizardoz 需要-t
    pynix
        28
    pynix  
       2017-09-09 23:50:03 +08:00
    @xi_lin ngrok 被扫描很烦。。。。
    cxbig
        29
    cxbig  
       2017-09-10 02:45:03 +08:00
    @xi_lin 什么样的登录方式?说的不是很清楚。如果是 ssh-key 的话:
    1. 把 B 上的私钥下载到 A 本地来,按我上面写的,在本地配置 B 到 C 的登录
    2. 用间接登录的方式把 A 本地自己的公钥加到 C 的$HOME/.ssh/authorized_keys 里,在本地用同一个私钥来配置 B 到 C
    xi_lin
        30
    xi_lin  
    OP
       2017-09-10 11:25:16 +08:00
    @cxbig B 和 C 都是运维配置的,我也不是很清楚具体的登录方式,目前从 A 登上 B 之后会有一个选择菜单出现,选完后连上 C,猜测是 B 到 C 上有配置好相应的 KEY。B/C 我都没有权限干你提到的 1 和 2 这两件事。。。
    xi_lin
        31
    xi_lin  
    OP
       2017-09-10 11:25:44 +08:00
    @pynix 被扫到会怎么样?我没用过 ngrok
    Harpsichord
        32
    Harpsichord  
       2019-11-08 22:42:06 +08:00
    @xi_lin 请问找到解决方案了吗。。我也遇到这个问题了
    xi_lin
        33
    xi_lin  
    OP
       2019-11-11 18:55:32 +08:00
    @Harpsichord 木有。。
    zinfinb
        34
    zinfinb  
       2020-05-14 20:36:34 +08:00
    @xi_lin 楼主找到解决方案了吗 我也遇到这个问题了
    xi_lin
        35
    xi_lin  
    OP
       2020-05-15 22:19:12 +08:00
    @zinfinb 放弃了。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1710 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:46 · PVG 00:46 · LAX 09:46 · JFK 12:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.