git 私有服务器错误:“权限被拒绝(公钥)。”

git 私有服务器错误:“权限被拒绝(公钥)。”

我按照说明这里以便在我的 Amazon EC2 实例上设置私有 git 服务器。但是,我在尝试通过 SSH 进入 git 帐户时遇到了问题。具体来说,我收到错误“权限被拒绝(公钥)”。

以下是 EC2 服务器上我的文件/文件夹的权限:

drwx------ 4 git git 4096 Aug 13 19:52 /home/git/
drwx------ 2 git git 4096 Aug 13 19:52 /home/git/.ssh
-rw------- 1 git git  400 Aug 13 19:51 /home/git/.ssh/authorized_keys

以下是我自己的计算机上的文件/文件夹的权限:

drwx------  5 CYT  staff   170 Aug 13 14:51 .ssh
-rw-------  1 CYT  staff  1679 Aug 13 13:53 .ssh/id_rsa
-rw-r--r--  1 CYT  staff   400 Aug 13 13:53 .ssh/id_rsa.pub
-rw-r--r--  1 CYT  staff  1585 Aug 13 13:53 .ssh/known_hosts

当我检查我的日志时/var/log/secure,每次我尝试 SSH 时都会收到以下错误消息:

Authentication refused: bad ownership or modes for file /home/git/.ssh/authorized_keys

但是,在进行了一些权限更改后,我不再收到此错误消息。尽管如此,每次尝试 SSH 时,我仍然会收到“权限被拒绝(公钥)”消息。

我用于 SSH 的命令是ssh -T git@my-ip

这是我运行时获得的完整日志:ssh -vT [email protected]

OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: Connecting to my-ip [my-ip] port 22.
debug1: Connection established.
debug1: identity file /Users/CYT/.ssh/id_rsa type -1
debug1: identity file /Users/CYT/.ssh/id_rsa-cert type -1
debug1: identity file /Users/CYT/.ssh/id_dsa type -1
debug1: identity file /Users/CYT/.ssh/id_dsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.2
debug1: match: OpenSSH_6.2 pat OpenSSH*
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr [email protected] none
debug1: kex: client->server aes128-ctr [email protected] 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 08:ad:8a:bc:ab:4d:5f:73:24:b2:78:69:46:1a:a5:5a
debug1: Host 'my-ip' is known and matches the RSA host key.
debug1: Found key in /Users/CYT/.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: /Users/CYT/.ssh/id_rsa
debug1: Trying private key: /Users/CYT/.ssh/id_dsa
debug1: No more authentication methods to try.
Permission denied (publickey).

我花了几个小时浏览各种网站(包括 SO 和 SF)上的帖子,寻找解决方案。看来我的文件的权限都没问题,但我就是搞不清楚问题所在。任何帮助都将不胜感激。

编辑:

EEAA:以下是您请求的输出:

$ getent passwd git
git:x:503:504::/home/git:/bin/bash

$ grep ssh ~git/.ssh/authorized_keys | wc -l
grep: /home/git/.ssh/authorized_keys: Permission denied
0

答案1

我应该更仔细地检查日志。我的 SSH 密钥的位置指定不正确。我曾经SSH -i指定了正确的目录,并且能够成功进行 SSH。

答案2

此错误

Authentication refused: bad ownership or modes for file /home/git/.ssh/authorized_keys

通常意味着路径链上的权限是这样的,不仅 root 和服务器端用户(“git”)可以更改 /home/git/.ssh/authorized_keys,而且其他人也可以。

每当我看到这个错误时,结果都是由于 authorized_keys 文件本身或其父目录之一具有组可写或全局可写权限。您列出了 /home/git/.ssh 和 /home/git 的权限,但没有列出 /home 和 / 的权限!

您可以通过在服务器端设置“StrictModes=no”来放宽此要求,请参阅sshd_配置(5)

答案3

这是有问题的行:

-rw-r--r--  1 CYT  staff   400 Aug 13 13:53 .ssh/id_rsa.pub

您的 id_rsa.pub 是公开可读的,但事实并非如此。只要chmod 600 .ssh/id_rsa.pub它能工作就行。

相关内容