使用两个独立公钥的两跳 SSH 连接

使用两个独立公钥的两跳 SSH 连接

我们有以下 ssh 跳转设置:

    localhost -> hub -> server

hubuser@hub 接受 localuser@localhost 的公钥。

serveruser@server 接受 hubuser@hub 的公钥。

因此我们正在发出ssh -t hubuser@hub ssh serveruser@server连接服务器的请求。

这个设置的问题在于我们无法直接 scp 到服务器。

我尝试创建如下 .ssh/config 文件:

    Host server
      user serveruser
      port 22
      hostname server
      ProxyCommand ssh -q hubuser@hub 'nc %h %p'

但我无法连接(yigit 是本地用户):

    $ ssh serveruser@server -v
    OpenSSH_6.1p1, OpenSSL 1.0.1c 10 May 2012
    debug1: Reading configuration data /home/yigit/.ssh/config
    debug1: /home/yigit/.ssh/config line 19: Applying options for server        debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: Executing proxy command: exec ssh -q hubuser@hub 'nc server 22'
    debug1: permanently_drop_suid: 1000
    debug1: identity file /home/yigit/.ssh/id_rsa type 1000        debug1: identity file /home/yigit/.ssh/id_rsa-cert type -1
    debug1: identity file /home/yigit/.ssh/id_dsa type -1
    debug1: identity file /home/yigit/.ssh/id_dsa-cert type -1
    debug1: identity file /home/yigit/.ssh/id_ecdsa type -1
    debug1: identity file /home/yigit/.ssh/id_ecdsa-cert type -1
    debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1
    debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1 pat OpenSSH_5*
    debug1: Enabling compatibility mode for protocol 2.0        debug1: Local version string SSH-2.0-OpenSSH_6.1
    debug1: SSH2_MSG_KEXINIT sent
    debug1: SSH2_MSG_KEXINIT received
    debug1: kex: server->client aes128-ctr hmac-md5 none
    debug1: kex: client->server aes128-ctr hmac-md5 none        debug1: sending SSH2_MSG_KEX_ECDH_INIT
    debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
    debug1: Server host key: ECDSA cb:ee:1f:78:82:1e:b4:39:c6:67:6f:4d:b4:01:f2:9f
    debug1: Host 'server' is known and matches the ECDSA host key.
    debug1: Found key in /home/yigit/.ssh/known_hosts:33
    debug1: ssh_ecdsa_verify: signature correct
    debug1: SSH2_MSG_NEWKEYS sent
    debug1: expecting SSH2_MSG_NEWKEYS
    debug1: SSH2_MSG_NEWKEYS received
    debug1: Roaming not allowed by server
    debug1: SSH2_MSG_SERVICE_REQUEST sent
    debug1: SSH2_MSG_SERVICE_ACCEPT received
    debug1: Authentications that can continue: publickey
    debug1: Next authentication method: publickey
    debug1: Offering RSA public key: /home/yigit/.ssh/id_rsa
    debug1: Authentications that can continue: publickey
    debug1: Trying private key: /home/yigit/.ssh/id_dsa
    debug1: Trying private key: /home/yigit/.ssh/id_ecdsa
    debug1: No more authentication methods to try.
    Permission denied (publickey).

请注意,它尝试使用公钥 localuser@localhost 在服务器上进行身份验证,但由于密钥不正确而失败。是否可以修改 ProxyCommand,以便使用 hubuser@hub 的密钥在服务器上进行身份验证?

答案1

您可以使用-i来指定用于从到 的ProxyCommand连接使用的密钥文件。您可以使用 来指定用于从到 的连接使用的密钥文件localhosthubIdentityFilelocalhostserver

两个密钥文件都需要位于localhost。此设置不需要将两个密钥文件中的任何一个位于hub

答案2

确保你已netcat在本地计算机(可能还有所有其他服务器)上安装了软件包,例如在 Ubuntu 上你可以使用 安装sudo apt-get install netcat

确保 hop-server 可以无需密码通过 SSH 连接到目标服务器(例如:使用ssh-copy-id

在本地机器上的 ~/.ssh/config 中添加:

Host any-nickname-here
    User destination-user
    ProxyCommand ssh -o 'ForwardAgent=yes' user@hop-server 'ssh-add && nc destination-host destination-port'

然后下面的应该工作ssh any-nickname-herescp file.txt any-nickname-here:/dir/path/

相关内容