← LeisureLinux 文章索引
LeisureLinux · 微信公众号文章

systemctl 命令完全指南:Linux systemd 管理器的每一个命令

安全/漏洞 阅读原文(微信)↗

TL;DR: systemctl 是 systemd 系统的核心控制工具。本文按功能分类列出所有 systemctl 命令,涵盖服务管理、单元管理、系统状态、日志、计时器、目标(target)、电源管理、资源控制、安全审计等场景。Linux 系统管理员必备参考手册。

systemd 已经成为绝大多数 Linux 发行版的默认 init 系统,而 systemctl 就是管理它的第一把手。从启停服务到查看日志,从电源管理到资源控制,理解和记住每个命令可以让你在排障时快人一步。

一、服务管理(最常用)

管理 systemd 服务的生命周期:启动、停止、重启、重载、查看状态。

基本操作

# 启动服务
systemctl start <service>

# 停止服务
systemctl stop <service>

# 重启服务
systemctl restart <service>

# 重新加载配置(不中断服务,适用于支持 reload 的服务)
systemctl reload <service>

# 重新加载或重启(优先 reload,不支持则 restart)
systemctl reload-or-restart <service>

# 启用服务(开机自启)
systemctl enable <service>

# 禁用服务
systemctl disable <service>

# 启用并同时启动
systemctl enable --now <service>

# 禁用并同时停止
systemctl disable --now <service>

# 查看服务状态
systemctl status <service>

# 查看服务是否正在运行
systemctl is-active <service>

# 查看服务是否已启用
systemctl is-enabled <service>

# 查看服务是否失败
systemctl is-failed <service>

# 屏蔽服务(彻底禁止启动,包括间接依赖触发)
systemctl mask <service>

# 取消屏蔽
systemctl unmask <service>

查询状态

# 列出所有运行中的服务
systemctl list-units --type=service

# 列出所有服务包括不活跃的
systemctl list-units --type=service --all

# 列出所有已启用的服务
systemctl list-unit-files --type=service --state=enabled

# 列出所有失败的服务
systemctl --failed --type=service

# 列出最近启动时间最长的服务
systemctl list-units --type=service --sort=-ACTIVE

# 查看服务依赖树
systemctl list-dependencies <service>

二、单元管理(Unit)

Unit 是 systemd 的基本管理单元,包括 service、socket、timer、mount、path、device、target、snapshot 等类型。

列出单元

# 列出所有正在运行的单元
systemctl list-units

# 列出所有单元包括不活跃的
systemctl list-units --all

# 按类型过滤
systemctl list-units --type=service    # 服务
systemctl list-units --type=socket     # 套接字
systemctl list-units --type=timer      # 定时器
systemctl list-units --type=target     # 目标
systemctl list-units --type=mount      # 挂载点
systemctl list-units --type=path       # 路径
systemctl list-units --type=device     # 设备
systemctl list-units --type=automount  # 自动挂载
systemctl list-units --type=slice      # 控制组切片

# 按状态过滤
systemctl list-units --state=active
systemctl list-units --state=running
systemctl list-units --state=exited
systemctl list-units --state=dead
systemctl list-units --state=failed
systemctl list-units --state=inactive

# 组合过滤
systemctl list-units --type=service --state=running
systemctl list-units --type=timer --state=active

单元文件管理

# 列出所有单元文件
systemctl list-unit-files

# 按状态列出单元文件
systemctl list-unit-files --state=enabled
systemctl list-unit-files --state=disabled
systemctl list-unit-files --state=static
systemctl list-unit-files --state=masked
systemctl list-unit-files --state=generated
systemctl list-unit-files --state=indirect

# 查看单元文件内容
systemctl cat <service>

# 编辑单元文件(自动创建 override 配置)
systemctl edit <service>

# 创建完整副本并编辑
systemctl edit --full <service>

# 重新加载单元文件(修改后必须执行)
systemctl daemon-reload

# 查看单元文件的 MD5 校验和
systemctl is-failed <service>

# 查看单元文件的实际路径
systemctl show <service> | grep FragmentPath

三、系统状态与信息

# 查看系统整体状态
systemctl status

# 列出所有活跃的单元(类似 list-units)
systemctl

# 查看系统启动耗时
    systemd-analyze
# 查看每个服务的启动耗时
    systemd-analyze blame
# 查看服务启动的关键链
    systemd-analyze critical-chain
# 查看特定服务启动耗时
    systemd-analyze blame | grep <service>
# 查看系统条件的满足情况
    systemd-analyze condition
# 列出所有可用 target
systemctl list-units --type=target

# 查看默认 target
systemctl get-default

# 设置默认 target
systemctl set-default <target>

# 查看主机名
hostnamectl

# 查看系统时间与日期
timedatectl

# 查看系统 locale
localectl

