Linux系统入门学习:如何修复“sshd error: could not load host key”

Linux系统入门学习:如何修复“sshd error: could not load host key”

[日期:2014-11-14] 来源:Linux中国  作者:Linux [字体:  ]
 

问题:当我尝试SSH到一台远程服务器时,SSH客户端登陆失败并提示“Connection closed by X.X.X.X”。在SSH服务器那端,我看到这样的错误消息:“sshd error: could not load host key.”。这发生了什么问题,我怎样才能修复该错误?

该SSH连接错误的详细症状如下。

SSH客户端方面:当你尝试SSH到一台远程主机时,你没有看见登录屏幕,你的SSH连接就立即关闭,并提示此消息:“Connection closed by X.X.X.X”。

SSH服务器方面:在系统日志中,你看到如下错误消息(如,在Debian/Ubuntu上,/var/log/auth.log)。

  1. Oct1608:59:45 openstack sshd[1214]: error:Couldnot load host key:/etc/ssh/ssh_host_rsa_key
  2. Oct1608:59:45 openstack sshd[1214]: error:Couldnot load host key:/etc/ssh/ssh_host_dsa_key
  3. Oct1608:59:45 openstack sshd[1214]: error:Couldnot load host key:/etc/ssh/ssh_host_ecdsa_key
  4. Oct1608:59:45 openstack sshd[1214]: fatal:No supported key exchange algorithms [preauth]

导致该问题的根源是,sshd守护进程不知怎么地不能加载SSH主机密钥了。

当OpenSSH服务器第一次安装到Linux系统时,SSH主机密钥应该会自动生成以供后续使用。如果,不管怎样,密钥生成过程没有成功完成,那就会导致这样的SSH登录问题。

让我们检查能否在相应的地方找到SSH主机密钥。

  1. $ ls -al /etc/ssh/ssh*key

如果SSH主机密钥在那里找不到,或者它们的大小被截断成为0(就像上面那样),你需要从头开始重新生成主机密钥。

重新生成SSH主机密钥

在Debian、Ubuntu或其衍生版上,你可以使用dpkg-reconfigure工具来重新生成SSH主机密钥,过程如下:

  1. $ sudo rm -/etc/ssh/ssh*key
  2. $ sudo dpkg-reconfigure openssh-server

CentOS、RHEL或Fedora上,你所要做的是,删除现存(有问题的)密钥,然后重启sshd服务。

  1. $ sudo rm -/etc/ssh/ssh*key
  2. $ sudo systemctl restart sshd

另外一个重新生成SSH主机密钥的方式是,使用ssh-keygen命令来手动生成。

  1. $ sudo ssh-keygen -t rsa -/etc/ssh/ssh_host_rsa_key
  2. $ sudo ssh-keygen -t dsa -/etc/ssh/ssh_host_dsa_key
  3. $ sudo ssh-keygen -t ecdsa -/etc/ssh/ssh_host_ecdsa_key

在生成新的SSH主机密钥后,确保它们能在/etc/ssh目录中找到。此时,不必重启sshd服务。

  1. $ ls -al /etc/ssh/ssh*key

现在,再试试SSH到SSH服务器吧,看看问题是否已经离你而去了。

CentOS 6.0下SSH免密码登录配置 http://www.linuxidc.com/Linux/2013-03/80488.htm

提高Ubuntu的SSH登陆认证速度的办法 http://www.linuxidc.com/Linux/2014-09/106810.htm

开启SSH服务让Android手机远程访问 Ubuntu 14.04  http://www.linuxidc.com/Linux/2014-09/106809.htm

如何为Linux系统中的SSH添加双重认证 http://www.linuxidc.com/Linux/2014-08/105998.htm

在 Linux 中为非 SSH 用户配置 SFTP 环境 http://www.linuxidc.com/Linux/2014-08/105865.htm

Linux 上SSH 服务的配置和管理 http://www.linuxidc.com/Linux/2014-06/103627.htm

SSH入门学习基础教程 http://www.linuxidc.com/Linux/2014-06/103008.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2014-11/109405.htm

linux
原文地址:https://www.cnblogs.com/sunnyside/p/12809184.html