Systemd Cheatsheet - Systemd Service Management Command Reference

管理 Linux 系统服务必备的 Systemd 命令都在这里了,从服务控制到日志排查,按场景分类整理,现场排障直接复制。

SysOps·47 commands·Last updated 2026-07-21
Back to SysOps

服务管理 8

systemctl start nginx
启动服务
systemctl stop nginx
停止服务
systemctl restart nginx
重启服务,会断连接
systemctl reload nginx
平滑重载配置,不断连接,不是所有服务都支持
systemctl enable nginx
设置开机自启,加 --now 同时启动
systemctl disable nginx
取消开机自启
systemctl enable --now nginx
设置开机自启并立即启动
systemctl mask nginx
彻底禁用服务,防止被其他服务间接拉起

状态查看 7

systemctl status nginx
查看服务状态和最近日志,最常用
systemctl is-enabled nginx
查看是否开机自启
systemctl is-active nginx
查看当前是否运行
systemctl is-failed nginx
检查服务是否处于 failed 状态
systemctl list-units --type=service
列出所有已加载的服务单元
systemctl list-unit-files --type=service
列出所有已安装的服务文件及自启状态
systemctl list-units --failed
列出失败的服务,排查启动异常

日志排查(journalctl) 8

journalctl -u nginx
查看指定服务日志
journalctl -u nginx -f
实时跟踪服务日志
journalctl --since "10 min ago" -u nginx
查看最近 10 分钟日志
journalctl --since today --until "1 hour ago"
按时间范围过滤日志
journalctl -p err -b
查看本次启动以来的 error 及以上级别日志
journalctl -u nginx -n 100 --no-pager
查看最近 100 条且不分页
journalctl --disk-usage
查看 journal 日志占用磁盘大小
journalctl --vacuum-size=100M
清理日志,保留最近 100MB

目标与运行级 6

systemctl get-default
查看默认启动目标,等价于查看默认运行级
systemctl set-default multi-user.target
设置默认为多用户命令行模式
systemctl set-default graphical.target
设置默认为图形界面模式
systemctl isolate multi-user.target
立即切换到多用户模式,等价于 init 3
systemctl rescue
进入救援模式,需 root,会停止非关键服务
systemctl list-units --type=target
列出当前所有已激活的目标

timer 定时任务 6

systemctl list-timers
列出所有定时任务,替代 crontab -l
systemctl list-timers --all
列出所有 timer 包括未激活的
systemctl status <timer>
查看指定 timer 状态和下次执行时间
systemctl start <timer> && systemctl enable <timer>
启用并设置开机自启 timer
systemctl cat <timer>
查看 timer 单元文件内容
journalctl -u <timer>
查看 timer 触发的服务日志

服务配置与资源控制 6

systemctl cat nginx
查看服务的单元文件内容
systemctl edit nginx
编辑服务覆盖配置,存于 override.conf
systemctl edit --full nginx
编辑完整的单元文件,非覆盖方式
systemctl daemon-reload
修改单元文件后重新加载 systemd 配置
systemctl set-property nginx CPUQuota=50%
限制服务最多使用 50% CPU
systemctl set-property nginx MemoryMax=512M
限制服务最大内存 512MB

故障排查与分析 6

systemctl --failed
快速查看所有失败的服务
systemctl reset-failed nginx
清除服务的 failed 状态
systemd-analyze time
查看本次启动总耗时
systemd-analyze blame
按耗时排序查看各服务启动时间
systemd-analyze critical-chain nginx
查看指定服务的启动依赖链
systemctl daemon-reload
修改单元文件后必须执行,否则配置不生效

💡 Tips

  • reload 是平滑重载配置不会断连接,restart 会重启进程;不是所有服务都支持 reload,nginx 支持。
  • journalctl 日志默认会轮转,--disk-usage 太大时用 journalctl --vacuum-size=100M 清理。
  • timer 比 crontab 更可控,能在 journalctl 里看执行日志,且支持依赖关系。
  • 修改单元文件后必须执行 systemctl daemon-reload,否则 systemd 不会读取新配置。
  • systemctl edit 创建的是 override.conf 覆盖片段,不会修改原始单元文件,更安全。
  • mask 比 disable 更彻底,被 mask 的服务无法被手动或间接启动,卸载服务前可先 mask。