在 CentOS 上“禁止 SSH 密码登录”通常是指只允许密钥登录,禁用密码认证。操作步骤如下:
在禁用密码前,必须保证你能用密钥登录,否则会被锁在服务器外。
ssh-keygen
ssh-copy-id user@server_ip
测试一下:
ssh user@server_ip
能免密登录再继续。
编辑配置文件:
sudo vi /etc/ssh/sshd_config
修改或确认以下参数:
PubkeyAuthentication yes
PasswordAuthentication no
PermitRootLogin no # 建议禁止 root 直接登录(可选)
ChallengeResponseAuthentication no
⚠️ 注意:
# 注释,去掉sudo systemctl restart sshd
service sshd restart
另开一个终端测试(不要断开当前连接):
ssh user@server_ip
检查是否有其他配置文件:
ls /etc/ssh/sshd_config.d/
如有,查看是否覆盖了 PasswordAuthentication
一般不会,但可检查:
getenforce
fail2ban如果你告诉我 CentOS 版本(6/7/8/Stream) 和 是否用 root 登录,我可以给你更精确的配置。