访问bitbucket repo多个密钥时git ssh授权错误

时间:2017-12-12 00:50:16

标签: git ssh bitbucket

我有多个ssh密钥,一个项目使用一个密钥。我已成功将公共ssh密钥分配给我的bitbucket帐户中的相关存储库。

它们存储在以下位置:

~/.ssh/rsa_generic_repos
~/.ssh/rsa_generic_repos.pub
~/.ssh/rsa_project1
~/.ssh/rsa_project1.pub

然后我在尝试任何git访问之前将这些密钥添加到我的ssh-agent中:

ssh-add ~/.ssh/rsa_generic_repos
ssh-add ~/.ssh/rsa_project1

ssh-add -l - 显示:

4096 SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXX Generic Repo Key (RSA)
4096 SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXX Project 1 Key (RSA)

我的问题:

这可以正常工作(克隆回购):

git clone git@bitbucket.org:Myusername/generic-repo.com.git

这不起作用:

git clone git@bitbucket.org:Myusername/project1.com.git

错误:

Cloning into 'project1'...
repository access denied. deployment key is not associated with the requested repository.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

但如果我跑:

ssh-add -D
ssh-add ~/.ssh/rsa_project1
git clone git@bitbucket.org:Myusername/project1.com.git

它成功地克隆了之前不会的回购。这首先表明公钥是在bitbucket上正确设置的,并且ssh守护程序没有尝试使用除第一个条目之外的任何ssh密钥,因此导致上述错误。

如果有人能帮我找到ssh来浏览ssh-agent会话中存储的所有密钥,我将非常感激。

感谢您的帮助和时间。

2 个答案:

答案 0 :(得分:1)

使用多个ssh密钥的正确方法是~/.ssh/config文件,I describe here

Host bbgeneric
    Hostname bitbucket.org
    IdentityFile ~/.ssh/rsa_generic_repos
    User git

Host bbproject1
    Hostname bitbucket.org
    IdentityFile ~/.ssh/rsa_project1
    User git

你会使用像

这样的ssh网址
bbgeneric:Myusername/generic-repo.com.git
bbproject1:Myusername/project1.com.git

使用一个部署密钥确实更容易,但我想说明配置ssh功能,它允许您使用任意数量的密钥。

答案 1 :(得分:1)

感谢VonC的回答。
这是我本可以使用的工作解决方案:

〜/ .ssh / config

Host bitbucket-generic-repos
    HostName bitbucket.org
    IdentityFile ~/.ssh/rsa_generic_repos

Host bitbucket-project1
    HostName bitbucket.org
    IdentityFile ~/.ssh/rsa_project1

以下命令给我一个错误:

git clone git@bitbucket.org:<MyUsername>/project1.com.git

在git命令中用bitbucket.org中定义的ssh别名替换~/.ssh/config会导致期望的行为而没有错误:

git clone git@bitbucket-project1:<MyUsername>/project1.com.git (works!)
git clone git@bitbucket-generic-repos:<MyUsername>/project1.com.git (also works!)