git pull:权限被拒绝(公钥)

git pull:权限被拒绝(公钥)

因此,我尝试让 git 与 AWS 上的新服务器(4.5.6.7 / 10.0.0.111)上的 repo 服务器(1.2.3.4)(debian)协同工作。

我的仓库的 .git/config

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[branch "master"]
    remote = origin
    merge = refs/heads/master
[remote "origin"]
    url = [email protected]:/opt/git/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

我收到此错误:

[ec2-user@ip-10-0-0-111 html]$ sudo git pull
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我的公钥位于 /home/git/.ssh/authorized_keys

之前,系统提示我输入 repo 服务器上 git 用户的密码(而不是我的密钥文件密码)。然后我禁用了 git 用户的密码验证,结果却收到权限被拒绝的提示。在此期间,现在出现了上述错误我能够通过 ssh 成功登录

ssh [email protected]

没有密码提示或类似情况:

debug1: Offering RSA public key: /home/ec2-user/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Authentication succeeded (publickey).

在 repo 服务器(1.2.3.4)上成功进行 ssh 登录后,/var/log/auth.log 中的内容如下所示:

Feb 22 15:45:44 hostname sshd[20142]: Connection from 4.5.6.7 port 50409
Feb 22 15:45:44 hostname sshd[20142]: Found matching RSA key: <fingerprint>
Feb 22 15:45:44 hostname sshd[20142]: Accepted publickey for git from 4.5.6.7 port 50409 ssh2
Feb 22 15:45:44 hostname sshd[20142]: pam_unix(sshd:session): session opened for user git by (uid=0)

当我尝试 git pull 时,auth.log 如下所示:

Feb 22 15:46:41 hostname sshd[20177]: Connection from 4.5.6.7 port 50410

之后就没再发生什么了。

当正常的 ssh 命令可以完美运行的时候,如何调试 git 的 git ssh 身份验证失败?

答案1

git pull在 下执行sudo,这就是问题所在。只执行

git pull

会对你有用。同时做

sudo ssh [email protected]

会失败。问题是sudo更改用户,它不再看到您的身份文件/home/ec2-user/.ssh/id_rsa(它在 中搜索/root/.ssh/id_rsa)。使用pull您的普通用户执行 或将您的密钥复制/移动到适合目标用户的位置(root)。

答案2

您的 ssh 密钥尚未取消验证。

ssh-keygen -t rsa -C "[email protected]"将为当前用户生成 ssh 密钥。例如:

$ whoami
nodejh

# It will generate the ssh key for user: nodejh
$ ssh-keygen -t rsa -C "[email protected]

# Check that you are connecting to the correct server 
$ ssh -T [email protected]
Hi username! You've successfully authenticated...

sudo ssh-keygen -t rsa -C "[email protected]将为 生成 ssh 密钥root。因此 将返回,但 可以正常工作。ssh -T [email protected]Permission Denied (publickey)sudo ssh -T [email protected]

如果你想为用户生成 ssh 密钥:admin,你可以更改当前用户然后admin生成 ssh 密钥。

# change the current user to admin
$ su admin
# generate ssh key for `admin`
$ ssh-keygen -t rsa -C "[email protected]`

相关内容