首页 文章

多个GitHub帐户和SSH配置

提问于
浏览
226

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

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

可以使用 git@github.com:anotheraccount 从其他帐户访问的Repos

每个帐户都有自己的SSH密钥 . 两个SSH密钥都已添加,我已经创建了一个配置文件 . 我不知道如何指定使用 git@github.com:accountname 访问的repos应该使用 id_rsagit@github.com:anotheraccount 应该使用 id_rsa_anotheraccount .

11 回答

  • 16

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

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

    安迪莱斯特的反应是准确的,但我发现了我需要做的一个重要的额外步骤才能让它发挥作用 . 在尝试设置两个配置文件时,一个用于个人,一个用于工作,我的 ~/.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的连接使用了正确的配置文件 . 以前他们违约了第一张公钥 .

    对于 Could not open a connection to your authentication agent ,当使用 ssh-add 时,
    检查:https://stackoverflow.com/a/17695338/1760313

  • 2

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

    Step 1: ssh keys
    创建'll need. In this example I'已将我命名为default / original 'id_rsa'(默认值)和我的新密码'id_rsa-work'的任何密钥对:

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

    Step 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
    

    Step 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
    

    Step 4: test
    为了测试你是否正确完成了这一切,我建议进行以下快速检查:

    $ 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) . 但是现在你应该好好去! :)

  • 154

    假设 alice 是github.com用户,拥有2个或更多私有存储库 repoN . 对于此示例,我们将仅使用名为 repo1repo2 的两个存储库

    https://github.com/alice/repo1

    https://github.com/alice/repo2

    您需要从这些存储库中提取,而无需在服务器或多个服务器上输入密码 . 例如,您想要执行 git pull origin master ,并且您希望在不要求密码的情况下执行此操作 .

    你现在没有发现 ~/.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 的url是这样的:

    [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个钥匙和回购 . 您将拥有更多要处理的密钥,但如果受到攻击,您将不那么容易受到攻击 .

  • 1

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

    • 创建2对rsa密钥,通过 ssh-keygen ,正确命名,以便让生活更轻松 .

    • 通过 ssh-add path_to_private_key 将私钥添加到本地代理

    • 对于每个github帐户,上传(不同的)公钥 .


    配置

    ~/.ssh/config

    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
    

    Set remote url for repo:

    • 对于主机 github-kc 中的仓库: git remote set-url origin git@github-kc:kuchaguangjie/pygtrans.git

    • 对于Host github-abc 中的repo:
      git remote set-url origin git@github-abc:abcdefg/yyy.git


    解释

    Options in ~/.ssh/config:

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

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

    • [Following are sub options of Host]

    • Hostname
      指定实际的主机名,只需使用 github.com 表示github,

    • User git
      对于github,用户总是 git

    • IdentityFile
      指定要使用的密钥,只需将路径设为公钥,

    • LogLevel
      指定要调试的日志级别,如果有任何问题, DEBUG3 会提供最详细的信息 .


  • 37

    在我的情况下,上述解决方案都没有解决我的问题,但ssh-agent确实如此 . 基本上,我做了以下事情:

    • 使用如下所示的ssh-keygen生成密钥对 . 它将生成一个密钥对(在此示例中为 .\keyfile.\keyfile.pub

    ssh-keygen -t rsa -b 4096 -C "yourname@yourdomain" -f keyfile

    • keyfile.pub 上传到git提供程序

    • 在您的机器上启动ssh-agent(您可以使用 ps -ef | grep ssh-agent 查看它是否已在运行)

    • 运行 ssh-add .\keyfile 以添加凭据

    • 现在你可以运行 git clone git@provider:username/project.git

  • 1

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

    • 使用 ssh-keygen -t rsa 创建新的身份文件 . 给它一个像 proj1.id_rsa 的替代品,毫无疑问,因为你不需要密码短语 .

    • .ssh/config 中添加新部分:

    Host proj1.github.com HostName 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 clone git@proj1.github.com .

    A good tutorial.

    不要搞砸主人

  • 17

    我用了,

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

    它很好 .

    在.ssh / config文件中使用上述设置,以获取不同用户名的不同rsa密钥 .

  • 1

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

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

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

  • 0

    我使用shell脚本将我切换到我想要“活跃”的任何帐户 . 基本上,您从一个新的开始,正确配置一个帐户并正常工作,然后将这些文件移动到具有正确前缀的名称 . 从那时起,您可以使用命令“github”或“gitxyz”来切换:

    # my github script
    cd ~/.ssh
    rm id_rsa
    rm id_rsa.pub
    rm config
    
    ln git_dhoerl id_rsa
    ln git_dhoerl.pub id_rsa.pub
    ln config_dhoerl config
    
    git config --global user.email "dhoerl@xyz.com"
    git config --global github.user "dhoerl"        
    # git config --global github.token "whatever_it_is" # now unused
    
    ssh-add -D
    

    我对此很幸运 . 我还在Xcode(为你的Mac用户)创建了一个运行脚本,所以它不会构建我的项目,除非我有正确的设置(因为它使用git):

    在依赖项之后运行脚本:

    #! /bin/ksh
    if [ "$(git config --global --get user.email)" != "dhoerl@<company>.com" ]
    then
        exit 1
    fi
    
  • 2

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

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

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

    并确保通过运行以下命令将您的密钥添加到ssh-agent:

    ssh-add ~/.ssh/id_rsa_anotheraccount
    

    请记住,上面的命令只会为当前会话的ssh-agent添加密钥 . 如果您希望这个工作,您必须"permanently"将它添加到您的ssh-agent . 例如,这里是如何为ubuntuOSX这样做的 .

    还应该可以使用全局git配置和条件包括将此方法扩展到多个存储库(请参阅示例) .

相关问题