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

使用git@github.com:accountname从一个帐户访问回购

从另一个帐户使用git@github.com:另一个帐户访问回购

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


在~/.ssh/config中使用IdentityFile参数:

Host github.com
    HostName github.com
    IdentityFile ~/.ssh/github.rsa
    User petdance

安迪莱斯特的回答是准确的,但我发现我需要做一个重要的额外步骤来让它工作。在尝试设置两个配置文件时,一个用于个人,一个用于工作,我的~/。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

我在这里发布了处理这些问题的技巧

我最近不得不这么做,不得不筛选所有这些答案和他们的评论,最终把信息拼凑在一起,所以我把它们都放在这里,在一个帖子里,为了你的方便:

步骤1:ssh密钥 创建任何你需要的键盘。在这个例子中,我将我的default/original命名为'id_rsa'(这是默认的),而我的新命名为'id_rsa-work':

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

步骤2:ssh config 通过创建/修改~/.ssh/config设置多个ssh配置文件。注意'Host'值略有不同:

# 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

第四步:测试 为了测试你是否正确地做了这些,我建议你快速检查一下:

$ 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)。但是现在你应该可以出发了!:)

我使用,

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

它运行得很好。

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

假设alice是一个github.com用户,拥有2个或更多的私有存储库repoN。 对于本例,我们只使用两个名为repo1和repo2的存储库

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) keypair,然后你也去你的仓库的。git/配置文件,你修改你的远程源url如下所示:

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

最后,您进入存储库设置>部署密钥部分,并添加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。repo1.alice_github.id_rsa.pub id_rsa) Repo2密钥对:(Repo2 .alice_github。repo2.alice_github.id_rsa.pub id_rsa)

现在,您将把新的公钥放在github.com上的repo2的Deploy keys配置中,但现在您有一个ssh问题要处理。

如果存储库托管在同一个github.com域中,ssh如何告诉使用哪个键?

你的.ssh/config文件指向github.com,它不知道在需要进行拉取时使用哪个键。

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

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

对于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中提取原始主机。

如果你有多台机器,你可以把钥匙复制到每台机器上,然后重复使用,但我建议做一些跑腿的工作,每台机器生成一个钥匙,然后回收。你将有更多的钥匙要处理,但如果其中一个被泄露,你就不会那么脆弱了。

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

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

因为id_rsa_work文件在~/路径下不存在。ssh/,我手动创建这个文件,它不起作用:(

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

Keys

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


配置

~ / . ssh /配置

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远程设置-url来源git@github-kc:kuchaguangjie/pygtrans.git 对于主机github-abc中的回购: Git远程集-url来源git@github-abc:abcdefg/yyy.git


解释

~/.ssh/config中的选项:

Host github-<identify_specific_user> Host could be any value that could identify a host plus an account, it don't need to be a real host, e.g github-kc identify one of my account on github for my local laptop, When set remote url for a git repo, this is the value to put after git@, that's how a repo maps to a Host, e.g git remote set-url origin git@github-kc:kuchaguangjie/pygtrans.git [Following are sub options of Host] Hostname specify the actual hostname, just use github.com for github, User git the user is always git for github, IdentityFile specify key to use, just put the path the a public key, LogLevel specify log level to debug, if any issue, DEBUG3 gives the most detailed info.


在我的例子中,上面的解决方案都没有解决我的问题,但是ssh-agent解决了。基本上,我做了以下事情:

使用ssh-keygen生成密钥对,如下所示。它将生成一个密钥对(在本例中是。\keyfile和。\keyfile.pub) ssh -t rsa -b 4096 -C "yourname@yourdomain" -f keyfile . sh 上传密钥文件。Pub到git提供程序 在您的机器上启动ssh-agent(您可以使用ps -ef | grep ssh-agent检查它是否已经在运行) 运行ssh-add .\keyfile添加凭证 现在你可以运行git clone git@provider:username/project.git

我花了很多时间来理解所有的步骤。让我们一步一步来描述:

使用ssh-keygen -t rsa创建新的身份文件。给它一个替代方案,比如proj1。Id_rsa和命中,因为您不需要密码短语。 在.ssh/config中添加新的section: 主机proj1.github.com 主机名:github.com PreferredAuthentications publickey IdentityFile ~ / . ssh / proj1.id_rsa

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

将标识添加到ssh代理ssh- Add ~/.ssh/proj1.id_rsa 这就是我第一次搞砸的地方-现在当你想克隆一个proj1 repo时,你使用proj1.github.com(正是配置文件中的主机)来做它。 Git克隆git@proj1.github.com。

一个很好的教程。

不要惹主人

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

在你想要使用不同键的存储库中,运行:

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

如果您的密钥是受密码保护的,并且您不想每次都输入密码,那么您必须将其添加到ssh-agent。下面是ubuntu和macOS的操作方法。

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

使用多个Git帐户和克隆而不做任何更改的最简单方法是将您的用户名添加到ssh配置中。

开放的~ / . ssh / config。如果一个人不存在,就去创造 添加主机条目,如下所示

Host github.com:<YOUR_GITHUB_USERNAME>
  AddKeysToAgent yes
  IdentityFile ~/.ssh/<YOUR_SSH_KEY_FILENAME>

将<YOUR_SSH_KEY_FILENAME>替换为您在。ssh文件夹中的密钥文件名(例如:id_ed25519)。 为每个要维护的github帐户添加一条主机记录。

现在git克隆将简单的git克隆github.com:<YOUR_GITHUB_USERNAME>/your-repo.git。克隆时不需要记住自定义gihub域;-)

做了一个主旨与进一步的信息https://gist.github.com/udantha/fed5439630eaf4651272f4fba6e1c6a3