Linux systemctl 常用管理命令总结

3次阅读

Linux systemctl 常用管理命令总结

systemctl 是 systemd 系统和服务管理器的核心命令,用于管理系统启动、服务控制、单元状态查看等。掌握常用子命令能大幅提升 linux 服务运维效率。

查看服务状态与列表

快速确认服务是否运行、启用或失败:

  • systemctl status nginx — 查看 nginx 服务当前详细状态(含日志片段)
  • systemctl list-units –type=service — 列出所有已加载的服务(默认仅活跃)
  • systemctl list-units –type=service –all — 显示所有服务(含 inactive、failed、disabled)
  • systemctl is-active sshd — 返回 active / inactive / failed,适合脚本判断
  • systemctl is-enabled nginx — 检查是否开机自启(enabled / disabled / Static

启动、停止、重启与重载服务

操作即时生效,不涉及开机配置:

  • systemctl start httpd — 启动服务(一次生效)
  • systemctl stop redis — 停止运行中的服务
  • systemctl restart mysql — 先 stop 再 start,适用于配置变更后
  • systemctl reload nginx — 仅重载配置(不中断连接),需服务本身支持 SIGHUP
  • systemctl try-restart nginx — 仅当服务正在运行时才重启,避免意外启动

启用/禁用开机自启

修改服务的持久化设置(写入 /etc/systemd/system/ 或 /usr/lib/systemd/system/):

  • systemctl enable docker — 创建软链接,使服务随系统启动
  • systemctl disable chronyd — 移除软链接,取消开机自启
  • systemctl enable –now httpd — 同时启用并立即启动(常用组合)
  • systemctl disable –now postfix — 同时禁用并立即停止

排查常见问题

定位服务无法启动或异常退出的原因:

  • systemctl status -l nginx — 加 -l 显示完整日志(避免截断)
  • journalctl -u nginx -n 50 -f — 实时跟踪 nginx 最近 50 行日志
  • systemctl show nginx.service | grep -E “(ExecStart|Restart|WantedBy)” — 查看服务定义关键字段
  • systemctl daemon-reload — 修改 .service 文件后必须执行,重新加载单元配置
text=ZqhQzanResources