赞
踩
1.问题现象
刚烧完树莓派的最新版镜像“2017-09-07-raspbian-stretch.img”,但由于没有hdmi的显示器,只能在tf卡的根目录增加一个ssh的文件用于使能树莓派的ssh登陆。可是当我给树莓派供电并接上网线,在secureCRT登陆ssh却提示出错”connection closed“,试过很多次都一样。以前用这个方法是能够正常登陆ssh的。
2.问题分析
不能登陆ssh又没有显示器,那只能接串口来登陆树莓派,看看问题现象。pi3的串口登陆默认是禁止的,需要在tf卡里的config文件里修改配置才能登陆。
修改方法:
在 /boot/config.txt 文件中添加如下两行,可以解决上述问题。
- dtoverlay=pi3-miniuart-bt
- core_freq=250
cat /var/log/auth.log
- Oct 10 07:44:27 raspberrypi sshd[757]: error: key_load_public: invalid format
- Oct 10 07:44:27 raspberrypi sshd[757]: error: Could not load host key: /etc/ssh/ssh_host_ecdsa_key
- Oct 10 07:44:27 raspberrypi sshd[757]: error: key_load_public: invalid format
- Oct 10 07:44:27 raspberrypi sshd[757]: error: Could not load host key: /etc/ssh/ssh_host_ed25519_key
- Oct 10 07:44:27 raspberrypi sshd[757]: fatal: No supported key exchange algorithms [preauth]
- Oct 10 07:44:32 raspberrypi sshd[759]: error: key_load_public: invalid format
- Oct 10 07:44:32 raspberrypi sshd[759]: error: Could not load host key: /etc/ssh/ssh_host_rsa_key
- Oct 10 07:44:32 raspberrypi sshd[759]: error: key_load_public: invalid format
- Oct 10 07:44:32 raspberrypi sshd[759]: error: Could not load host key: /etc/ssh/ssh_host_ecdsa_key
- Oct 10 07:44:32 raspberrypi sshd[759]: error: key_load_public: invalid format
- Oct 10 07:44:32 raspberrypi sshd[759]: error: Could not load host key: /etc/ssh/ssh_host_ed25519_key
- Oct 10 07:44:32 raspberrypi sshd[759]: fatal: No supported key exchange algorithms [preauth]
- Oct 10 07:44:37 raspberrypi sshd[761]: error: key_load_public: invalid format
- Oct 10 07:44:37 raspberrypi sshd[761]: error: Could not load host key: /etc/ssh/ssh_host_rsa_key
- Oct 10 07:44:37 raspberrypi sshd[761]: error: key_load_public: invalid format
- Oct 10 07:44:37 raspberrypi sshd[761]: error: Could not load host key: /etc/ssh/ssh_host_ecdsa_key
- Oct 10 07:44:37 raspberrypi sshd[761]: error: key_load_public: invalid format
大致意思是无法加载密钥。
用另一个命令也可以查看ssh的错误信息”sudo dpkg-reconfigure openssh-server”
- pi@raspberrypi:~$ sudo dpkg-reconfigure openssh-server
- ^C
- pi@raspberrypi:~$ sudo systemctl status ssh -l
- ● ssh.service - OpenBSD Secure Shell server
- Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enab
- Active: active (running) since Tue 2017-10-10 08:26:59 UTC; 9min ago
- Main PID: 1441 (sshd)
- CGroup: /system.slice/ssh.service
- └─1441 /usr/sbin/sshd -D
-
- Oct 10 08:26:59 raspberrypi systemd[1]: Starting OpenBSD Secure Shell server...
- Oct 10 08:26:59 raspberrypi sshd[1441]: Server listening on 0.0.0.0 port 22.
- Oct 10 08:26:59 raspberrypi sshd[1441]: Server listening on :: port 22.
- Oct 10 08:26:59 raspberrypi systemd[1]: Started OpenBSD Secure Shell server.
- Oct 10 08:26:59 raspberrypi sshd[1401]: pam_unix(sshd:auth): authentication fail
- Oct 10 08:27:01 raspberrypi sshd[1401]: Failed password for pi from 192.168.0.14
- Oct 10 08:27:06 raspberrypi sshd[1401]: Accepted password for pi from 192.168.0.
- Oct 10 08:27:06 raspberrypi sshd[1401]: pam_unix(sshd:session): session opened f
- Oct 10 08:27:20 raspberrypi sshd[1487]: Accepted password for pi from 192.168.0.
- Oct 10 08:27:20 raspberrypi sshd[1487]: pam_unix(sshd:session): session opened f
- lines 1-17/17 (END)
从上述错误信息可以断定ssh可能初始化了一个错误的ssh密钥,导致ssh一直无法认证登陆过程,导致ssh登陆失败。
3.问题解决
ssh有问题的话是无论如何都登陆不上的,只能重新初始化ssh才可以使用。
用下面的命令可以重新初始化ssh
- sudo rm /etc/ssh/ssh_host*
- sudo dpkg-reconfigure openssh-server
会提示下面的信息代表重新生成ssh的密钥
- Creating SSH2 RSA key; this may take some time ...
- 2048 SHA256:GibJstg/cJs+oRiJ8gfJoPD4x7Pbgq6a0P/kXMtFj9Y root@raspberrypi (RSA)
- Creating SSH2 ECDSA key; this may take some time ...
- 256 SHA256:PpGJBv3AZKSJ9vx0sBGkbKtTsmX6SrvupVaJCM8LCbY root@raspberrypi (ECDSA)
- Creating SSH2 ED25519 key; this may take some time ...
- 256 SHA256:tvhTODPogtst1CM39/LXgp3coPuKalQVGsJOGUSq2Ak root@raspberrypi (ED25519)
至此ssh的问题解决了,利用网络可以登陆ssh了。
参考资料:https://www.raspberrypi.org/forums/viewtopic.php?t=172911
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。