多个github帐户:.ssh / config中Host的值是多少?

时间:2013-02-04 15:21:01

标签: git github ssh ssh-keys

我正在尝试了解.ssh / config和.git / config下的配置如何互动。

情况就是这样:我有两个单独的github帐户,让我们称它们为GH0和GH1,我想与“无密码”进行交互,即使用~/.ssh/id_rsa.GH0.pub~/.ssh/id_rsa.GH1.pub中的ssh密钥。目前这适用于GH0但不适用于GH1。 (例如,push命令GH1死于ERROR: Repository not found.\nfatal: The remote end hung unexpectedly.; ssh -T git@github.com有效,但只是因为它连接了GH0。)

这意味着对于每个github帐户,我必须在〜/ .ssh / config中有相应的部分,指定要使用哪个ssh密钥文件。 (例如,GH0的部分将具有类似IdentityFile ~/.ssh/id_rsa.GH0等的内容)

问题是:我还需要在这些部分中添加什么?更具体地说,

  

我需要将Host中的~/.ssh/config关键字作为参数?

到目前为止,我发现的信息对我来说毫无意义。在某些例子中,我看到像

这样的东西
Host github.com
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_rsa

Host ac2.github.com
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_rsa_ac2

“ac2”在哪里。第二个Host中的前缀来自???

  

我在哪里可以找到我的github帐户的相应内容?

我发现的一些信息导致人们猜测Host关键字的参数实际上是任意的,暗示以下情况也会很好

Host omgwtfbbq
    Hostname github.com
    User git
    IdentityFile ~/.ssh/GH0

Host 4.8.15.16.23.42
    Hostname github.com
    User git
    IdentityFile ~/.ssh/GH1

但如果是这样,这就提出了另一个问题: git(或github)如何知道这两个部分中的哪一部分用于任何给定的命令?

同样,我猜测 1 这将在项目的.git/config文件中,在某些[remote ...]部分下指定,但是如何?< / p>

1 我必须求助于猜测,因为一方面,我无法找到解释.git / config中键值对的文档。我找到的最接近的是git-config的手册页,但我在url = ...部分的[remote ...]字段的页面中找不到任何文档。 git-remote的手册页也没有关于此字段的文档。

2 个答案:

答案 0 :(得分:6)

执行以下操作:

$ git remote add omgwtfbbq git@omgwtfbbq:user/repo.git
$ git pull omgwtfbbq master

这将使用您在Host中定义的~/.ssh/config别名作为git主机,因此将使用您已设置的IdentityFile。

答案 1 :(得分:1)

是的,.ssh / config文件中的Host行是别名。您使用它来代替github.com,任何正确使用SSH的东西都会读取文件并交换真实的主机名,并且如果已经给出了密码文件和用户名,也可以使用它们。