CentOS 8 配置 authorized_keys 免密登录后,免密登录失败的排查以及最终的解决

Posted FungLeo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS 8 配置 authorized_keys 免密登录后,免密登录失败的排查以及最终的解决相关的知识,希望对你有一定的参考价值。

CentOS 8 配置 authorized_keys 后,登录失败的排查以及最终的解决

安装好系统后,照例新增了 /root/.ssh/authorized_keys 文件,并将我的公钥粘贴进去,然后就准备免密登录了。但是,告诉我,要输入密码-_-|||

我嘞个娘亲哎,这么多年,第一次遇到这种情况。于是开始排查。首先根据网上的说法,排查了文件的权限,然后发现,木有问题。。

没办法了,我重新配置 /etc/ssh/sshd_config 文件,将日志调整为 DEBUG 模式

LogLevel DEBUG

然后 用 tail -f /var/log/secure 命令,来观察免密登录时,发生了什么。

重启 sshd 服务 systemctl restart sshd

最终追踪到了日志如下:

Mar 24 16:34:08 localhost sshd[1649]: debug1: attempt 0 failures 0 [preauth]
Mar 24 16:34:08 localhost sshd[1649]: debug1: PAM: initializing for "root"
Mar 24 16:34:08 localhost sshd[1649]: debug1: PAM: setting PAM_RHOST to "10.211.55.2"
Mar 24 16:34:08 localhost sshd[1649]: debug1: PAM: setting PAM_TTY to "ssh"
Mar 24 16:34:08 localhost sshd[1649]: debug1: userauth-request for user root service ssh-connection method publickey [preauth]
Mar 24 16:34:08 localhost sshd[1649]: debug1: attempt 1 failures 0 [preauth]
Mar 24 16:34:08 localhost sshd[1649]: debug1: userauth_pubkey: test pkalg rsa-sha2-512 pkblob RSA SHA256:hDgwXsHuuMqbtygHpzMIFwUU/SPtGoQrNfZCnN8Evqo [preauth]
Mar 24 16:34:08 localhost sshd[1649]: debug1: temporarily_use_uid: 0/0 (e=0/0)
Mar 24 16:34:08 localhost sshd[1649]: debug1: trying public key file /root/.ssh/authorized_keys
Mar 24 16:34:08 localhost sshd[1649]: debug1: Could not open authorized keys '/root/.ssh/authorized_keys': Permission denied
Mar 24 16:34:08 localhost sshd[1649]: debug1: restore_uid: 0/0
Mar 24 16:34:08 localhost sshd[1649]: Failed publickey for root from 10.211.55.2 port 63312 ssh2: RSA SHA256:hDgwXsHuuMqbtygHpzMIFwUU/SPtGoQrNfZCnN8Evqo
Mar 24 16:34:15 localhost sshd[1649]: Connection closed by authenticating user root 10.211.55.2 port 63312 [preauth]

重点是 Failed publickey for root from 10.211.55.2 这句,说明我的公钥有误,导致失败。但是,这是我复制粘贴进去的呀,这能有什么错误呢?肉眼是看不出来的,于是换了一个方法。

在我本地运行如下命令:

ssh-copy-id centos

其中 centos 是我为虚拟机命名的别名。


输入虚拟机root 密码后,顺利完成。后续也顺利的免密登录上了虚拟机。

之前一直是复制秘钥,从没用过 ssh-copy-id 这个命令。复制还能出错,我也是日了狗了。。。不过还好,学习了一下 ssh-copy-id 这个命令的用法。。

以上是关于CentOS 8 配置 authorized_keys 免密登录后,免密登录失败的排查以及最终的解决的主要内容,如果未能解决你的问题,请参考以下文章

Centos网络配置

CentOS 6.5系统安装配置图解教程

CentOS 8个人配置文档

CentOS 8.4 yum源配置

CentOS 8 启用网卡与配置网卡固定 IP 等信息

VMware workstations 虚拟机Centos 8下的网络配置 问题