Systemd 命令速查表 - Systemd 服务管理常用命令大全
即便只会 systemctl start/stop,也建议把这套命令过一遍——这是 systemd 发行版下管理服务的标准方式。除了启停与开机自启,这里还涵盖日志定向查询、timer 定时任务、资源上限和控制组单元树,能解决"服务起不来、日志去哪找、怎么限 CPU 内存"这三类高频问题。
服务管理 8
systemctl start nginx临时启动服务并进入 running 状态,不会加入开机自启
systemctl stop nginx停止运行中的服务,退出前执行 ExecStop;不影响开机自启配置
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修改单元文件后必须执行,否则配置不生效
提示
- 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。
由 巧匠 维护
公开更新于 2026年7月21日,内容持续校对官方文档。