使用ssh_config进行ssh隧道的单行命令,使用本地私钥

时间:2017-04-27 08:36:03

标签: ssh ssh-tunnel

上下文

  • 工作代理服务是计算机。
  • 工作是一台多用户计算机。
  • 我拥有代理
  • 工作位于限制服务访问权限的防火墙后面,因为服务入口点端口(122)被防火墙阻止。
  • 我可以通过ssh 代理访问,然后访问服务
  • priv.key是连接服务的私钥,只能位于工作(不是代理 )。

               firewall
        +-------+  |  +-------+    +---------+
        |  Work |--|--| proxy |----| service |
        +-------+  |  +-------+    +---------+
           `-------------'  `----------' <- port forwarding/ssh tunneling
    

解决方案A:端口转发

我可以通过执行以下两个命令来访问服务

ssh server -L 2222:service:122 -N
ssh -i /home/work-user/.ssh/priv.key -p 2222 service-user@localhost

此解决方案不安全且不方便:任何访问工作的用户也可以访问service:122,并且有两个命令。

解决方案B: ssh tunnel

我尝试利用ssh_config并使用以下内容配置ssh隧道:

Host service
    HostName service
    User service-user
    Port 122
    IdentityFile /home/work-user/.ssh/priv.key
    ProxyCommand ssh proxy /usr/bin/nc %h %p 2>/dev/null
    # "proxy" in the previous line is a working ssh alias.

但是,ssh service命令不成功,以下是日志:

debug1: Reading configuration data /home/work-user/.ssh/config
debug3: kex names ok: [*authorized algorithms*]
debug1: /home/work-user/.ssh/config line 54: Applying options for test
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Hostname has changed; re-reading configuration
debug1: Reading configuration data /home/work-user/.ssh/config
debug3: kex names ok: [*authorized algorithms*]
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Executing proxy command: exec ssh proxy /usr/bin/nc service 122 2>/dev/null
debug1: permanently_drop_suid: 1000
debug1: identity file /home/work-user/.ssh/priv.key type 4
debug1: key_load_public: No such file or directory
debug1: identity file /home/work-user/.ssh/priv.key-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
ssh_exchange_identification: Connection closed by remote host

我还尝试在代理上复制priv.key,但只是为了测试,因为我不希望它是永久解决方案,但我也有同样的错误。

问题

是否有使用ssh_config的单行命令来利用ssh-tunneling同时将我的私钥保持在工作而不是代理

1 个答案:

答案 0 :(得分:0)

  

是否有一个使用ssh_config的单行命令来利用ssh-tunneling同时保持我的私钥工作而不是代理?

使用ProxyCommand,始终从本地计算机进行身份验证,而不是proxy进行身份验证。今天,您应该使用-W切换而不是netcat

ProxyCommand ssh -W %h:%p proxy

ProxyJump选项,它更简单:

ProxyJump proxy

您的用例确实将错误重定向到/dev/null,因此即使存在一些错误,您也不会看到错误。

尝试使用上述内容,如果没有帮助,请提供调试日志(使用LogLevel DEBUG3 两个步骤 - 代理和服务。

相关问题