281

我在让两个不同的 SSH 密钥/GitHub 帐户一起玩时遇到了一些麻烦。我有以下设置:

可以从一个帐户访问回购协议git@github.com:accountname

使用从另一个帐户访问的存储库git@github.com:anotheraccount

每个帐户都有自己的 SSH 密钥。两个 SSH 密钥都已添加,并且我创建了一个配置文件。我不相信配置文件是正确的。我不太确定如何指定使用git@github.com:accountname应该使用id_rsagit@github.com:anotheraccount应该使用访问的存储库id_rsa_anotheraccount

4

12 回答 12

349

Andy Lester 的回答是准确的,但我发现我需要采取一个重要的额外步骤才能使其发挥作用。在尝试设置两个配置文件时,一个用于个人,一个用于工作,我~/.ssh/config的大致如下:

Host me.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/me_rsa

Host work.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/work_rsa

我的工作资料直到我做了一个ssh-add ~/.ssh/work_rsa. 之后与 github 的连接使用了正确的配置文件。以前他们默认使用第一个公钥。

对于使用时无法打开与您的身份验证代理的连接ssh-add
检查: https ://stackoverflow.com/a/17695338/1760313

于 2011-12-13T03:25:00.127 回答
192

我最近不得不这样做,并且必须筛选所有这些答案和他们的评论,最终将信息拼凑在一起,所以为了您的方便,我将把它们都放在一个帖子中:


第 1 步:ssh 密钥
创建您需要的任何密钥对。在此示例中,我将我命名为默认/原始“id_rsa”(这是默认值)和我的新名称“id_rsa-work”:

ssh-keygen -t rsa -C "stefano@work.com"


第 2 步:ssh config通过创建/修改 ~/.ssh/config
来设置多个 ssh 配置文件。请注意略有不同的“主机”值:

# Default GitHub
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa

# Work GitHub
Host work.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_work


第 3 步:ssh-add
您可能需要也可能不需要这样做。要检查,请运行以下命令列出身份指纹:

$ ssh-add -l
2048 1f:1a:b8:69:cd:e3:ee:68:e1:c4:da:d8:96:7c:d0:6f stefano (RSA)
2048 6d:65:b9:3b:ff:9c:5a:54:1c:2f:6a:f7:44:03:84:3f stefano@work.com (RSA)

如果您的条目不存在,则运行:

ssh-add ~/.ssh/id_rsa_work


第 4 步:测试
为了测试您是否正确完成了这一切,我建议进行以下快速检查:

$ ssh -T git@github.com
Hi stefano! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T git@work.github.com
Hi stefano! You've successfully authenticated, but GitHub does not provide shell access.

请注意,您必须根据您要使用的密钥/身份更改主机名(github / work.github)。但现在你应该好好去!:)

于 2013-06-18T00:39:37.127 回答
48

假设alice是一个 github.com 用户,拥有 2 个或更多私有存储库repoN。对于这个例子,我们将只使用两个名为repo1repo2

https://github.com/alice/repo1

https://github.com/alice/repo2

您需要从这些存储库中提取而无需在服务器或多台服务器上输入密码。例如,您想要执行git pull origin master,并且您希望在不要求密码的情况下发生这种情况。

您不喜欢与 ssh-agent 打交道,您已经发现(或者您现在正在发现)~/.ssh/config一个文件,该文件让您的 ssh 客户端根据主机名和用户名知道要使用的私钥,并带有一个看起来像这样的简单配置条目这:

Host github.com
  HostName github.com
  User git
  IdentityFile /home/alice/.ssh/alice_github.id_rsa
  IdentitiesOnly yes

所以你继续创建你的(alice_github.id_rsa, alice_github.id_rsa.pub)密钥对,然后你也去你的存储库的.git/config文件,你修改了你的遥控器的 url 是origin这样的:

[remote "origin"]
        url = "ssh://git@github.com/alice/repo1.git"

最后你去了存储库Settings > Deploy keys部分并添加了alice_github.id_rsa.pub

此时,您可以在git pull origin master不输入密码的情况下毫无问题地进行操作。

但是第二个存储库呢?

因此,您的直觉是获取该密钥并将其添加到repo2的 Deploy 密钥中,但 github.com 会出错并告诉您该密钥已被使用。

