在 CentOS 中“禁止 SSH 登录”有多种层级和场景,下面按常见需求从最安全/最常用到最彻底逐一说明,你可以按需选择。
systemctl stop sshd
systemctl disable sshd
✅ 效果:服务器完全不接受 SSH 连接
⚠ 注意:远程服务器会立刻断开且无法再连,请确保你有其他方式(控制台 / IPMI / 物理机)
编辑配置文件:
vi /etc/ssh/sshd_config
修改或添加:
PasswordAuthentication no
重启服务:
systemctl restart sshd
✅ 效果:禁止密码登录,只允许密钥
vi /etc/ssh/sshd_config
修改:
PermitRootLogin no
重启:
systemctl restart sshd
✅ 效果:root 不能直接 SSH,但普通用户可以
DenyUsers user1 user2
passwd -l username
usermod -s /sbin/nologin username
重启 sshd:
systemctl restart sshd
AllowUsers admin1 admin2
# 或
AllowGroups sshusers
重启 sshd 生效。
firewall-cmd --permanent --remove-service=ssh
firewall-cmd --reload
或:
iptables -A INPUT -p tcp --dport 22 -j DROP
systemctl stop sshd
需要恢复时:
systemctl start sshd
| 需求 | 推荐方式 |
|---|---|
| 彻底关闭 | 停止 sshd |
| 安全加固 | 禁止 root + 密码 |
| 限制用户 | DenyUsers / AllowUsers |
| 网络层 | 防火墙 |
如果你能说明是 云服务器 / 内网服务器 / 是否还有控制台权限,我可以给你最安全的具体操作方案。