尝试通过SSH克隆使用了错误的公钥

时间:2019-08-15 02:38:28

标签: git macos ssh bitbucket

我创建了一对新的专门用于使用bitbucket的ssh密钥,它们位于我的~/.ssh文件夹中,如下所示:

config id_rsa.pub id_rsa_alt.pub id_rsa id_rsa_alt

我想将rsa_alt密钥对用于bitbucket。

首先,我将id_rsa_alt.pub的内容添加到了我的Bitbucket帐户中。

我意识到,当我尝试通过SSH连接到我的bitbucket帐户时,将不知道是否使用替代密钥对的原始密钥对。因此,我在config内的~/ .ssh文件中添加了以下内容。

# bitbucket account
Host bitbucket.org-BitBucketUsername
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/id_rsa_alt
    IdentitiesOnly yes

我也在Mac上这样更改了git名称:

git config --global user.name "BitBucketUsername"

但是,当我尝试克隆这样的仓库时,git clone git@bitbucket.org:myteam/myproject.git

它要求输入id_rsa而不是id_rsa_alt的密码,像这样:

Enter passphrase for key '/Users/me/.ssh/id_rsa':

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

如果要使用在~/.ssh/config中配置的内容,则需要使用SSH URL:

bitbucket.org-BitBucketUsername:myteam/myproject.git

这将使用~/.ssh/id_rsa_alt私钥。

所以:

cd /path/to/local/cloned/repository
git remote set-url origin bitbucket.org-BitBucketUsername:myteam/myproject.git