使用多个Github用户SSH密钥拒绝权限

omvjsjqw  于 2023-01-24  发布在  Git
关注(0)|答案(1)|浏览(163)

朋友们,这是我第一次为多个用户使用多个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相关的每个存储库追加以下配置:

[user]
    name = user1
    mail = user1@mail.com

我对user2存储库做了同样的操作
问题是,每当我在**step 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无法加载当前用户的**右键,原因是当我重做step 4**并忽略加载第二个键时,它工作正常。错误还显示denied to user2,尽管我的配置设置为user1
我真的很感激任何变通办法,以避免重复相同的步骤,每次我推我的更新。
谢谢你,

62lalag4

62lalag41#

尝试使用以下~/.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

然后,对于repo 1,确保使用正确的SSH URL:

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

这将使用~/.ssh/id_git_user1

相关问题