Skip to content

Ubuntu配置禁止密码登录无效

问题

Ubuntu20.04禁用密码登录,使用密钥登陆。本来是十分简单的操作,但是ssh配置一直不生效。再三检查ssh配置文件都没有任何问题。

原因

在ssh配置文件中,默认导入sshd_config.d文件夹下的所有conf配置。

sh
Include /etc/ssh/sshd_config.d/*.conf

在ubntu中默认生成50-cloud-init.conf,在sshd_config.d文件夹下

sh
taketo@ubunt:~$ ll /etc/ssh/sshd_config.d/
total 12
drwxr-xr-x 2 root root 4096 Aug 11 08:55 ./
drwxr-xr-x 4 root root 4096 Aug 11 12:19 ../
-rw------- 1 root root   27 Aug 11 08:55 50-cloud-init.conf
taketo@ubunt:~$ sudo cat /etc/ssh/sshd_config.d/50-cloud-init.conf 
PasswordAuthentication yes

解决方案

删除50-cloud-init.conf文件或者在50-cloud-init.conf配置文件中配置禁止密码登陆参数。

sh
taketo@ubunt:~$ sudo cat /etc/ssh/sshd_config.d/50-cloud-init.conf 
PasswordAuthentication no
PubkeyAuthentication yes

# 重启ssh服务
taketo@ubunt:~$ systemctl restart ssh