如何使用正确的SSH私钥?

如何使用正确的SSH私钥?

我在 /home/myuser/.ssh/privateKey 里面有一个私钥

我在连接 ssh 服务器时遇到问题,因为我总是得到:

Permission denied (publickey).

我尝试调试该问题,发现 ssh 正在读取错误的文件,请查看输出:

[damiano@Damiano-PC .ssh]$ ssh -v root@vps1 
OpenSSH_5.8p2, OpenSSL 1.0.0g-fips 18 Jan 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for vps1
debug1: Applying options for *
debug1: Connecting to 111.111.111.111 [111.111.111.111] port 2000.
debug1: Connection established.
debug1: identity file /home/damiano/.ssh/id_rsa type -1
debug1: identity file /home/damiano/.ssh/id_rsa-cert type -1
debug1: identity file /home/damiano/.ssh/id_dsa type -1
debug1: identity file /home/damiano/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.8p1 Debian-7ubuntu1
debug1: match: OpenSSH_5.8p1 Debian-7ubuntu1 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.8
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: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA 74:8f:87:fe:b8:25:85:02:d4:b6:5e:03:08:d0:9f:4e
debug1: Host '[111.111.111.111]:2000' is known and matches the RSA host key.
debug1: Found key in /home/damiano/.ssh/known_hosts:1
debug1: ssh_rsa_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: Trying private key: /home/damiano/.ssh/id_rsa
debug1: Trying private key: /home/damiano/.ssh/id_dsa
debug1: No more authentication methods to try.

如您所见,ssh 正在尝试读取:/home/damiano/.ssh/id_rsa但我没有这个文件,我将其命名为不同。我如何告诉 SSH 使用正确的私钥文件?

谢谢!

答案1

您可以使用个人配置文件覆盖它并指向您的密钥文件;

vi ~/.ssh/config

然后您可以附加以下内容;

Host vps1
IdentityFile ~/.ssh/privateKey

提示:我通常必须在任何主机声明之前进行以下设置,它将首先尝试您的默认密钥,然后它将在文件夹 ~/.ssh/$hostname/$userid 中查找,最后在 ~/.ssh/$hostname 中查找;

IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/id_dsa
IdentityFile ~/.ssh/%h/%r/id_rsa
IdentityFile ~/.ssh/%h/%r/id_dsa
IdentityFile ~/.ssh/%h/id_rsa
IdentityFile ~/.ssh/%h/id_dsa

答案2

您需要使用:

ssh -i <Key file>

答案3

即使在设置了 Mark 建议的选项之后,我仍然必须进行设置IdentitiesOnly yes以确保仅使用指定的键。

相关内容