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

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

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

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


当前回答

我使用,

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

它运行得很好。

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

其他回答

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

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

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

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

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

我使用,

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

它运行得很好。

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

在我的例子中,上面的解决方案都没有解决我的问题,但是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 /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