OpenSSH 是 Linux 系统中最常用的远程登录工具之一。但在某些场景下(如安全加固、本地测试或临时禁用远程访问),你可能需要关闭 OpenSSH 服务。本文将介绍在主流 Linux 发行版中安全关闭 OpenSSH 的方法。
在关闭服务前,建议先确认 OpenSSH 服务的状态:
# 使用 systemctl(适用于 systemd 系统)
systemctl status ssh
# 或
systemctl status sshd
# 使用 service 命令(兼容旧系统)
service ssh status
# 或
service sshd status
注意:Ubuntu/Debian 系统通常使用 ssh 作为服务名,而 CentOS/RHEL/Fedora 使用 sshd。
以下命令会立即停止服务,但系统重启后仍会自动启动(如果已启用开机自启):
# Ubuntu / Debian
sudo systemctl stop ssh
# CentOS / RHEL / Fedora
sudo systemctl stop sshd
# 通用 service 命令
sudo service ssh stop # Debian/Ubuntu
sudo service sshd stop # RHEL/CentOS
若希望系统重启后也不自动启动 SSH 服务,需禁用其开机自启:
# Ubuntu / Debian
sudo systemctl disable ssh
# CentOS / RHEL / Fedora
sudo systemctl disable sshd
可使用以下命令确认 SSH 端口(默认 22)是否仍在监听:
sudo ss -tuln | grep ':22'
# 或
sudo netstat -tuln | grep ':22'
若无输出,说明服务已成功关闭。
# 启动服务
sudo systemctl start ssh # Debian/Ubuntu
sudo systemctl start sshd # RHEL/CentOS
# 启用开机自启
sudo systemctl enable ssh
# 或
sudo systemctl enable sshd