大家好,这是我第一次为多个用户使用多个SSH密钥。我在下面列出了我执行的所有步骤:
假设我有2个GitHub用户分别附加到2个不同的GitHub帐户 user1
和user2
.
1. 我能够为每个用户生成不同的SSH密钥。
2. 同样地,我已将各自的私钥上传到每个用户帐户中。
3. 然后,我像这样设置了SSH配置文件:
Host user1.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_git_user1 # private key user1
Host user2.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_git_ user2 # private key user2
4. 我清除了SSH缓存并加载了新的身份验证信息
ssh-add -D *
eval `ssh-agent -s`
ssh-add ~/.ssh/id_git_ user1
ssh-add ~/.ssh/id_git_ user2
执行ssh-add -l
命令后,两个密钥都会列出。
5、每当我为它们中的任何一个创建新的repo时,我习惯更新本地文件.git/config
。
例如,我手动为与user1相关的每个repo添加以下配置:
[user]
name = user1
mail = user1@mail.com
我也对user2的代码库进行了同样的操作。
问题在于,每当我在步骤4
加载两个密钥时,我无法使用user1
进行git push
,它会抛出以下错误。
假设repo_user1
是与user1相关的新创建的存储库。
ERROR: Permission to user1/repo_user1.git denied to user2
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
我怀疑问题出在 Git 没有加载当前用户的正确密钥上,因为当我重新执行 步骤 4
但省略加载第二个密钥时,它可以正常工作。错误提示也显示了 denied to user2
,尽管我的配置设置为 user1
。
非常感谢任何可以避免每次推送更新都重复相同步骤的解决方法。
谢谢。
git remote get-url origin
命令,你会发现它总是显示git@github.com
。在 Github 上,ssh 用户名始终为 "git"。 - jordanm