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

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

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

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


当前回答

使用多个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

其他回答

我在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/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

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

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

我使用,

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

它运行得很好。

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