四、日志查看

# 查看系统日志
    journalctl
# 查看特定服务的日志
    journalctl -u <service>
# 查看指定时间范围内的日志
    journalctl --since "2026-06-01"
    journalctl --until "2026-06-30"
    journalctl --since "1 hour ago"
    journalctl --since yesterday
    journalctl --since "2026-06-01" --until "2026-06-02"
# 查看最近 N 行日志
    journalctl -n 50
# 实时跟踪日志(类似 tail -f)
    journalctl -f
# 查看内核日志
    journalctl -k
# 查看本次启动以来的日志
    journalctl -b
# 查看上次启动的日志
    journalctl -b -1
# 查看指定优先级的日志(0: emerg ~ 7: debug)
    journalctl -p err
    journalctl -p warning
    journalctl -p info
# 查看日志占用的磁盘空间
    journalctl --disk-usage
# 清理旧日志
    journalctl --vacuum-time=7d
    journalctl --vacuum-size=500M
# 将日志导出为 JSON
    journalctl -o json
# 查看特定 PID 的日志
    journalctl _PID=<pid>
# 查看特定可执行文件的日志
    journalctl /usr/bin/nginx

五、计时器(systemd Timer)

systemd timer 是 cron 的现代替代方案。使用独立的 .timer 单元文件触发对应的 .service。

# 列出所有计时器
systemctl list-timers

# 列出所有计时器(包括已失效的)
systemctl list-timers --all

# 查看特定计时器的详细信息
systemctl status <timer>

# 启动计时器
systemctl start <timer>

# 启用计时器(开机自启)
systemctl enable <timer>

# 启用并立即启动
systemctl enable --now <timer>

# 停止计时器
systemctl stop <timer>

# 禁用计时器
systemctl disable <timer>

# 查看计时器配置
systemctl cat <timer>

六、电源管理

systemd 整合了 systemd-logind 的电源管理功能。

# 关闭系统
systemctl poweroff

# 重启系统
systemctl reboot

# 重启到固件设置
systemctl reboot --firmware-setup

# 挂起(睡眠)
systemctl suspend

# 休眠
systemctl hibernate

# 混合睡眠(休眠 + 挂起)
systemctl hybrid-sleep

# 挂起后再休眠
systemctl suspend-then-hibernate

# 软重启用户空间(不重启内核)
systemctl soft-reboot

# 退出(退出 init 进程,容器场景用)
systemctl exit

七、Target 管理

Target 是 systemd 中的运行级别(runlevel)的现代等价物。

# 查看当前 target
systemctl get-default

# 设置默认 target
systemctl set-default multi-user.target
systemctl set-default graphical.target

# 切换到指定 target立即生效
systemctl isolate multi-user.target
systemctl isolate graphical.target
systemctl isolate rescue.target
systemctl isolate emergency.target

# 切换到 rescue 模式单用户
systemctl rescue

# 切换到 emergency 模式
systemctl emergency

# 列出所有 target
systemctl list-units --type=target --all

# 查看 target 的依赖
systemctl list-dependencies graphical.target
systemctl list-dependencies multi-user.target

常用 target 对照

Target 等价 Runlevel 说明
poweroff.target runlevel0 关机
rescue.target runlevel1 单用户救援模式
multi-user.target runlevel3 多用户无图形界面
graphical.target runlevel5 多用户带图形界面
reboot.target runlevel6 重启
emergency.target --- 紧急 shell(最小环境)

八、资源控制(CGroup)

# 查看服务的资源使用情况
systemctl show <service> | grep -i memory
systemctl show <service> | grep -i cpu

# 查看所有 cgroup
    systemd-cgls
# 查看 cgroup 资源使用统计
    systemd-cgtop
# 查看控制组状态
    systemd-cgls --unit <service>
# 查看系统整体资源状态
systemctl show

# 查看特定服务的属性
systemctl show <service>

# 修改服务的资源限制(需要编辑或 overridesystemctl set-property <service> CPUQuota=50%
systemctl set-property <service> MemoryMax=512M
systemctl set-property <service> TasksMax=1000

# 查看 service 的所有属性
systemctl show -p <property> <service>

九、安全与审计

# 查看服务的安全等级
    systemd-analyze security <service>
# 查看所有服务的整体安全情况
    systemd-analyze security
# 查看服务的 capability
systemctl show <service> | grep Capability

# 查看服务的保护设置
systemctl show <service> | grep -i protect

# 查看服务的命名空间设置
systemctl show <service> | grep -i -E "(Private|Protect)"

# 查看服务的系统调用过滤
systemctl show <service> | grep SystemCall

十、环境变量与调试

# 查看服务的环境变量
systemctl show <service> | grep Environment

# 为服务临时设置环境变量并启动
systemctl set-environment <KEY>=<value>

