systemctl 是 linux 系统中管理服务的核心命令,兼顾当前运行状态与开机自启设置;常用操作包括 status 查状态、start/stop/restart 控制运行、enable/disable 设置自启,并需 daemon-reload 重载配置。

systemctl 是 Linux 系统中管理服务(service)的核心命令,取代了旧版的 service 和 chkconfig。它既管“当前运行状态”,也管“开机是否自启”,操作简洁但逻辑清晰。
查看服务状态与列表
快速确认某个服务是否在运行、有没有报错,是日常运维第一步。
- systemctl status sshd —— 查看 SSH 服务实时状态(含日志片段、启用状态、最近启动时间)
- systemctl list-units –type=service –state=running —— 列出所有正在运行的服务
- systemctl list-unit-files –type=service —— 查看所有服务的“开机自启”设置(enabled/disabled/Static 等)
启动、停止、重启一个服务
这些操作只影响当前会话,不改变开机行为。适合调试或临时启用/禁用服务。
- systemctl start nginx —— 启动 nginx(无输出即成功;失败时会显示错误原因)
- systemctl stop nginx —— 停止 Nginx
- systemctl restart nginx —— 先 stop 再 start,常用于重载配置后生效
- systemctl reload nginx —— 仅重载配置(不中断连接),前提是服务支持 reload 操作
设置开机自启或禁止自启
enable/disable 决定系统启动时是否自动拉起该服务,和当前运行状态无关。
- systemctl enable httpd —— 创建软链接到 /etc/systemd/system/multi-user.target.wants/,下次开机自动启动
- systemctl disable httpd —— 移除该软链接,开机不再启动
- systemctl is-enabled sshd —— 快速检查是否已设为开机自启(返回 enabled 或 disabled)
常见问题与注意点
实际使用中容易卡住的地方,往往不是命令写错,而是理解偏差。
- start/stop 不影响 enable/disable 设置;反之亦然——两者独立控制“现在”和“以后”
- 修改服务文件(如 /usr/lib/systemd/system/nginx.service)后,需运行 systemctl daemon-reload 才能让 systemd 重新读取配置
- 服务名不带 .service 后缀也能识别(systemctl start nginx 等价于 systemctl start nginx.service),但写全更明确
- 部分服务(如 network-manager)可能被其他服务依赖,disable 前建议先查依赖:systemctl list-dependencies –reverse nginx
基本上就这些。掌握 status、start/stop、enable/disable 这三组动作,再配合 daemon-reload,就能覆盖 95% 的日常服务管理场景。