当前位置:   article > 正文

pi3 无法登陆ssh的问题解决_key_load_private: invalid format key_load_public:

key_load_private: invalid format key_load_public: invalid format could not l

1.问题现象

       刚烧完树莓派的最新版镜像“2017-09-07-raspbian-stretch.img”,但由于没有hdmi的显示器,只能在tf卡的根目录增加一个ssh的文件用于使能树莓派的ssh登陆。可是当我给树莓派供电并接上网线,在secureCRT登陆ssh却提示出错”connection closed“,试过很多次都一样。以前用这个方法是能够正常登陆ssh的。


2.问题分析

       不能登陆ssh又没有显示器,那只能接串口来登陆树莓派,看看问题现象。pi3的串口登陆默认是禁止的,需要在tf卡里的config文件里修改配置才能登陆。

修改方法:

/boot/config.txt 文件中添加如下两行,可以解决上述问题。

  1. dtoverlay=pi3-miniuart-bt
  2. core_freq=250

登陆串口后查询日志文件会发现很多关于ssh的错误信息,查询命令用

cat /var/log/auth.log

会提示一大堆错误信息

  1. Oct 10 07:44:27 raspberrypi sshd[757]: error: key_load_public: invalid format
  2. Oct 10 07:44:27 raspberrypi sshd[757]: error: Could not load host key: /etc/ssh/ssh_host_ecdsa_key
  3. Oct 10 07:44:27 raspberrypi sshd[757]: error: key_load_public: invalid format
  4. Oct 10 07:44:27 raspberrypi sshd[757]: error: Could not load host key: /etc/ssh/ssh_host_ed25519_key
  5. Oct 10 07:44:27 raspberrypi sshd[757]: fatal: No supported key exchange algorithms [preauth]
  6. Oct 10 07:44:32 raspberrypi sshd[759]: error: key_load_public: invalid format
  7. Oct 10 07:44:32 raspberrypi sshd[759]: error: Could not load host key: /etc/ssh/ssh_host_rsa_key
  8. Oct 10 07:44:32 raspberrypi sshd[759]: error: key_load_public: invalid format
  9. Oct 10 07:44:32 raspberrypi sshd[759]: error: Could not load host key: /etc/ssh/ssh_host_ecdsa_key
  10. Oct 10 07:44:32 raspberrypi sshd[759]: error: key_load_public: invalid format
  11. Oct 10 07:44:32 raspberrypi sshd[759]: error: Could not load host key: /etc/ssh/ssh_host_ed25519_key
  12. Oct 10 07:44:32 raspberrypi sshd[759]: fatal: No supported key exchange algorithms [preauth]
  13. Oct 10 07:44:37 raspberrypi sshd[761]: error: key_load_public: invalid format
  14. Oct 10 07:44:37 raspberrypi sshd[761]: error: Could not load host key: /etc/ssh/ssh_host_rsa_key
  15. Oct 10 07:44:37 raspberrypi sshd[761]: error: key_load_public: invalid format
  16. Oct 10 07:44:37 raspberrypi sshd[761]: error: Could not load host key: /etc/ssh/ssh_host_ecdsa_key
  17. Oct 10 07:44:37 raspberrypi sshd[761]: error: key_load_public: invalid format
大致意思是无法加载密钥。

用另一个命令也可以查看ssh的错误信息”sudo dpkg-reconfigure openssh-server”

  1. pi@raspberrypi:~$ sudo dpkg-reconfigure openssh-server
  2. ^C
  3. pi@raspberrypi:~$ sudo systemctl status ssh -l
  4. ● ssh.service - OpenBSD Secure Shell server
  5. Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enab
  6. Active: active (running) since Tue 2017-10-10 08:26:59 UTC; 9min ago
  7. Main PID: 1441 (sshd)
  8. CGroup: /system.slice/ssh.service
  9. └─1441 /usr/sbin/sshd -D
  10. Oct 10 08:26:59 raspberrypi systemd[1]: Starting OpenBSD Secure Shell server...
  11. Oct 10 08:26:59 raspberrypi sshd[1441]: Server listening on 0.0.0.0 port 22.
  12. Oct 10 08:26:59 raspberrypi sshd[1441]: Server listening on :: port 22.
  13. Oct 10 08:26:59 raspberrypi systemd[1]: Started OpenBSD Secure Shell server.
  14. Oct 10 08:26:59 raspberrypi sshd[1401]: pam_unix(sshd:auth): authentication fail
  15. Oct 10 08:27:01 raspberrypi sshd[1401]: Failed password for pi from 192.168.0.14
  16. Oct 10 08:27:06 raspberrypi sshd[1401]: Accepted password for pi from 192.168.0.
  17. Oct 10 08:27:06 raspberrypi sshd[1401]: pam_unix(sshd:session): session opened f
  18. Oct 10 08:27:20 raspberrypi sshd[1487]: Accepted password for pi from 192.168.0.
  19. Oct 10 08:27:20 raspberrypi sshd[1487]: pam_unix(sshd:session): session opened f
  20. lines 1-17/17 (END)
从上述错误信息可以断定ssh可能初始化了一个错误的ssh密钥,导致ssh一直无法认证登陆过程,导致ssh登陆失败。


3.问题解决

         ssh有问题的话是无论如何都登陆不上的,只能重新初始化ssh才可以使用。

用下面的命令可以重新初始化ssh

  1. sudo rm /etc/ssh/ssh_host*
  2. sudo dpkg-reconfigure openssh-server
会提示下面的信息代表重新生成ssh的密钥

  1. Creating SSH2 RSA key; this may take some time ...
  2. 2048 SHA256:GibJstg/cJs+oRiJ8gfJoPD4x7Pbgq6a0P/kXMtFj9Y root@raspberrypi (RSA)
  3. Creating SSH2 ECDSA key; this may take some time ...
  4. 256 SHA256:PpGJBv3AZKSJ9vx0sBGkbKtTsmX6SrvupVaJCM8LCbY root@raspberrypi (ECDSA)
  5. Creating SSH2 ED25519 key; this may take some time ...
  6. 256 SHA256:tvhTODPogtst1CM39/LXgp3coPuKalQVGsJOGUSq2Ak root@raspberrypi (ED25519)
至此ssh的问题解决了,利用网络可以登陆ssh了。



参考资料:https://www.raspberrypi.org/forums/viewtopic.php?t=172911

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/185249
推荐阅读
相关标签
  

闽ICP备14008679号