【问题标题】:How to manage one only key per each git repository?如何为每个 git 存储库管理一个唯一的密钥?
【发布时间】:2014-04-02 14:00:51
【问题描述】:

我在两种情况下使用git:

  • 我使用一些 Github 存储库。
  • 我目前正在使用 OpenShift,它使用 sshgit 进行部署。

首先,我使用ssh-keygen 生成在 OpenShift 站点更新的密钥。此类密钥存储在 ~/.ssh/ 并创建 id_rsaid_rsa.pub

然后我开始从 Github 克隆一个存储库,我曾经做过一次 ssh-keygen 并开始推送,它工作正常。然后我克隆了另一个存储库并开始遇到问题:

克隆到第二个存储库时遇到问题。每次我尝试推送时都会显示如下内容:

ERROR: Permission to diegoaguilar/cursoJava.git denied to diegoaguilar/cursoCannibalCreatures. fatal: The remote end hung up unexpectedly

但可以看出 diegoaguilar/cursoCannibalCreatures 不正确,因为它是 另一个 存储库。

我什至尝试删除这样的存储库目录,然后再次克隆它,同样发生了。

我已经下~/.ssh:

config

Host cursoJava
Hostname github.com
User git
IdentityFile ~/.ssh/id_java

Host cursoCannibalCreatures
Hostname github.com
User git
IdentityFile ~/.ssh/id_cannibal

Host openshift
Hostname openshift.com
User git
IdentityFile ~/.ssh/openshift

所以得到了:

id_cannibal  id_cannibal.pub  id_java  id_java.pub  known_hosts

id_openshiftid_openshift.pub 之类的东西不存在,但由于它不起作用,我现在不太在意。

我创建了这样的文件,它们是ssh-keygen -f <filename>.pub,并为每个文件提供了不同的密码。我在每个 Github 存储库设置中添加了 .pub 的内容作为部署密钥。

我做错了什么?这应该如何工作?而且,在另一台机器上工作时,如何正确获取这些密钥,证明是我并透明地工作?

编辑

git remote -v 的输出:

  • 用于cursoJava存储库

origin git@github.com:diegoaguilar/cursoJava.git (fetch) origin git@github.com:diegoaguilar/cursoJava.git (push)

  • 对于 cursoCannibalCreatures

origin git@github.com:diegoaguilar/cursoCannibalCreatures.git (fetch) origin git@github.com:diegoaguilar/cursoCannibalCreatures.git (push)

【问题讨论】:

  • 您的公共 ssh 密钥是否在您的 GitHub 存储库管理页面中正确注册了正确的 Github 存储库?
  • 是的,我仔细检查过。我添加了我尝试过的所有内容,@VonC​​pan>
  • 你能复制git remote -vgit branch -avvv的结果,以及你输入的确切的git push命令,以及它的确切输出吗?
  • 好的,请稍等
  • 完成了,@VonC 我想这意味着没关系或应该......我现在试着推动......

标签: git github ssh


【解决方案1】:

正如“ssh,github,it doesnot work”中提到的,诀窍是不要为您的公钥使用默认的 id_rsa(.pub) 名称:私钥(因为您只能定义 一个 一对) ,但名称不同。

但前提是您以不同的用户

访问这些存储库

在您的情况下,您使用相同的用户访问存储库,并且一个 ssh 密钥就足够了。

见“GitHub help”:

此错误意味着您推送的密钥作为部署密钥附加到另一个存储库,并且无权访问您尝试推送到的存储库。

要解决此问题,请从存储库中删除部署密钥,并 attach the key to your user account instead


这是为两个不同的用户使用 GitHub。

然后定义一个~/.ssh/config 文件,在其中通过完整路径引用每个私钥:

Host github1
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_repo1

Host github2
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_repo2

不要使用git@gihub.com:user/repo1,而是使用:

github1:user/repo1

这使用密钥 Host 条目“github1”来引用用户 (git)、主机名 (github.com) 以及使用 ~/.ssh/id_repo1(.pub) 的确切私钥/公钥


因此,如果您有第二个 repo,它使用存储为 ~/.ssh/id_repo2(.pub) 的第二个密钥,您需要使用上面定义的条目 'github2'(您可以根据需要命名),然后更改您的 url有原产地:

git remote set-url origin github2:user/repo2

这样,git push 将使用正确的键(repo2 的那个键)

如果您不这样做,您将能够推送一个 repo(使用默认密钥 ~/.ssh/id_rsa(.pub),默认名称),但您将无法推送到需要不同集合的第二个 repo公钥/私钥。

【讨论】:

  • 它在我得到的另一个 github 文件夹中失败了,但在我遇到问题的那个文件夹中没有。当我尝试git push 时,我得到了同样的错误
  • 在这种情况下,两个存储库都属于同一用户,这将如何更改配置文件以及git remote 命令?
  • @diegoaguilar 我没有意识到这两种情况下都是针对同一个用户的。我已经编辑了我的答案。
  • @Noitidart 是的,您可以将GIT_SSH_COMMAND 设置为ssh -v。然后再次尝试您的git clone。您将需要查看一些日志。
  • @Noitidart 您可以通过配置进行相同的调试:stackoverflow.com/a/38474400/6309
猜你喜欢
  • 2016-04-24
  • 2016-05-20
  • 1970-01-01
  • 2014-11-11
  • 2019-04-08
  • 1970-01-01
  • 2015-09-14
  • 2020-10-25
  • 2021-12-01
相关资源
最近更新 更多