SSH 禁止密码登录
2025/5/30小于 1 分钟
ubuntu 新装后配置 SSH 禁止密码登录失效
今日将云服务器内的系统重装,在重新配置 SSH 配置时,已设置禁止密码登录,但未生效,
root@ubuntu:~# cat /etc/ssh/sshd_config | grep PasswordAuthentication
PasswordAuthentication no # 关闭密码登录
通过搜索发现,仍存在另一处配置影响,
root@ubuntu:~# grep -r "PasswordAuthentication" /etc/
/etc/ssh/sshd_config:PasswordAuthentication no # 关闭密码登录
/etc/ssh/sshd_config.d/01-PasswordAuthentication.conf:PasswordAuthentication yes
先备份文件再将该文件内的配置从 yes 改为 no,
root@ubuntu:~# cp /etc/ssh/sshd_config.d/01-PasswordAuthentication.conf /etc/ssh/sshd_config.d/01-PasswordAuthentication.conf.bak$(date +%s)
root@ubuntu:~# ls -l /etc/ssh/sshd_config.d/
total 12
-rw-r--r-- 1 root root 26 May 28 18:32 01-PasswordAuthentication.conf
-rw-r--r-- 1 root root 27 May 28 18:32 01-PasswordAuthentication.conf.bak1748428324
-rw-r--r-- 1 root root 20 May 28 09:24 01-permitrootlogin.conf
root@ubuntu:~# sed -i 's/^PasswordAuthentication\s\+yes/PasswordAuthentication no/' /etc/ssh/sshd_config.d/01-PasswordAuthentication.conf
root@ubuntu:~# grep -r "PasswordAuthentication" /etc/
/etc/ssh/sshd_config:PasswordAuthentication no # 关闭密码登录
/etc/ssh/sshd_config.d/01-PasswordAuthentication.conf:PasswordAuthentication no
最后重启 SSH 服务,使配置生效。
root@ubuntu:~# systemctl restart sshd