在 CentOS 中“关闭 SSH 登录”有多种理解,下面按常见场景分别说明,你可以按需选择。
service sshd stop
chkconfig sshd off
systemctl stop sshd
systemctl disable sshd
⚠️ 注意:如果你是通过 SSH 远程连接的,执行后你会立即断开且无法再连上,请确保有其它管理方式(如控制台、IPMI)。
编辑配置文件:
vi /etc/ssh/sshd_config
修改:
PermitRootLogin no
重启服务:
systemctl restart sshd
在 sshd_config 中添加:
DenyUsers user1 user2
或只允许某些用户:
AllowUsers alice bob
然后重启:
systemctl restart sshd
# 查看
firewall-cmd --list-services
# 移除 ssh
firewall-cmd --permanent --remove-service=ssh
firewall-cmd --reload
iptables -A INPUT -p tcp --dport 22 -j DROP
service iptables save
systemctl stop sshd
需要时再:
systemctl start sshd
在 sshd_config 中:
AllowUsers none
或
DenyUsers *
然后:
systemctl restart sshd
如果你能说明是 临时 / 永久 / 禁止某个用户 / 云服务器,我可以给你更精确的操作命令。