Git 在 Git 实验室中使用 SSH 克隆不起作用

Git clone with SSH in GitLab doesn't work

我已经在我的 Git 实验室配置文件设置中设置了我的 public SSH RSA 密钥。在我的远程服务器上,我已经添加了我的 SSH 私钥:

ssh-add ~/.ssh/id_rsa

事实上,当我 运行 在我的远程服务器上进行 SSH 连接验证时,它已通过身份验证:

user@some-remote-server:/var/www$ ssh -T git@gitlab.com
The authenticity of host 'gitlab.com (xxx.xx.xxx.xx)' can't be established.
ECDSA key fingerprint is SHA256:anmsdaskdnaslkdmaskd.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitlab.com,xxx.xx.xxx.xx' (ECDSA) to the list of known hosts.
Enter passphrase for key '/home/user/.ssh/id_rsa':
Welcome to GitLab, @alramdein!
user@some-remote-server:/var/www$

我是存储库的所有者。但是当我 运行 Git 克隆它说 fatal: Could not read from remote repository.

user@some-remote-server:/var/www$ sudo git clone git@gitlab.com:repo/repo.git
Cloning into 'repo'...
git@gitlab.com: Permission denied (publickey,keyboard-interactive).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
user@some-remote-server:/var/www$

您正在使用sudo git clone;这意味着您没有使用与您的用户相同的 ssh-agent

如果你想检查,你可以执行

ssh-add -L

sudo ssh-add -L

而且您会看到两个帐户中没有相同的代理人。

我不知道你为什么要用 sudo 权限做 git clone,但是你需要用 sudo 启动你的代理(对于 root 用户)并将私钥附加到它。

此外,由于 sudo 禁用了环境变量,因此每次使用 ssh.[=22 时都必须提供 SSH_AUTH_SOCKSSH_AGENT_PID =]