ssh连不上服务器问题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/u013560932/article/details/78435003

git pull时git clone时或putty ssh连不上服务器时

如果报错(git 报错):
ssh_exchange_identification: read: Connection reset by peer
fatal: Could not read from remote repository.
这种情况就需要你去排错了

首先ssh -v
看看能不能获得什么有用的信息
或者物理登录服务器,service sshd status
我的服务器报错 Could not load host key: /etc/ssh/ssh_ed25519_key
这就是ssh登录不上的原因了。
这种错误有两种情况:

  1. host key莫名奇妙没了,或者长度为0(可能是因为病毒)
  2. (这个就是我为什么登不上的原因)新版的opensshd 中添加了Ed25519 做签名验证,而之前系统里没这个算法的证书(好像是因为重启服务器的问题)
  3. host key不知道为啥没权限访问了,导致ssh失败(多半是因为777权限了,SSH 服务会对相关密钥文件的权限进行检查。比如,私钥文件默认权限是 600,如果配置成 777 等其它权限,导致其它用户也有读取或修改权限。则 SSH 服务会认为该配置存在安全风险,进而导致客户端连接失败。)

最简单的解决办法(缺少什么key就重新生成一遍):

#ed25519_key
ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key  
#rsa_key
ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key

直接重置host key,然后重启ssh:

service sshd restart

前2个问题都能解决

第三个问题的解决办法:

cd /etc/ssh/
chmod 600 ssh_host_*
chmod 644 *.pub

操作示意如下:

[root@centos]# cd /etc/ssh/
[root@centos]# chmod 600 ssh_host_*
[root@centos]# chmod 644 *.pub
[root@centos]# ll
total 156
-rw-------. 1 root root 125811 Nov 23  2013 moduli
-rw-r--r--. 1 root root   2047 Nov 23  2013 ssh_config
-rw-------  1 root root   3639 May 16 11:43 sshd_config
-rw-------  1 root root    668 May 20 23:31 ssh_host_dsa_key
-rw-r--r--  1 root root    590 May 20 23:31 ssh_host_dsa_key.pub
-rw-------  1 root root    963 May 20 23:31 ssh_host_key
-rw-r--r--  1 root root    627 May 20 23:31 ssh_host_key.pub
-rw-------  1 root root   1675 May 20 23:31 ssh_host_rsa_key
-rw-r--r--  1 root root    382 May 20 23:31 ssh_host_rsa_key.pub

检查文件有效性

如果参阅前述步骤,修改相关文件权限后,还是无法正常连接。由于 SSH 服务在启动时会自动重建丢失的密钥文件。所以,也可以直接将相关文件删除并重启 SSH 服务,让相关文件自动生成。

相关指令如下:

cd /etc/ssh/
rm -rf ssh_host_*
service sshd restart

操作示意如下:


[root@centos]# cd /etc/ssh/
[root@centos]# ll
total 156
-rw-------. 1 root root 125811 Nov 23  2013 moduli
-rw-r--r--. 1 root root   2047 Nov 23  2013 ssh_config
-rw-------  1 root root   3639 May 16 11:43 sshd_config
-rw-------  1 root root    672 May 20 23:08 ssh_host_dsa_key
-rw-r--r--  1 root root    590 May 20 23:08 ssh_host_dsa_key.pub
-rw-------  1 root root    963 May 20 23:08 ssh_host_key
-rw-r--r--  1 root root    627 May 20 23:08 ssh_host_key.pub
-rw-------  1 root root   1675 May 20 23:08 ssh_host_rsa_key
-rw-r--r--  1 root root    382 May 20 23:08 ssh_host_rsa_key.pub
[root@centos]# rm -rf ssh_host_*
[root@centos]# ll
total 132
-rw-------. 1 root root 125811 Nov 23  2013 moduli
-rw-r--r--. 1 root root   2047 Nov 23  2013 ssh_config
-rw-------  1 root root   3639 May 16 11:43 sshd_config
[root@centos]# service sshd restart
Stopping sshd:                                             [  OK  ]
Generating SSH1 RSA host key:                              [  OK  ]
Generating SSH2 RSA host key:                              [  OK  ]
Generating SSH2 DSA host key:                              [  OK  ]
Starting sshd:                                             [  OK  ]
[root@centos]# ll
total 156
-rw-------. 1 root root 125811 Nov 23  2013 moduli
-rw-r--r--. 1 root root   2047 Nov 23  2013 ssh_config
-rw-------  1 root root   3639 May 16 11:43 sshd_config
-rw-------  1 root root    668 May 20 23:16 ssh_host_dsa_key
-rw-r--r--  1 root root    590 May 20 23:16 ssh_host_dsa_key.pub
-rw-------  1 root root    963 May 20 23:16 ssh_host_key
-rw-r--r--  1 root root    627 May 20 23:16 ssh_host_key.pub
-rw-------  1 root root   1671 May 20 23:16 ssh_host_rsa_key
-rw-r--r--  1 root root    382 May 20 23:16 ssh_host_rsa_key.pub

补充说明:对于 Ubuntu 、Debain 类操作系统,修复指令如下:

sudo rm -r /etc/ssh/ssh*key
sudo dpkg-reconfigure openssh-server

还有一种错误不是Could not load host key:….的问题
而是错误次数太多被列入黑名单了
需要检查hosts.allow和hosts.deny
这里两个文件在/etc/下。

参考文章:

SSH所有问题排查详细方法:https://help.aliyun.com/knowledge_detail/52874.html
重置host key:http://blog.csdn.net/hyholine/article/details/7362073
hosts.allow和hosts.deny的问题:https://help.aliyun.com/knowledge_detail/41485.html
host_key权限问题:https://help.aliyun.com/knowledge_detail/41486.html
ed25519出错:http://www.linuxbyte.org/2014/943.html
ed25519是什么:http://ed25519.cr.yp.to/

猜你喜欢

转载自blog.csdn.net/u013560932/article/details/78435003