使用多个Github用户SSH密钥出现"Permission denied"错误。

4

大家好,这是我第一次为多个用户使用多个SSH密钥。我在下面列出了我执行的所有步骤:

假设我有2个GitHub用户分别附加到2个不同的GitHub帐户 user1user2.

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

非常感谢任何可以避免每次推送更新都重复相同步骤的解决方法。

谢谢。


问题在于,就SSH而言,它们都是名为“git”的相同用户。 - jordanm
@jordanm,你能提供更多细节吗? - Mahery Ranaivoson
如果在你的代码库上运行 git remote get-url origin 命令,你会发现它总是显示 git@github.com。在 Github 上,ssh 用户名始终为 "git"。 - jordanm
1个回答

3

请尝试使用以下的 ~/.ssh/config 文件:

Host user1.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_git_user1      # private key user1
    User git

Host user2.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_git_ user2      # private key user2 
    User git

然后,对于repo1,请确保使用正确的SSH URL:

cd /path/to/repo1
git remote set-url user1.github.com:user1/repo_user1.git

这将使用~/.ssh/id_git_user1


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