尝试 SSH 时出现“权限被拒绝(公钥)”并显示“key_load_public:没有此文件或目录”

尝试 SSH 时出现“权限被拒绝(公钥)”并显示“key_load_public:没有此文件或目录”

运行时我不断收到以下错误。ssh -v -i privatekey [email protected]

OpenSSH_6.9p1, LibreSSL 2.1.8
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to server.org [XXX.XXX.XXX.XXX] port 22.
debug1: Connection established.
debug1: identity file privatekey type 1
debug1: key_load_public: No such file or directory
debug1: identity file privatekey-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.1
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to server.org:22 as 'user'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client [email protected] <implicit> none
debug1: kex: client->server [email protected] <implicit> none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:ig8ZTpASPM0W/drPBHPzcUIr+NtqzOKvVAvihnbaNuc
debug1: Host 'server.org' is known and matches the RSA host key.
debug1: Found key in /Users/XXX/.ssh/known_hosts:3
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: ysm
debug1: Authentications that can continue: publickey
debug1: Offering RSA public key: /Users/XXX/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).

我不明白哪里出了问题。我已经正确复制了我的公钥,并编辑了私钥文件的权限以确保其安全。

答案1

“权限被拒绝”错误意味着服务器拒绝您的连接。

  1. 检查 SSH 上的日志。
  2. 你是如何将公钥传输到服务器的?始终确保ssh-copy-id权限。用户主目录、.ssh 目录和 authorized_keys 文件在服务器端有严格的权限要求。(参见man sshd有关authorized_keys)。
  3. 查看服务器配置文件/etc/ssh/sshd_config也许服务器只允许特定的用户或组登录。

答案2

考虑到 Slipeer 的回答,以下是详细添加到 /etc/ssh/sshd_config 的内容。

在我的例子中,我尝试克隆一个 gitlab 项目,该解决方案应该适用于任何 git 服务。错误是

#15 0.589 debug1: identity file /root/.ssh/id_rsa type -1, 
#15 0.589 debug1: key_load_public: No such file or directory

使用后消失

echo "Host *\n  User git\n  HostName gitlab.com\n  AddKeysToAgent yes\n  IdentityFile /root/.ssh/id_rsa" >> /etc/ssh/ssh_config

这也是您在消息块中所要求的内容:

debug1: Reading configuration data /etc/ssh/ssh_config
...
debug1: key_load_public: No such file or directory

解决方案证明 ssh 需要检查服务器是否为已知主机。如果你没有指定所需的已知主机,则 gitlab 服务器不受信任,这就是 的意思the key_load_public cannot be found。是的,错误消息令人困惑。

echo当然,您也可以不使用命令来手动填写 ssh_config 。

相关内容