现在你去生成另一个密钥(ssh-keygen -t rsa -C "alice@alice.com"当然使用没有密码),这样就不会变得一团糟,你现在将这样命名你的密钥:

  • repo1密钥对:(repo1.alice_github.id_rsa, repo1.alice_github.id_rsa.pub)
  • repo2密钥对:(repo2.alice_github.id_rsa, repo2.alice_github.id_rsa.pub)

您现在将新的公钥放在repo2github.com 的 Deploy keys 配置中,但现在您需要处理 ssh 问题。

github.com如果存储库托管在同一个域上,ssh 如何判断使用哪个密钥?

您的.ssh/config文件指向github.com并且它不知道在进行拉取时使用哪个键。

所以我在 github.com 上找到了一个窍门。您可以告诉您的 ssh 客户端,每个存储库都位于不同的 github.com 子域中,在这些情况下,它们将是repo1.github.com并且repo2.github.com

所以第一件事是编辑.git/config你的 repo 克隆上的文件,所以它们看起来像这样:

对于 repo1

[remote "origin"]
        url = "ssh://git@repo1.github.com/alice/repo1.git"

对于 repo2

[remote "origin"]
        url = "ssh://git@repo2.github.com/alice/repo2.git"

然后,在您的.ssh/config文件中,现在您将能够为每个子域输入配置:)

Host repo1.github.com
  HostName github.com
  User git
  IdentityFile /home/alice/.ssh/repo1.alice_github.id_rsa
  IdentitiesOnly yes

Host repo2.github.com
  HostName github.com
  User git
  IdentityFile /home/alice/.ssh/repo2.alice_github.id_rsa
  IdentitiesOnly yes

现在,您git pull origin master无需从两个存储库中输入任何密码即可。

如果您有多台机器,您可以将密钥复制到每台机器并重复使用它们,但我建议您做一些工作以在每台机器和存储库中生成 1 个密钥。您将有更多的密钥需要处理,但如果其中一个被泄露,您将不会那么容易受到攻击。

于 2014-10-22T12:38:06.137 回答
31

我在 github 上有 2 个帐户,这就是我所做的 (on linux) 使其工作。

钥匙

  • 创建 2 对 rsa 密钥,通过ssh-keygen,正确命名它们,让生活更轻松。
  • 通过将私钥添加到本地代理ssh-add path_to_private_key
  • 对于每个 github 帐户,上传一个(不同的)公钥。

配置

~/.ssh/config

Host github-kc
    Hostname        github.com
    User git
    IdentityFile    ~/.ssh/github_rsa_kc.pub
    # LogLevel DEBUG3

Host github-abc
    Hostname        github.com
    User git
    IdentityFile    ~/.ssh/github_rsa_abc.pub
    # LogLevel DEBUG3

为 repo 设置远程 url:

  • 对于主机中的回购github-kc

    git remote set-url origin git@github-kc:kuchaguangjie/pygtrans.git
    
  • 对于主机中的回购github-abc

    git remote set-url origin git@github-abc:abcdefg/yyy.git
    

解释

中的选项~/.ssh/config

  • Hostgithub-<identify_specific_user>
    主机可以是任何可以识别主机和帐户的值,它不需要是真正的主机,例如 github-kc在 github 上为我的本地笔记本电脑识别我的一个帐户,

    当为 git repo 设置远程 url 时,这是放在 after 的值,这就是git@repo 映射到主机的方式,例如git remote set-url origin git@github-kc:kuchaguangjie/pygtrans.git


  • [以下是 的子选项Host]
  • Hostname
    指定实际主机名,仅github.com用于 github,
  • Usergit
    用户总是git为 github 服务的,
  • IdentityFile
    指定要使用的密钥,只需将路径放入公钥,
  • LogLevel
    指定要调试的日志级别,如果有任何问题,DEBUG3会提供最详细的信息。

于 2015-11-05T06:15:07.620 回答
19

使用IdentityFile您的参数~/.ssh/config

Host github.com
    HostName github.com
    IdentityFile ~/.ssh/github.rsa
    User petdance
于 2010-07-12T04:30:04.147 回答
8

编辑 ssh 配置文件的一种可能更简单的替代方法(如所有其他答案中所建议的)是将单个存储库配置为使用不同的(例如非默认)ssh 密钥。

在要使用不同密钥的存储库中,运行:

git config core.sshCommand 'ssh -i ~/.ssh/id_rsa_anotheraccount'

如果您的密钥受密码保护并且您不想每次都输入密码,则必须将其添加到 ssh-agent。这是ubuntumacOS的操作方法。

