我遇到了一些麻烦,让两个不同的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

我使用,

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

它运行得很好。

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

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


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

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

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