github部署密钥:如何为单个机器授权多个存储库

use*_*512 36 github ssh-keys

所以,我有一个主持人,称之为rob.我使用ssh-keygen rob来获取一个公钥,我在为存储库添加一个新的部署密钥屏幕时给了github cheech.现在我想部署chongrob为好.但是,如果我去添加新的部署关键屏幕仓库chong在github上,并粘贴在公共密钥我生成的rob,它说key already in use.我想,如果他们的密钥被使用,我可以克隆chong,rob但是说允许被拒绝.

很明显,这比我想象的要复杂得多,它涉及多个键或其他东西.我应该怎么做克隆chongrob

谢谢您的帮助.

acu*_*ich 64

一旦将密钥作为部署密钥附加到一个存储库,就不能在另一个存储库上使用它.如果您在设置部署密钥时遇到此错误,则需要修改远程并设置 ~/.ssh/config文件以使用不存在的 github.com主机名,ssh可以使用该主机名来选择正确的ssh为您的存储库部署密钥.

# first we remove the origin
$ git remote -v
origin  git@github.com:username/foo.git (fetch)
origin  git@github.com:username/foo.git (push)
$ git remote rm origin

# here we add a new origin using a host nickname called
# foo.github.com that we will reference with a Host stanza in our
# ~/.ssh/config to specify which key to use with which fake hostname.
$ git remote add origin git@fake-hostname-foo.github.com:username/foo.git
$ git remote -v
origin  git@fake-hostname-foo.github.com:username/foo.git (fetch)
origin  git@fake-hostname-foo.github.com:username/foo.git (push)
Run Code Online (Sandbox Code Playgroud)

为您的存储库生成部署密钥,并将其命名为合理的:

$ ssh-keygen -t rsa -f ~/.ssh/id_rsa-foo -C https://github.com/username/foo
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/username/.ssh/id_rsa-foo.
Your public key has been saved in /home/username/.ssh/id_rsa-foo.pub.
The key fingerprint is:
c0:ff:ee:34:24:11:5e:6d:7c:4c:b1:a0:de:ad:be:ef https://github.com/username/foo
The key's randomart image is:
+--[ RSA 2048]----+
|  E   o..o.oo.   |
| M o o o .+CoW   |
|  + o = o. ..    |
| .   . +         |
|        S        |
|       o .       |
|        +        |
|       . o       |
|        ..o.     |
+-----------------+
Run Code Online (Sandbox Code Playgroud)

一旦你 添加了部署关键 然后你会需要以下节添加到您的~/.ssh/config文件:

Host fake-hostname-foo.github.com
    Hostname github.com
    IdentityFile ~/.ssh/id_rsa-foo
Run Code Online (Sandbox Code Playgroud)

现在你可以测试它:

$ ssh -T git@fake-hostname-foo.github.com
Hi username! You've successfully authenticated, but GitHub
does not provide shell access.
Run Code Online (Sandbox Code Playgroud)

  • 在几个回购中使用相同的密钥有什么问题?为什么我们要做这样的黑客攻击? (5认同)
  • 很好的答案,非常有帮助.工作完美.谢谢 (3认同)
  • @kontextify Github 限制每个 repo 的部署密钥。你也不能分享它们。作为次要说明,请确保您不保留 id_rsa 或 github 将继续默认使用它。 (2认同)

Nar*_*pai 11

我找到的最简单的解决方案在这里概述.

1)输入此命令(您将为所需的许多键执行此操作):

ssh-keygen -t rsa -C"your_email@example.com"

2)当提示下面的语句时,键入一个唯一的名称(即foo1_rsa).该文件将在您当前的目录中创建,如果您想要整洁,可能需要将其移动到.ssh:

输入要保存密钥的文件(/Users/you/.ssh/id_rsa):[按Enter键]

3)更新SSH配置文件:

vi~/.ssh/config

哪个可能是空的:

Host cheech github.com
Hostname github.com
IdentityFile ~/.ssh/foo1_rsa

Host chong github.com
Hostname github.com
IdentityFile ~/.ssh/foo2_rsa
Run Code Online (Sandbox Code Playgroud)

  • 这不起作用。Git 将使用第一个匹配的主机名(即`github.com`),因此仅适用于配置文件中第一个列出的主机。 (2认同)

kla*_*her 2

github 的部署密钥是唯一的......您必须为其他存储库生成一个新密钥。只需再次运行 ssh-keygen

请参阅 github 文档:https://help.github.com/articles/managing-deploy-keys