不能克隆,可以 SSH。 “权限被拒绝(公钥)。”

时间:2021-02-20 02:28:41

标签: windows git ssh git-credential-manager

我无法克隆或推送到我服务器上的存储库。

我有一个裸仓库,它位于目录 user@host 中的目录 home/user/test.git,我试图通过 git clone 访问该目录。我使用 ssh-add <pathtokey> 添加我的 ssh 密钥。它问我要密码。然后我可以ssh user@host成功。

但是,如果我尝试 git clone ssh://user@host/~/test.git 我得到:

Cloning into 'test'...
user@host: Permission denied (publickey).
fatal: Could not read from remote repository.

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

也试过了

  • git clone ssh://user@host/home/user/test.git
  • git clone user@host:home/user/test.git
  • git clone user@host:/home/user/test.git

结果相同

我猜 git 凭证管理器没有拿到钥匙?

在服务器上 /var/auth/logFeb 20 02:25:36 xxxxx sshd[24674]: Connection closed by authenticating user XXXX x.x.x.x port 56433 [preauth]

  • Git 版本:git version 2.30.1.windows.1
  • Git 凭证管理器:Git Credential Manager version 2.0.318-beta+44acfafa98 (Windows, .NET Framework 4.0.30319.42000)
  • git config -l 报告 credential.helper=manager-core
  • 尝试了 PowerShellgit bash shell,结果相同
  • user 拥有对 repo 的读取、执行权限

2 个答案:

答案 0 :(得分:1)

git 凭据管理器仅用于缓存 HTTPS URL 的凭据(用户名/密码),而不涉及 SSH。
如果私钥是用它定义的,则只有 ssh-agent 可以参与,用于缓存可能的密码。

我会首先尝试使用完整路径,因为 ~ 可能不会被远程 shell 解释,而是本地的(~ 的路径不同):

git clone ssh://user@host/home/user/test.git
# or
git clone user@host:/home/user/test.git

如果没有,在 git bash 会话中,输入:

export GIT_SSH_COMMAND='ssh -v'
git clone ...

OP 确认 in the discussion 它在 bash 会话中有效:

<块引用>

在 git bash 中,我启动了 ssh-agent,
在那里添加了密钥,然后它就起作用了。

答案 1 :(得分:1)

添加到@VonC 的回复中

在 git-bash 中一切正常。

启动 ssh-agent 的正常流程(通过 eval 'ssh-agent' ),通过 ssh-add <path_to_key> 添加密钥可使 git clone 工作。

在 PowerShell Core 或 Cmd 中,需要通过 Windows 终端进行更多工作

ssh-agent 自动启动(假设您之前启动了 OpenSSH Authentication Agent 服务),添加密钥有效,之后您可以 ssh,但 git 命令不会工作,最初,但如果你这样做

git config --global core.sshCommand C:/Windows/System32/OpenSSH/ssh.exe

这将用 Windows10 实现替换 git-for-windows 附带的(默认)ssh。

在此之后,它应该可以在 git-bash 以外的其他地方正常工作。在 Powershell-Core 中确认,命令提示符

另见: https://gist.github.com/danieldogeanu/16c61e9b80345c5837b9e5045a701c99