gpt4 book ai didi

git - 使用多个 Git 用户 SSH key 的权限被拒绝

转载 作者:行者123 更新时间:2023-12-05 04:57:20 25 4
gpt4 key购买 nike

伙计们,这是我第一次为多个用户使用多个 SSH key 。我在下面指出我执行的所有步骤:

假设我有 2 个 GitHub 用户连接到 2 个不同的 GitHub 帐户,user1user2分别。

1。我能够为每个用户生成不同的 SSH key

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 存储库做了同样的事情

问题是,每当我在 step 4 加载两个 key 时,我无法做到 git push使用 user1 ,并引发此错误。
让我们假设 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.

我怀疑问题是,当我重做 step 4 时,git 未能加载当前用户的右键 并省略加载第二个 key ,它工作正常。该错误还显示 denied to user2尽管我的配置设置为 user1 .

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

谢谢,

最佳答案

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

关于git - 使用多个 Git 用户 SSH key 的权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64465924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com