温馨提示×

centos怎么关闭ssh登录

小樊
47
2026-09-24 00:33:05
栏目: 智能运维

在 CentOS 上“关闭 SSH 登录”有几种不同含义,下面按常见场景给你说明,你可以按需选择。


一、完全关闭 SSH 服务(不允许任何人通过 SSH 连接)

CentOS 6 及以前(init 系统)

service sshd stop
chkconfig sshd off

CentOS 7 / 8 / Stream(systemd)

systemctl stop sshd
systemctl disable sshd

✅ 效果:SSH 服务停止,服务器无法通过 SSH 远程登录
⚠ 注意:如果你当前是通过 SSH 登录的,执行后会被断开且无法再连回


二、禁止 root 用户 SSH 登录(推荐安全做法)

编辑配置文件:

vi /etc/ssh/sshd_config

修改或添加:

PermitRootLogin no

重启服务:

systemctl restart sshd

三、禁止所有用户 SSH 登录(仅本地登录)

在 /etc/ssh/sshd_config 中:

DenyUsers *

或

AllowUsers 空

然后重启:

systemctl restart sshd

四、通过防火墙禁止 SSH 端口(22)

systemctl stop firewalld
# 或
firewall-cmd --permanent --remove-service=ssh
firewall-cmd --reload

或 iptables:

iptables -A INPUT -p tcp --dport 22 -j DROP

五、临时禁止 SSH(维护模式)

systemctl stop sshd

(不 disable,维护完再 start)


建议

  • 如果是安全加固:用「禁止 root 登录 + 密钥登录」
  • 如果是彻底不用 SSH:停止并禁用 sshd
  • 如果是云服务器:谨慎操作,避免失联

如果你能说明是 CentOS 几 + 想达到什么目的,我可以给你更精确的命令。

0 踩