还应该可以使用全局 git config 和条件包含将这种方法扩展到多个存储库(参见示例)。

于 2018-06-07T17:08:57.173 回答
6

我花了很多时间来了解所有步骤。因此,让我们逐步描述:

  1. 使用 . 创建新的身份文件ssh-keygen -t rsa。给它一个替代的喜欢proj1.id_rsa并毫无疑问地点击,因为你不需要密码。
  2. 在中添加新部分.ssh/config

    Host proj1.github.com
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/proj1.id_rsa
    

考虑第一部分,并注意proj1.github.com我们稍后会回到该部分。

  1. 将身份添加到 ssh 代理ssh-add ~/.ssh/proj1.id_rsa
  2. 那是我第一次搞砸的事情-现在,当您要克隆 proj1 存储库时,您可以使用它proj1.github.com(确切地说是配置文件中的主机)。 git clone git@proj1.github.com.

一个很好的教程。

不要与主机混淆

于 2017-09-16T18:20:42.183 回答
4

就我而言,上述解决方案都没有解决我的问题,但 ssh-agent 可以。基本上,我做了以下事情:

  1. 使用如下所示的 ssh-keygen 生成密钥对。它将生成一个密钥对(在本例中.\keyfile.\keyfile.pub

    ssh-keygen -t rsa -b 4096 -C "yourname@yourdomain" -f keyfile

  2. 上传keyfile.pub到 git 提供者

  3. 在您的机器上启动 ssh-agent(您可以检查ps -ef | grep ssh-agent它是否已经在运行)
  4. 运行ssh-add .\keyfile以添加凭据
  5. 现在你可以运行git clone git@provider:username/project.git
于 2016-01-26T02:57:54.640 回答
3

作为@stefano 答案的补充,最好在-f为另一个帐户生成新的 SSH 密钥时使用命令,

ssh-keygen -t rsa -f ~/.ssh/id_rsa_work -C "your@mail.com"

由于id_rsa_work路径中不存在文件~/.ssh/,因此我手动创建了此文件,但它不起作用:(

于 2015-03-28T09:01:02.410 回答
3

请按照以下步骤解决此问题,它看起来太长但相信我不会超过 5 分钟:

步骤 1:创建两个 ssh 密钥对:

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

第 2 步:它将在此处创建两个 ssh 密钥:

~/.ssh/id_rsa_account1
~/.ssh/id_rsa_account2

步骤 3:现在我们需要添加这些键:

ssh-add ~/.ssh/id_rsa_account2
ssh-add ~/.ssh/id_rsa_account1
  • 您可以使用以下命令查看添加的密钥列表:ssh-add -l
  • 您可以通过以下命令删除旧的缓存键:ssh-add -D

第四步:修改 ssh 配置

cd ~/.ssh/
touch config

subl -a configcode confignano config

第 5 步:将其添加到配置文件:

#Github account1
Host github.com-account1
    HostName github.com
    User account1
    IdentityFile ~/.ssh/id_rsa_account1

#Github account2
Host github.com-account2
    HostName github.com
    User account2
    IdentityFile ~/.ssh/id_rsa_account2

第 6 步:更新您的.git/config文件:

Step-6.1:导航到account1的项目并更新主机:

[remote "origin"]
        url = git@github.com-account1:account1/gfs.git

如果您在他们的 git 存储库中受到其他用户的邀请。然后你需要像这样更新主机:

[remote "origin"]
            url = git@github.com-account1:invitedByUserName/gfs.git

Step-6.2:导航到account2的项目并更新主机:

[remote "origin"]
        url = git@github.com-account2:account2/gfs.git

第 7 步:如果需要,分别更新每个存储库的用户名和电子邮件,这不是修改步骤:

导航到 account1 项目并运行这些:

git config user.name "account1"
git config user.email "account1@domain.com" 

导航到 account2 项目并运行这些:

git config user.name "account2"
git config user.email "acccount2@doamin.com" 
于 2020-11-24T14:23:09.347 回答
2

我在这里发布了我用来处理这些问题的技术

于 2012-03-04T03:45:16.917 回答
2

我用了,

Host github.com
   HostName github.com
   IdentityFile ~/.ssh/github_rsa
   User abc@gmail.com

它工作得很好。

使用 .ssh/config 文件中的上述设置为不同的用户名设置不同的 rsa 密钥。

于 2013-07-17T17:10:31.927 回答