多个GitHub账户& SSH配置

时间:2010-07-12 04:24:39

标签: ssh github ssh-keys

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

可以使用git@github.com:accountname

从一个帐户访问

可使用git@github.com:anotheraccount

从其他帐户访问

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

12 个答案:

答案 0 :(得分:297)

安迪莱斯特的反应是准确的,但我发现了我需要做的一个重要的额外步骤才能使其发挥作用。在尝试设置两个配置文件时,一个用于个人,一个用于工作,我的~/.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 :(得分:161)

我最近不得不这样做,不得不筛选所有这些答案及其评论,最终将信息拼凑在一起,所以为了方便起见,我会在一篇文章中把它全部放在这里:


第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),具体取决于您要使用的密钥/标识。但现在你应该好好去! :)

答案 2 :(得分:39)

假设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文件,你修改了你的远程origin的网址是这样的:< / p>

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

最后,您访问了存储库Settings > Deploy keys部分,并添加了alice_github.id_rsa.pub

的内容

此时,您可以在不输入密码的情况下执行git pull origin master

但是第二个存储库呢?

所以你的直觉是获取该密钥并将其添加到repo2的部署密钥,但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)

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

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

您的.ssh/config文件指向github.com,并且在知道拉动时它不知道要使用哪个密钥。

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

首先是编辑repo克隆上的.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 pull origin master而无需从两个存储库输入任何密码。

如果你有多台机器,你可以将钥匙复制到每台机器上并重复使用它们,但我建议做腿部工作,每台机器生成1个钥匙和回购。您将拥有更多要处理的密钥,但如果受到攻击,您将不那么容易受到攻击。

答案 3 :(得分:20)

我在github上有2个帐号,这就是我所做的(在linux上)以使其正常工作。

  • 通过ssh-keygen创建2对rsa密钥,正确命名,以便让生活更轻松。
  • 通过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设置远程网址:

  • 对于主持人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中的选项:

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

    为git repo设置远程网址时,这是git@之后的值,这就是回购映射到主机的方式,例如git remote set-url origin git@github-kc:kuchaguangjie/pygtrans.git


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

答案 4 :(得分:17)

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

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

答案 5 :(得分:2)

就我而言,上述解决方案都没有解决我的问题,但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

答案 6 :(得分:2)

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

  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
    
  3. 考虑到第一部分并注意proj1.github.com我们稍后会回到该部分。

    1. 将标识添加到ssh代理ssh-add ~/.ssh/proj1.id_rsa
    2. 我第一次搞砸了 - 现在当你想要克隆一个proj1 repo时,你可以使用proj1.github.com(完全是配置文件中的主机)来实现它。 git clone git@proj1.github.com
    3. A good tutorial.

      不要搞砸主人

答案 7 :(得分:1)

我发布了用于处理这些here

的技术

答案 8 :(得分:1)

我用过,

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

工作正常。

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

答案 9 :(得分:1)

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

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

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

答案 10 :(得分:1)

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

在要使用其他密钥的存储库中,运行:

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

并确保通过运行:

将您的密钥添加到ssh-agent
ssh-add ~/.ssh/id_rsa_anotheraccount

请记住,上面的命令只会为当前会话的ssh-agent添加密钥。如果你想让它永远工作,你必须永久地&#34;将它添加到您的ssh-agent。例如,ubuntuOSX的处理方式如何。

还应该可以使用全局git配置和条件包含(see example)将此方法扩展到多个存储库。功能

答案 11 :(得分:1)

请按照以下步骤进行操作,以解决它看起来很长的时间,但是请相信,它不会超过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

步骤4:修改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文件:

步骤6.1:导航到account1的项目并更新主机:

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

如果其他用户在他们的git Repository中邀请您。 然后,您需要像这样更新主机:

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

步骤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" 
相关问题