在 CentOS 上“关闭 SSH 密码登录”通常是指 禁止通过密码认证 SSH,只允许密钥登录。操作步骤如下:
否则一旦关了密码,可能直接连不上服务器。
ssh-keygen -t rsa -b 4096
ssh-copy-id user@centos_ip
或手动把 ~/.ssh/id_rsa.pub 内容追加到服务器的:
~/.ssh/authorized_keys
ssh user@centos_ip
✅ 能登录再继续
编辑配置文件:
sudo vi /etc/ssh/sshd_config
修改或确认以下参数:
PubkeyAuthentication yes
PasswordAuthentication no
PermitRootLogin no # 建议禁止 root 密码登录(可选)
ChallengeResponseAuthentication no
UsePAM no
sudo systemctl restart sshd
service sshd restart
重新开一个终端测试:
ssh user@centos_ip
authorized_keys 权限:chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
restorecon -Rv ~/.ssh
PermitRootLogin prohibit-password
如果你是说 完全关闭 SSH(停止服务) 或 只允许特定用户,也可以告诉我,我可以给你对应方案。