ssh -T git@github.com 出现 ssh_exchange_identification: Connection closed by remote host 的错误

2016-07-06 07:34:03 +08:00
 DaCong

本人是在 win10 系统下使用在 https://git-scm.com/download/win 下载的 git 命令行。 在使用 ssh -vT git@github.com 后,输出如下:

$ ssh git@github.com -vT
OpenSSH_7.1p2, OpenSSL 1.0.2h  3 May 2016
debug1: Reading configuration data /c/Users/DaCong/.ssh/config
debug1: /c/Users/DaCong/.ssh/config line 1: Applying options for github.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to ssh.github.com [182.239.95.136] port 443.
debug1: Connection established.
debug1: identity file /c/Users/DaCong/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/DaCong/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/DaCong/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/DaCong/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/DaCong/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/DaCong/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/DaCong/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/DaCong/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.1
ssh_exchange_identification: Connection closed by remote host

从 debug 的结果来看,应该是没有找到 key 文件,可是,我在 ~/.ssh/ 下执行 ls -la 之后,输出如下:

total 34
drwxr-xr-x 1 DaCong 197609    0 7 月   5 09:47 ./
drwxr-xr-x 1 DaCong 197609    0 7 月   5 23:10 ../
-rw-r--r-- 1 DaCong 197609   53 7 月   5 08:43 config
-rw-r--r-- 1 DaCong 197609 1679 7 月   5 09:19 id_rsa
-rw-r--r-- 1 DaCong 197609  392 7 月   5 09:19 id_rsa.pub

应该说我是有密钥文件的,而且也有读写权限,为什么就成了找不到 key 了呢? 因为这个问题,我提交文件的时候用的是 https , clone 也是,每次更新 hexo 博客都要输入密码,非常麻烦。 还有,我已经在 github 中提交了 id_rsa.pub 。 我已经在 google 上搜索过了,相似的问题的解决方案并没有起作用,所以来 V2EX 提问,希望能够解决问题。

18902 次点击
所在节点    git
17 条回复
xing393939
2016-07-06 07:40:56 +08:00
看下你 windows 的 c 盘有 c:/Users/DaCong/.ssh/吗,另外 你那 debug 信息好像还在找 https 的证书?
aec4d
2016-07-06 08:02:01 +08:00
并不是没有找到该文件 identity file /c/Users/DaCong/.ssh/id_rsa type 1
你可以执行 ssh -T git@github.com -vvv 查看更详细的信息
同时贴出你的 config 文件看看
最后你连接的是 ssh.github.com:443 这果断是不科学的
aec4d
2016-07-06 08:13:14 +08:00
另外 你上面显示连接的 ip 是中国移动的 ip.......
如果本来就被中间人 连不上也是情理之中的了
Citrus
2016-07-06 08:36:50 +08:00
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to ssh.github.com [182.239.95.136] port 443.

你在逗我么?写了配置强制 ssh 去连 HTTPS 端口???
DaCong
2016-07-06 09:30:32 +08:00
@xing393939
@aec4d
@Citrus
感谢回复,请看附言,谢谢
cielpy
2016-07-06 09:43:37 +08:00
@Citrus 这样写有什么问题吗
yangtukun1412
2016-07-06 09:44:24 +08:00
config 里加上 IdentityFile ~/.ssh/id_rsa ,另外文件权限应该要 600 才可以。
aec4d
2016-07-06 09:54:20 +08:00
@DaCong 是因为你用的网络被中间人了 你可以查那个 IP 是移动的不是 github 的 当然会握手协商失败 挂代理吧:D
或者你试一下添加 hosts
192.30.253.112 github.com
aec4d
2016-07-06 09:56:00 +08:00
ssh -T git@192.30.253.112 -vvv 直接这样测试
lonelinsky
2016-07-06 10:07:59 +08:00
~/.ssh/id_rsa 的权限太开放了吧,改为 400 或者 600 试试…
Citrus
2016-07-06 10:15:00 +08:00
@DaCong
@cielpy 我的错,没注意 GitHub 支持 SSH over HTTPS ,这样写没问题。
不过
@DaCong 你的网络明显是被劫持了, 182.239.95.136 并不是 GitHub 的 IP ,而是香港中国移动的反代服务器,所以导致这个问题。其实没有特殊情况的话,没必要 SSH over HTTPS ,直接用 SSH 不就可以了?
raysonx
2016-07-06 10:15:57 +08:00
@aec4d 正解。
根据楼主的日志, github.com 解析到了 182.239.95.136 ,这个 IP 是中国移动的 SNI 缓存服务器:

IPIP.net 数据
您输入的 IP: 182.239.95.136
中国香港 移动

结论就是,楼主的运营商(移动)使用 DNS 劫持将 github.com 劫持到了移动的缓存服务器。
skydiver
2016-07-06 10:16:32 +08:00
key 权限必须是 600
DaCong
2016-07-06 10:17:58 +08:00
@aec4d edit the hosts file can work! Thx
lslqtz
2016-07-06 14:43:05 +08:00
又得到了个新缓存服务器。
tomorrowin
2018-03-18 22:26:08 +08:00
THIS post helped me out !!!!thank you sooooooooooo much!!!!!
shuirong1997
2020-01-20 22:12:50 +08:00
help me too, thank you, guys.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/290545

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX