git - 多个 github 帐户 : what values for Host in . ssh/config?

标签 git github ssh ssh-keys

我试图了解 .ssh/config 和 .git/config 下的配置如何交互。

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

这意味着对于这些 github 帐户中的每一个,我都必须在 ~/.ssh/config 中有一个相应的部分,指定要用于它的 ssh key 文件。 (例如,GH0 部分将包含类似 IdentityFile ~/.ssh/id_rsa.GH0 等内容)

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

what do I have to put as the argument to the Host keyword in ~/.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 中的前缀来自???

Where do I find the corresponding ones for my github accounts?

我发现的一些信息让人猜测 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 .. .] 部分。 git-remote 的手册页也没有关于此字段的文档。

最佳答案

执行以下操作:

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

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

关于git - 多个 github 帐户 : what values for Host in . ssh/config?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14689788/

相关文章:

git - 自动镜像 GitHub 存储库

ruby-on-rails - 乱序 merge ActiveRecord 迁移

git - .gitignore 和 git rm --cached 不会阻止文件被跟踪

bash - 使用 ssh 手动加载私钥

ssh - 如何使 SSH RSA key 自动接受?

git - 从另一个分支获取文件的内容而不检查它

git - 将远程分支 pull 入具有不同名称的本地仓库?

github - 有没有办法查看 gist 上托管的代码示例的实时演示?

Github API-作者提交

linux - 通过 SSH 在远程 shell 脚本执行中分配变量