首页 > 解决方案 > mac 超时时 git 的 ssh 配置文件

问题描述

我有 2 个要与 Github 一起使用的帐户,我正在尝试按照本教程将 github 设置为通过 ~/.ssh/config 处理多个帐户。我创建了 2 个 ssh 密钥(例如 rsa_1 和 rsa_2)并像这样设置 ~/.ssh/config 文件:

Host github.com
        HostName github.com
        User username1
        IdentityFile ~/.ssh/id_rsa_1
        IdentitiesOnly yes

Host work.github.com
        HostName github.com
        User username2
        IdentityFile ~/.ssh/id_rsa_2
        IdentitiesOnly yes

然后,我在要与另一个帐户一起使用的 repo 上设置远程 url

git remote set-url origin work.github.com:<shared_gitrepo_name>/project.git

但是当我 git push origin 时,它总是超时

ssh: connect to host work.github.com port 22: Operation timed out
fatal: Could not read from remote repository.

这让我觉得配置文件没有被引用。我不确定我做错了什么。任何帮助,将不胜感激!

标签: gitmacosgithubssh

解决方案


在这两种情况下,您都使用了错误的用户名:

Host github.com
        HostName github.com
        User git <========================== 
        IdentityFile ~/.ssh/id_rsa_1
        IdentitiesOnly yes

Host work.github.com
        HostName github.com
        User git <==========================
        IdentityFile ~/.ssh/id_rsa_2
        IdentitiesOnly yes

git您总是要求以用户“ ”而不是用户“ ”的身份与 github.com 建立 SSH 会话usernamex
您的公钥被复制到 SSH 个人资料页面这一事实username1足以username2 GitHub 知道您是谁。

但是,正如评论中的phd所指出的,超时可能表明存在网络连接问题(例如防火墙阻止了端口 22 上的传出流量)。 即使这样有效,那么错误的用户无论如何都会使 ssh 调用失败。

注意:如讨论中所见,不要使用sudo
Usingsudo手段,只会/etc/shh/config考虑,而不是~/.ssh/config


推荐阅读