systemctl 是现代 Linux 系统管理服务开机自启的核心工具,统一管控启动状态、启用/禁用逻辑及依赖关系;需先确认 .service 单元存在,再通过 enable/disable 设置自启,--now 实现启用并立即启动,is-enabled 与 is-active 分别检查自启和运行状态。

在现代 Linux 系统(如 CentOS 7+、Ubuntu 16.04+、Debian 8+)中,systemctl 是管理服务开机自启的核心工具。它不只控制“是否开机启动”,还统一管理服务的当前运行状态、启用/禁用逻辑和依赖关系。
确认服务是否已由 systemd 管理
不是所有程序都自带 .service 文件。若直接运行 systemctl enable xxx 报错 “Unit not found”,说明该服务尚未注册为 systemd 单元。
- 先检查服务单元是否存在:
systemctl list-unit-files --type=service | grep xxx - 手动启动测试是否可行:
systemctl start xxx.service,再用systemctl status xxx查看有无报错 - 若提示找不到服务,需先创建或安装对应的
/usr/lib/systemd/system/xxx.service或/etc/systemd/system/xxx.service
设置或取消开机自启
enable 和 disable 操作本质是创建或删除符号链接,不改变当前运行状态。
Linux 安全审计工具,检查 SSH 配置、开放/监听端口、防火墙规则(ufw/iptables/nftables)、失败登录尝试、sudoers 权限...
- 设为开机自启:
sudo systemctl enable xxx(.service 后缀可省略) - 取消开机自启:
sudo systemctl disable xxx - 验证结果:
systemctl is-enabled xxx返回enabled或disabled - 注意:disable 不会停止正在运行的服务;如需同时停用,额外执行
sudo systemctl stop xxx
启用并立即启动(一步到位)
很多场景下既要开机自启,又希望现在就生效——这时用 --now 选项最直接。
- 启用 + 立即启动:
sudo systemctl enable --now xxx - 禁用 + 立即停止:
sudo systemctl disable --now xxx - 执行后可用
systemctl is-active xxx确认服务是否正在运行
查看服务自启与运行状态
两个命令分工明确,建议配合使用:
-
systemctl is-enabled xxx:只回答“开机时会不会自动拉起” -
systemctl is-active xxx:只回答“此刻服务有没有在跑” -
systemctl status xxx:综合显示启用状态、运行状态、最近日志、主进程 PID 等,适合快速诊断 - 批量查看所有服务的启用情况:
systemctl list-unit-files --type=service










