传递给GIT_SSH_COMMAND的参数“-i”被忽略了。

19

我想在Git中使用另一个IdentityFile。 我想要动态地使用它,而不是通过配置文件。 我正在这样做:

  $ GIT_SSH_COMMAND='ssh -i /home/my_user/.ssh/id_ed25519' git pull origin master
  repository access denied.
  fatal: Could not read from remote repository.

  Please make sure you have the correct access rights
  and the repository exists.
“id_ed25519.pub”公钥在我的Bitbucket上。

这个也失败了:

  $ git pull origin master
  repository access denied.
  fatal: Could not read from remote repository.

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

并且:

$ git remote -v
origin  git@bitbucket.org:company123/repo456.git (fetch)
origin  git@bitbucket.org:company123/repo456.git (push)

'ssh -i /home/my_user/.ssh/id_ed25519' 后面 添加 "-v",可以看到使用了我的 RSA 密钥,而不是 ED 密钥。为什么?

3个回答

20

我在最近的Ubuntu版本中遇到了相同的问题:

使用-vvv揭示了以下内容:

debug2: key: /home/ubuntu/.ssh/id_rsa (0x5628e48246d0), agent
debug2: key: /home/ubuntu/code/id_rsa (0x5628e4820af0), explicit

添加-o IdentitiesOnly=yes解决了这个问题。它告诉SSH忽略来自ssh-agent(例如使用ssh-add添加的)的身份验证信息,否则它们将优先。

完整的git命令:

GIT_SSH_COMMAND='ssh -o IdentitiesOnly=yes -i /home/ubuntu/code/id_rsa -F /dev/null' git pull

我添加了一句话简要描述了IdentitiesOnly的作用。如果您不同意这个更改,请随时进行编辑/还原。 - luator

11

请检查您的命令(是直接调用git还是通过别名调用)和配置:
如我在“使用”中提到的,git config -l 可能会显示其他配置,这些配置可能会覆盖环境变量。

请检查 git config core.sshCommand 的返回结果。

最后,GIT_SSH_COMMAND 是Git 2.10+ 版本才有的功能,如果您的Git版本太旧,您需要先更新它。


4
"git太老"是我的问题。 - studog
git --version 显示 "git version 2.33.0",但仍然存在这个问题。 - Truong Hieu
@TruongHieu 2.40 版的 git 也有这个吗? - VonC

3

如果有人在设置GIT_SSH_COMMAND时出现了稍微不同的问题,然后在另一行上实际运行git命令,请尝试以下方法之一:

  1. 在同一行上设置GIT_SSH_COMMAND
$ GIT_SSH_COMMAND="ssh -i ${key_location}" git clone [...]

或者...

  1. 导出 GIT_SSH_COMMAND
$ export GIT_SSH_COMMAND="ssh -i ${key_location}"
$ git clone [...]

我在找到要使用的密钥后,设置了GIT_SSH_COMMAND,然后几行代码后运行了git。但是我错误地删除了export,导致git命令出现问题。


由于这是搜索“GIT_SSH_COMMAND”时排名靠前的谷歌链接之一,并且该问题的提问者的问题得到更多赞同,可能有些访客遇到的问题与其他答案推荐的原因不同。希望对某些人有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接