【问题标题】:Mac: SSH doesn't work anymore without changing somethingMac:SSH 不再工作而无需更改某些内容
【发布时间】:2018-04-25 06:49:45
【问题描述】:

在网上搜索了一天没有好的解决方案后,我在这里告诉你我的问题。

三天前我设置了一个 SSH-key 来使用 git 和 SSH。一切正常,我能够将所有内容推送和拉取到 GitHub。从昨天开始,我不能再这样做了。我总是得到同样的错误(如下所示)。 SSH-key 是 ssh-agent 的一部分,配置文件没有更改,甚至无法通过 SSH 正常连接到 GitHub。也尝试了其他项目和其他 repos 并得到了同样的错误。最近几天我没有更新 Sierra 或其他内容,所以这也不是原因。

终端输入和输出:

User-MBP:data-analysis user$ git push -v origin master
Pushing to git@github.com:Username/data-analysis.git
ssh_exchange_identification: Connection closed by remote host
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

普通 SSH 连接:

User-MBP:data-analysis user$ ssh -v git@github.com
OpenSSH_7.4p1, LibreSSL 2.5.0
debug1: Reading configuration data /Users/user/.ssh/config
debug1: /Users/user/.ssh/config line 1: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [192.30.253.113] port 443.
debug1: Connection established.
debug1: identity file /Users/user/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/user/.ssh/id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
ssh_exchange_identification: Connection closed by remote host

感谢您的帮助!

更新

将端口改回 22 以获得以下内容:

User-MBP:data-analysis user$ git push origin master
ssh: connect to host github.com port 22: Connection refused
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

User-MBP:data-analysis user$ ssh -v git@github.com
OpenSSH_7.4p1, LibreSSL 2.5.0
debug1: Reading configuration data /Users/user/.ssh/config
debug1: /Users/user/.ssh/config line 1: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [192.30.253.113] port 22.
debug1: connect to address 192.30.253.113 port 22: Connection refused
debug1: Connecting to github.com [192.30.253.112] port 22.
debug1: connect to address 192.30.253.112 port 22: Connection refused
ssh: connect to host github.com port 22: Connection refused

【问题讨论】:

标签: git macos github ssh


【解决方案1】:
debug1: Connecting to github.com [192.30.253.113] port 443.
...
ssh_exchange_identification: Connection closed by remote host

“ssh_exchange_identification: Connection closed by remote host”表示远程服务器在接受TCP连接后故意关闭它。尚未进行密钥交换或身份验证尝试。

您正在连接到端口 443 (HTTPS) 而不是端口 22 (SSH)。我尝试时遇到同样的错误:

$ ssh -p 443 git@github.com
ssh_exchange_identification: Connection closed by remote host
$ ssh  git@github.com
Permission denied (publickey).

连接到端口 443 会产生与您得到的相同的错误。连接到正确的 ssh 端口至少会尝试进行身份验证(因为我没有在 github 上设置而失败)。

显然 github 不接受端口 443 上的 ssh 连接。改为连接到端口 22。

编辑:

ssh: connect to host github.com port 22: Connection refused

“连接被拒绝”表示您没有与远程服务器建立 TCP 连接。在这种情况下,我的猜测是由于本地网络中的防火墙阻止了传出的 SSH 连接。这可能就是您最初使用端口 443 进行 SSH 的原因。

正如@phd 所指出的,github 支持 ssh 到端口 443,但您必须安排连接到“ssh.github.com”而不仅仅是“github.com”。参考github's documentation

或者,您可以与本地网络管理员讨论允许 ssh 访问 github。

【讨论】:

  • 我读过有关使用 443 而不是 22 的信息。将其改回也不起作用。更新了帖子。
  • 端口 443(除了 22)在不同的主机上打开:ssh.github.com
【解决方案2】:

您必须首先确保ssh -v git@github.com 正常工作,这意味着ssh connection test 以:

Hi username! You've successfully authenticated, 
but GitHub does not provide shell access.

确保您的 /Users/user/.ssh/id_rsa.pub 公共 ssh 密钥已在您的 ssh 设置配置文件中注册。
仔细检查您是否有可能干扰的/Users/user/.ssh/config 文件。
比如this issue mentions:

我遇到了同样的问题,发现我在 ~/.ssh/config 中有一个用于所有连接的设置。我在代理后面,所有出站连接都需要以下内容。完全忘记了这也将应用于本地连接,例如;

Host *
    ProxyCommand ...
    ServerAliveInterval 10

此设置试图使所有连接都通过代理运行 - 这对于本地连接不正确

【讨论】:

  • 但是 2 天前一切正常,三到四次重新启动后它不再工作了。所以我想这不是问题。
  • @Train 然后你的防火墙发生了一些变化:askubuntu.com/a/620527/5470
  • @Train 是的,就像askubuntu.com/a/705500/5470,你可以试试ssh -T -p 443 git@ssh.github.com
【解决方案3】:

对于所有寻找解决方案的人: 我做了以下几次,然后成功了:

User-MacBook-Pro:~ user$ ssh git@github.com
The authenticity of host 'github.com (192.30.253.112)' can't be established.
RSA key fingerprint is  <fingerprint>.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

现在 git 和 ssh 再次工作。不知道为什么这个解决方案以前不起作用。

无论如何感谢所有回复。

【讨论】:

    猜你喜欢
    • 2010-12-31
    • 1970-01-01
    • 2022-06-10
    • 2017-11-22
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多