Mac:如果不改变某些东西,SSH就不再起作用了

时间:2017-11-12 14:22:32

标签: git macos github ssh

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

三天前我设置了一个SSH密钥,用于在SSH上使用git。一切都很好,我能够把所有东西推到GitHub。从昨天开始,我不能再这样做了。我总是得到同样的错误(如下所示)。 SSH-key是ssh-agent的一部分,config-files没有改变,甚至无法通过SSH到GitHub进行正常连接。尝试了其他项目和其他回购,并得到了同样的错误。我没有在最后几天更新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

3 个答案:

答案 0 :(得分:3)

debug1: Connecting to github.com [192.30.253.113] port 443.
...
ssh_exchange_identification: Connection closed by remote host

“ssh_exchange_identification:远程主机关闭的连接”表示远程服务器在接受后立即故意关闭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的问题。

答案 1 :(得分:1)

您必须首先确保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 :(得分:0)

为所有人寻找解决方案: 我做了以下几次然后它起作用了:

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再次工作。不知道为什么这个解决方案以前没有用过。

无论如何,感谢所有回复。