【问题标题】:Multiple github accounts: what values for Host in .ssh/config?多个 github 帐户:.ssh/config 中 Host 的值是什么?
【发布时间】:2013-01-19 07:58:47
【问题描述】:

我正在尝试了解 .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 等内容)

问题是:我还必须在每个部分中添加什么内容?更具体地说,

我必须在~/.ssh/config 中将什么作为Host 关键字的参数?

到目前为止,我在这方面找到的信息对我来说毫无意义。在某些示例中,我看到类似

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 ...] 部分下指定,但是如何?

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

【问题讨论】:

  • 关于猜测:在 git config 手册中查找“remote..url”。
  • 做一个git remote add omgwtfbbq git@omgwtfbbq:user/repo.gitgit pull omgwtfbbq master,看看是否可行...
  • @HieryNomus:谢谢!请发表您的评论作为答案,以便我选择它
  • 我已将其添加为答案。

标签: git github ssh ssh-keys


【解决方案1】:

是的,.ssh/config 文件中的Host 行是一个别名。您使用它来代替github.com,任何可以正确使用 SSH 的东西都将读取该文件并交换真实的主机名,并且还使用给定的密钥文件和用户名(如果已给出)。

【讨论】:

    【解决方案2】:

    执行以下操作:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-23
      • 2011-03-14
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多