GeekIt

systemctl服务管理速查

服务启停、开机自启、unit 编写、日志查看、定时器等 135 条常用命令

共 135 条命令 · 10 类场景 · 点击右侧图标可复制

命令 / 配置说明复制
systemctl status nginx查看服务状态、主 PID、最近日志与退出码
systemctl status nginx -l --no-pager完整显示日志行且不分页,便于脚本或日志留存
systemctl is-active nginx只输出 active/inactive,适合脚本判断
systemctl is-enabled nginx查看是否开机自启(enabled/disabled/masked)
systemctl is-failed nginx判断服务是否处于 failed 状态
systemctl list-units --type=service列出当前加载的服务单元
systemctl list-units --type=service --state=running只看正在运行的服务
systemctl list-units --failed列出所有启动失败的服务,排障第一步
systemctl list-unit-files --type=service查看全部已安装服务的开机自启状态
systemctl list-unit-files | grep enabled筛选出所有开机自启项
systemctl cat nginx查看服务的 unit 文件内容与所在路径
systemctl show nginx查看服务全部属性(含默认值)
systemctl show nginx -p MainPID -p ExecStart -p Restart只看关心的属性,排查启动命令是否被覆盖
systemctl --version查看 systemd 版本,判断支持的指令集
systemctl list-jobs查看正在执行的任务,启动卡住时使用
systemctl list-sockets查看 socket 激活单元,排查端口被 systemd 占用
命令 / 配置说明复制
systemctl start nginx启动服务
systemctl stop nginx停止服务
systemctl restart nginx重启服务,会中断现有连接
systemctl reload nginx重载配置,进程不退出
systemctl reload-or-restart nginx支持重载则重载,否则重启
systemctl try-restart nginx仅在服务已运行时才重启
systemctl enable nginx设置开机自启(建立 wants 符号链接)
systemctl disable nginx取消开机自启
systemctl enable --now nginx设置自启并立即启动,部署时最常用
systemctl disable --now nginx取消自启并立即停止
systemctl reenable nginx重建自启链接,修正 unit 文件改名后的残留
systemctl mask nginx彻底禁用服务,连手动启动也会被拒绝
systemctl unmask nginx解除 mask,恢复可用状态
systemctl reset-failed nginx清除 failed 计数与状态,便于重新拉起
systemctl kill -s HUP nginx向服务的所有进程发送信号
systemctl daemon-reload新增或修改 unit 文件后必须执行
命令 / 配置说明复制
vim /etc/systemd/system/app.service自定义 unit 放在 /etc/systemd/system,优先级高于 /usr/lib/systemd/system
[Unit] Description=My App After=network.targetUnit 段:描述与启动顺序依赖
[Service] Type=simple ExecStart=/opt/app/start.shService 段:前台运行型服务的最小配置
Type=forking 与 PIDFile=/run/app.pid后台守护进程型服务需声明 PID 文件
Type=notify应用支持 sd_notify 时使用,systemd 能准确判断就绪
User=app 与 Group=app以非 root 用户运行服务,权限最小化
WorkingDirectory=/opt/app指定工作目录,相对路径配置不再出错
Environment=ENV=prod TZ=Asia/Shanghai注入环境变量(也可用 EnvironmentFile)
EnvironmentFile=-/etc/sysconfig/app从文件读取环境变量,前缀 - 表示文件不存在也不报错
ExecStartPre=/bin/mkdir -p /run/app启动前执行的准备命令
ExecStop=/bin/kill -TERM $MAINPID自定义停止动作,默认会发 SIGTERM
Restart=always 与 RestartSec=5s异常退出后 5 秒自动重启,常驻服务必备
Restart=on-failure 与 StartLimitBurst=5仅失败时重启,并限制启动频率避免死循环
LimitNOFILE=65535提高文件句柄上限,高并发服务必配
[Install] WantedBy=multi-user.targetInstall 段决定 enable 时挂到哪个 target 下
systemctl daemon-reload && systemctl enable --now app新建服务后的标准生效流程
systemctl edit app创建 override 片段,不修改原 unit 文件即可覆盖配置
systemctl revert app撤销全部 override,恢复原始配置
命令 / 配置说明复制
journalctl -u nginx查看指定服务的全部日志
journalctl -u nginx -f实时跟踪服务日志
journalctl -u nginx -n 200 --no-pager查看最近 200 行且不分页
journalctl -u nginx --since "10 min ago"按相对时间过滤日志
journalctl -u nginx --since "2026-11-02 09:00" --until "2026-11-02 10:00"按绝对时间区间过滤,复盘故障时间线
journalctl -u nginx -p err只看错误级别(emerg/alert/crit/err)
journalctl -xe查看最近的错误与启动失败详情
journalctl -b查看本次开机以来的日志
journalctl -b -1查看上一次开机的日志,排查意外重启
journalctl -k只查看内核日志(等价 dmesg)
journalctl -o short-iso -u nginx -n 50带 ISO 时间戳输出,便于与监控数据对齐
journalctl -o json-pretty -u nginx -n 5结构化输出,方便脚本解析字段
journalctl --disk-usage查看日志占用磁盘空间
journalctl --vacuum-size=500M压缩日志到 500M 以内
journalctl --vacuum-time=7d只保留最近 7 天日志
grep -i failed /var/log/messages非 journald 环境可直接查文件日志
命令 / 配置说明复制
systemctl list-timers --all查看全部定时器及下次触发时间
systemctl status backup.timer查看定时器状态与上次/下次执行时间
systemctl start backup.timer启动定时器
systemctl enable --now backup.timer设置开机自启并立即生效
systemctl list-units --type=timer列出已加载的 timer 单元
vim /etc/systemd/system/backup.service定时任务的 service:写实际执行的命令
vim /etc/systemd/system/backup.timer定时任务的 timer:定义触发规则,与 service 同名
OnCalendar=daily 与 OnCalendar=*-*-* 02:00:00timer 中按日历规则触发
OnUnitActiveSec=1h距上次执行 1 小时后再次触发
Persistent=true关机期间错过的任务在开机后补跑一次
Unit=backup.servicetimer 显式指定要触发的 service
systemd-analyze calendar "*-*-* 02:00:00"校验日历表达式并查看下次触发时间
systemctl daemon-reload && systemctl enable --now backup.timer新建 timer 后的标准生效流程
命令 / 配置说明复制
systemctl get-default查看默认启动 target
systemctl set-default multi-user.target设置默认启动到命令行模式
systemctl set-default graphical.target设置默认启动到图形界面
systemctl isolate multi-user.target临时切换运行级别,立即停止图形服务
systemctl list-units --type=target列出所有 target 及状态
systemctl rescue进入单用户救援模式(需要 root 密码)
systemctl emergency进入紧急模式,仅挂载根文件系统
systemctl default返回默认 target
systemctl list-dependencies multi-user.target查看 target 下挂载的全部服务
systemctl isolate rescue.target排障时切换到最小环境
命令 / 配置说明复制
systemctl list-dependencies nginx查看服务依赖哪些单元
systemctl list-dependencies nginx --reverse查看谁依赖当前服务,判断能否安全停止
systemd-analyze查看开机总耗时(内核 + 用户空间)
systemd-analyze blame按耗时排序启动项,找出拖慢开机的服务
systemd-analyze critical-chain查看关键启动链路,定位瓶颈环节
systemd-analyze verify /etc/systemd/system/app.service校验 unit 文件语法与依赖是否有效
systemd-analyze plot > boot.svg导出启动时序图,直观对比优化效果
systemctl show -p After -p Wants nginx查看 unit 的启动顺序与依赖声明
systemctl mask --now systemd-timesyncd屏蔽与自建 NTP 冲突的时间同步服务
命令 / 配置说明复制
systemctl set-property nginx MemoryMax=1G限制服务内存上限,超限会被 OOM 杀掉
systemctl set-property nginx CPUQuota=200%限制 CPU 使用(200% 表示 2 个核)
systemctl set-property nginx TasksMax=512限制最大进程/线程数
systemctl show nginx -p MemoryCurrent -p CPUUsageNSec查看服务当前资源占用(cgroup v2)
systemctl status nginx | grep -i cgroup确认服务所在 cgroup 路径
systemd-run --scope -p MemoryMax=512M /opt/tool/run.sh以临时 scope 限制某次命令的资源
systemd-run --unit=tmp-job -p CPUQuota=100% /opt/tool/job.sh临时以独立 unit 运行任务,可随时 stop
ProtectSystem=strict 与 ProtectHome=trueunit 中强化隔离,限制服务写系统目录与访问家目录
PrivateTmp=true 与 NoNewPrivileges=true独立临时目录并禁止提权,提升服务安全性
ReadWritePaths=/data/app配合 ProtectSystem=strict 开放必要可写路径
命令 / 配置说明复制
systemctl reboot重启系统(等同 reboot 命令)
systemctl poweroff关闭系统
systemctl suspend 与 systemctl hibernate挂起休眠与休眠到磁盘
systemctl hybrid-sleep混合休眠,兼顾恢复速度与掉电安全
hostnamectl查看主机名、系统版本与虚拟化信息
hostnamectl set-hostname web-01修改主机名,立即生效且持久
timedatectl查看时区、NTP 同步状态与当前时间
timedatectl set-timezone Asia/Shanghai设置时区,日志时间对齐必做
timedatectl set-ntp true启用 NTP 自动同步
timedatectl list-timezones | grep Shanghai查询可用时区名称
localectl status查看系统语言与键盘布局
loginctl list-sessions查看当前登录会话
loginctl terminate-user app强制注销指定用户的全部会话
systemctl --user status app查看当前用户的用户级服务(无需 root)
命令 / 配置说明复制
systemctl status app -l --no-pager && journalctl -u app -n 100 --no-pager服务异常时的第一条组合命令
systemctl list-units --failed先找出所有失败单元,避免漏掉关联故障
systemctl is-enabled app重启后没起来先确认是否 enabled
systemctl is-enabled app 返回 masked被 mask 时任何启动都会被拒绝,需 unmask
systemd-analyze verify /etc/systemd/system/app.serviceunit 写错时直接告诉你哪一行有问题
systemctl daemon-reload改了 unit 但行为没变化,八成忘了 reload
journalctl -u app -p err --since "1 hour ago"聚焦近一小时的运行错误
ls -l /etc/systemd/system/*.wants/检查自启链接是否真的建立
grep -r "ExecStart" /etc/systemd/system/app.service.d/排查 override 片段是否覆盖了启动命令
systemctl reset-failed app && systemctl start app达到启动次数上限(start-limit-hit)时的恢复方式
systemctl show app -p ExecMainStatus -p Result查看上次退出码与失败原因
journalctl -u app | grep -i "timeout"定位启动超时(Job timeout)问题
ss -lntp | grep app确认服务端口是否真的监听成功

unit 文件三段式模板

Unit 段(基础信息)[Unit] Description=My App After=network-online.target Wants=network-online.target
Service 段(运行方式)[Service] Type=simple User=app WorkingDirectory=/opt/app EnvironmentFile=-/etc/sysconfig/app ExecStart=/opt/app/bin/start.sh Restart=always RestartSec=5s LimitNOFILE=65535
Install 段(开机自启)[Install] WantedBy=multi-user.target

常用命令组合

新建服务标准流程vim /etc/systemd/system/app.service && systemctl daemon-reload && systemctl enable --now app && systemctl status app -l --no-pager
排查服务异常systemctl status app -l --no-pager && journalctl -u app -n 200 --no-pager && systemctl list-units --failed
重载与验证nginx -t && systemctl reload nginx && systemctl is-active nginx
开机自启核对systemctl list-unit-files --type=service | grep enabled
清理日志占用journalctl --disk-usage && journalctl --vacuum-size=500M
维护前安全停止systemctl list-dependencies app --reverse && systemctl stop app

服务排障常用思路

1. 服务启动失败(failed)

systemctl status 看退出码 → journalctl -u 看报错 → 确认路径、权限与依赖

2. 改了配置不生效

unit 文件改动必须 daemon-reload;应用配置改动要在服务内支持 reload

3. 重启后服务没起来

is-enabled 检查自启,list-unit-files 看状态,必要时 enable --now

4. start-limit-hit

短时间重启过多被限流:reset-failed 后修好根因再启动

5. 端口冲突/无法绑定

ss -lntp 查占用,确认是否有 socket 激活或旧进程残留

6. OOM 被杀

journalctl -k 查 kernel OOM 记录,合理设置 MemoryMax 与应用堆大小

共 135 条 systemctl 相关命令 · 修改 unit 文件后记得执行 systemctl daemon-reload

关于systemctl服务管理速查

在线 systemctl 命令速查手册,按服务状态查询、启停与开机自启、unit 文件编写、journalctl 日志查看、timer 定时器、target 运行级别、依赖关系分析、资源限制与安全、电源与主机管理、常见排障等 10 类场景整理 120+ 条常用 systemctl 及相关命令,每条附中文说明与典型用法,支持关键词搜索、分类折叠和一键复制,打开网页即可查询,无需登录。涵盖 systemctl start/stop/restart/reload/status/enable/disable/mask、list-units、list-unit-files、is-active、daemon-reload、systemd-analyze blame/verify、journalctl -u/-f/--since/vacuum-size、systemd-run、timedatectl、hostnamectl 等高频命令,并附 unit 文件模板与 Type、Restart、ExecStart 等关键字段说明。适用于 Linux 服务部署上线、开机自启配置、服务启动失败排查、日志定位、systemd 定时任务替代 crontab,以及运维面试与 RHCE/Linux 认证备考。

核心能力

  • 涵盖 systemctl start/stop/restart/reload/status/enable/disable/mask、list-units、list-unit-files、is-active、daemon-reload、systemd-analyze blame/verify、journalctl -u/-f/--since/vacuum-size、systemd-run、timedatectl、hostnamectl 等高频命令,并附 unit 文件模板与 Type、Restart、ExecStart 等关键字段说明

适用场景

适用于 Linux 服务部署上线、开机自启配置、服务启动失败排查、日志定位、systemd 定时任务替代 crontab,以及运维面试与 RHCE/Linux 认证备考。systemctl服务管理速查全程在浏览器中完成,无需安装插件,可放心用于日常办公与排障。

常见问题

systemctl服务管理速查是免费的吗?需要注册或安装吗?
systemctl服务管理速查完全免费,无需注册、无需下载安装任何软件,用浏览器打开网页即可直接使用,也不限制使用次数。
systemctl服务管理速查支持哪些功能?
涵盖 systemctl start/stop/restart/reload/status/enable/disable/mask、list-units、list-unit-files、is-active、daemon-reload、systemd-analyze blame/verify、journalctl -u/-f/--since/vacuum-size、systemd-run、timedatectl、hostnamectl 等高频命令,并附 unit 文件模板与 Type、Restart、ExecStart 等关键字段说明。
systemctl服务管理速查适合哪些场景使用?
适用于 Linux 服务部署上线、开机自启配置、服务启动失败排查、日志定位、systemd 定时任务替代 crontab,以及运维面试与 RHCE/Linux 认证备考。