问题描述
今天配置SSH免密登录时,使用 ssh-keygen 命令成功生成了公钥和私钥,并且也执行了 ssh-copy-id 机器地址 将公钥添加到了服务器的authorized_keys文件中。紧接着用 service sshd restart 命令重启了SSH服务,但当执行 ssh 登录命令时仍然需要密码。
解决方案
一、查看目标机器上系统安全日志,定位问题
执行 sudo cat /var/log/secure
查看系统的安全日志,然后再安全日志中看到SSH登录过程中提示了如下错误:
Apr 15 19:29:29 iZ8vb5di5ol6ae5n8prlaoZ sshd[31769]: Authentication refused: bad ownership or modes for file /root/.ssh/authorized_keys
二、解决SSH Authentication Refused问题
从日志中我们已经分析出了错误的具体原因
chmod 400 /root/.ssh/authorized_keys
报错如下: chmod: changing permissions of 'authorized_keys': Operation not permitted
lsattr /root/.ssh/authorized_keys
chattr -i /root/.ssh/authorized_keys
su jenkins
cd ~
ssh-keygen -t ed25519 -C "admin@teimloit.com"
cd .ssh
ssh-copy-id -i ./id_ed25519.pub root@ip