设置多个SSH密钥访问Github
我正在尝试配置一台远程 linux 机器来访问我拥有的两个不同的 git 帐户。我不断获得身份验证访问权限。
> git push
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
我采取的步骤,
- 生成了两组 ssh 密钥(调用它们
key1和key2) - 生成了一个配置文件在
~/.ssh/config - 编辑了那个文件
1 # Default github account: key1
2 Host github.com
3 HostName github.com
4 IdentityFile ~/.ssh/key1
5 IdentitiesOnly yes
6
7 # Other github account: key2
8 Host github-key2
9 HostName github.com
10 IdentityFile ~/.ssh/key2
11 IdentitiesOnly yes
- 将
.pub版本添加到我的用户帐户下的两个 git 帐户中settings/ssh keys。 - 将它们添加到我的 VM 上
ssh-add ~/.ssh/key1
我不确定从这里去哪里以及下一步是什么,为什么这不起作用。我觉得我可能遗漏了一步。
回答
TL;博士:将以下条目添加到您的~/.ssh/config地方$user是GitHub的用户名和$key是在设置为GitHub的用户为该用户指定和定义的私有密钥:
Host github.com-$user
Hostname github.com
User git
IdentityFile ~/.ssh/$key
IdentitiesOnly yes
然后,配置您的 git 远程 URL:
git remote add origin git@github.com-$user:$user/$repo
基本上,我有一个用于工作/个人项目的长期运行的 SSH 密钥,然后想为不相关的副项目添加一个新密钥。我执行了您在问题中概述的所有步骤,但仍然出现相同的错误。正在做$ GIT_SSH_COMMAND='ssh -v' git fetch表明 git 首先传递了我的主要用户的私钥,即使它作为有效密钥正确地进行了身份验证,该用户当然对新用户的私有存储库没有权限。
这篇文章是相关的:https : //medium.com/@ordinary.yobi/multiple-ssh-keys-on-github-f75a372d5374
这是我的第一个答案,希望对您有所帮助。我刚刚解决了这个确切的问题,这非常令人沮丧。
编辑:哎呀,忘记了关键部分。对不起!