# 取消设置环境变量
systemctl unset-environment <KEY>

# 列出所有环境变量
systemctl show-environment

# 调试模式运行服务(前台,非 daemon
    systemd-run --user --unit=debug-<service> <command>
# 查看 systemd 版本
systemctl --version

# 查看帮助
systemctl --help
systemctl -h

十一、远程管理

# 管理远程主机的 systemd
systemctl -H <user>@<host> status <service>

# 通过 SSH 管道执行
systemctl --host=<user>@<host> start <service>

# 管理容器内的 systemd
systemctl -M <container-name> status <service>

十二、快照与回滚

# 创建当前状态快照
systemctl snapshot <name>

# 列出所有快照
systemctl list-units --type=snapshot

# 回滚到指定快照
systemctl isolate <name>.snapshot

# 删除快照
systemctl delete <name>.snapshot

注:snapshot 功能在 systemd v240+ 中已废弃,推荐使用 systemd-run——scope 作为替代。

十三、实用组合技

# 禁用所有服务的开机自启快速排查服务冲突
for srv in $(systemctl list-unit-files --type=service --state=enabled --no-legend | awk '{print $1}'); do
systemctl disable "$srv"

done

# 查找谁占用了端口
systemctl list-sockets

# 列出发送 SIGKILL 信号最多的服务
systemctl show --property=MainPID <service>

# 查看监听端口的服务
ss -tlnp | grep -i systemd

# 快速查看所有活跃服务的状态概览
systemctl list-units --type=service --state=running --no-legend | wc -l

# 查看 systemd  journal  cgroup 根路径
systemctl show | grep -E "(RuntimeWatchdog|ShutdownWatchdog)"

# 实时监控服务启动/停止状态变化
systemctl --system --wait

# 创建临时一次性服务运行后自动清理
    systemd-run --unit=temp-job --remain-after-exit /bin/true

十四、Systemd 组件和小工具

systemd 生态不只是 systemctl,还包括一系列辅助工具:

# 日志工具
    journalctl          # 日志查看
    journalctl -u xxx   # 查看特定服务日志
# 分析工具
    systemd-analyze            # 启动时间分析
    systemd-analyze blame      # 各服务启动耗时
    systemd-analyze critical-chain  # 启动关键链
    systemd-analyze plot > boot.svg # 启动时间图
    systemd-analyze security   # 安全审计
# 控制组工具
    systemd-cgls        # cgroup 树查看
    systemd-cgtop       # cgroup 资源监控
# 时间与区域
timedatectl         # 时间日期设置
localectl           # 区域语言设置

# 主机名
hostnamectl         # 主机名查看设置

# 日志管理
    journalctl --disk-usage
    journalctl --vacuum-size=500M
    journalctl --vacuum-time=7d
# 登录管理
loginctl            # 用户会话管理

# 系统管理

busctl # D-Bus 调试工具

总结

systemctl  Linux 系统管理员的瑞士军刀。掌握这些命令,你就可以对 systemd 系统做到**想查就查、想管就管**。

最常用的 10 个命令(新手必记):

systemctl start|stop|restart|status <service>   # 基础五连
systemctl enable|disable <service>               # 开机策略
systemctl list-units --type=service --all        # 查看所有服务
    journalctl -u <service>                          # 查看日志
systemctl daemon-reload                          # 重载配置
systemctl mask|unmask <service>                  # 禁止/允许
systemctl poweroff|reboot                        # 关机重启
systemctl set-default multi-user.target          # 设置运行级别
systemctl list-timers                            # 查看定时器
    systemd-analyze blame                            # 分析启动慢原因

版本信息:

本文覆盖 systemd v242——v258 的命令行接口。某些命令(如 soft-rebootreboot——firmware-setup)在较老版本中可能不可用。

━━━ ━━━ ━━━

后记: systemctl 的命令数量在每一次 systemd 大版本更新中都在增加,从 v219 的 30 多个命令增长到 v256+ 的 80+ 个。本文收录了所有主要的 systemctl 命令及子命令,如有遗漏,欢迎补充。

━━━ ━━━ ━━━

*欢迎关注 LeisureLinux,获取更多深度技术解读。*

手搓微型 Linux 发行版:从零开始打造你的专属系统

Flatpak 架构变更:强耦合 Systemd 的技术深度解析

规范重构:Linux 主目录标准化演进与 XDG 的"名分"之争

【实战】解决 Debian 13 桌面频繁崩溃:从显卡驱动到 D-Bus 的深度排坑

本文整理自微信公众号 LeisureLinux 的原创内容(Linux / AI / 安全 硬核技术)。

· 阅读原文(微信公众号)

· 关注公众号 LeisureLinux,第一时间获取技术深度内容。

LeisureLinux 公众号二维码(微信扫一扫关注)