Skip to main content
☘️ Septvean's Documents
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

systemctl

一、systemctl 是什么(一句话)

systemctl 是 systemd 的统一管理入口,用于控制、查询和调试 Linux 系统中的服务(unit)、启动流程和系统状态。


二、systemd / systemctl 核心概念速览

1️⃣ Unit(单元)

systemd 管理的一切都是 Unit

类型 后缀 说明
Service .service 服务(最常用)
Socket .socket 套接字激活
Target .target 启动阶段 / 逻辑分组
Timer .timer 定时任务(替代 cron)
Mount .mount 挂载点
Path .path 文件路径监控
Device .device 设备
Slice .slice 资源控制组
Scope .scope 外部进程

2️⃣ systemctl 管什么?

┌────────────┐
│ systemctl  │
└─────┬──────┘
 ┌────▼──────────────────────┐
   systemd (PID 1)
    ├─ unit 生命周期管理
    ├─ 依赖关系 (After/Wants)
    ├─ 资源控制 (cgroups)
    ├─ 日志 (journald)
 └───────────────────────────┘

三、最常用 systemctl 命令(必须会)

1️⃣ 服务控制(service)

systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx      # 不重启,重载配置

restart vs reload

  • restart:进程重启
  • reload:发送信号(如 HUP),不中断服务

2️⃣ 开机自启控制

systemctl enable nginx      # 开机自启
systemctl disable nginx
systemctl is-enabled nginx

本质:创建 / 删除 符号链接


3️⃣ 查看状态(最常用)

systemctl status nginx

你能看到:

  • Active 状态
  • Main PID
  • 最近日志
  • 失败原因(exit code)

4️⃣ 列出服务

systemctl list-units --type=service
systemctl list-units --state=failed
systemctl list-unit-files
命令 作用
list-units 当前加载
list-unit-files 磁盘上存在

四、Target(启动级别)管理

1️⃣ 查看当前启动目标

systemctl get-default

常见:

target 说明
multi-user.target 命令行服务器
graphical.target 桌面
rescue.target 单用户
emergency.target 紧急模式

2️⃣ 修改默认启动级别

systemctl set-default multi-user.target

3️⃣ 切换运行级别(立即生效)

systemctl isolate rescue.target

五、Unit 文件管理(非常重要)

1️⃣ unit 文件加载顺序(核心)

/etc/systemd/system/        # 管理员自定义(最高优先级)
/run/systemd/system/        # 运行时生成
/usr/lib/systemd/system/    # 发行版提供

永远不要直接改 /usr/lib


2️⃣ 修改 unit 的正确方式(强烈推荐)

编辑覆盖配置

systemctl edit nginx

生成:

/etc/systemd/system/nginx.service.d/override.conf

3️⃣ 修改后必须执行

systemctl daemon-reload
命令 含义
daemon-reload 重新加载 unit 文件
daemon-reexec 重新执行 systemd(更重)

六、Socket / Timer(进阶)

1️⃣ Socket 激活

systemctl status rpcbind.socket
systemctl start rpcbind.socket

特点:

  • 服务 按需启动
  • socket 先监听,service 后启动

2️⃣ Timer(替代 cron)

systemctl list-timers

启用 timer:

systemctl enable --now backup.timer

七、日志与排障(运维必备)

1️⃣ 结合 journalctl

journalctl -u nginx
journalctl -u nginx -f
journalctl -xe

2️⃣ 常见失败原因速查

现象 可能原因
status=203/EXEC ExecStart 路径错误
status=1/FAILURE 程序自身错误
core-dump 程序 panic / assert
Failed at step 权限 / SELinux

3️⃣ 查看 unit 解析结果(高级)

systemd-analyze verify nginx.service

八、资源控制(systemctl + cgroups)

systemd 是 cgroups 的统一入口

[Service]
MemoryMax=1G
CPUQuota=50%
TasksMax=200

查看:

systemctl status nginx
systemd-cgtop

九、systemctl 与容器 / Podman / runc 的关系

  • systemctl 不直接管理容器

  • 但可以:

    • 管理 podman unit
    • 管理 cri-o
    • 管理 kubelet

例:

systemctl enable podman@mycontainer

十、常见误区(非常重要)

  • ❌ 直接修改 /usr/lib/systemd/system/*.service
  • ❌ 忘记 daemon-reload
  • ❌ 不区分 enablestart
  • ❌ 用 kill 管服务而不是 systemctl

十一、一句话心智模型(记住这个)

systemctl 是 systemd 的遥控器,systemd 是 Linux 上“进程 + 启动 + 资源 + 依赖”的统一调度器。


十二、速查表(收藏级)

## 服务
systemctl start|stop|restart|reload xxx
systemctl status xxx

## 开机自启
systemctl enable|disable xxx

## unit 管理
systemctl daemon-reload
systemctl edit xxx

## 启动级别
systemctl get-default
systemctl set-default multi-user.target

## 日志
journalctl -u xxx -f

## 排障
systemd-analyze blame
systemd-analyze critical-chain

systemctl --type= 全部参数(Unit 类型)

--type 用于 按 Unit 类型过滤

systemctl list-units --type=TYPE
systemctl list-unit-files --type=TYPE

✅ 全部合法类型

type unit 后缀 说明
service .service 服务(最常用)
socket .socket 套接字激活
target .target 启动目标 / 分组
timer .timer 定时器(cron 替代)
mount .mount 挂载点
automount .automount 自动挂载
path .path 路径监控
device .device 设备
swap .swap swap 分区
slice .slice cgroup 资源切片
scope .scope 外部进程组

🔹 示例

systemctl list-units --type=service
systemctl list-units --type=socket
systemctl list-units --type=timer

systemctl --state= 全部参数(Unit 状态)

--state 用于 按运行状态过滤

systemctl list-units --state=STATE

1️⃣ 活跃态(Active States)

state 含义
active 正在运行
inactive 未运行
activating 正在启动
deactivating 正在停止
reloading 正在 reload

示例

systemctl list-units --state=active
systemctl list-units --state=activating

2️⃣ 失败态(Failed)

state 含义
failed 启动或运行失败
systemctl list-units --state=failed

3️⃣ Unit 文件状态(list-unit-files 专用)

⚠️ 只用于 list-unit-files

systemctl list-unit-files --state=STATE
state 含义
enabled 已设置开机自启
disabled 未设置自启
static 不能单独启用(被依赖)
masked 被完全屏蔽
generated systemd 自动生成
transient 临时 unit
indirect 间接启用
bad unit 文件损坏

4️⃣ 特殊状态(较少使用但存在)

state 说明
plugged 设备已就绪(device)
mounted 已挂载(mount)
waiting 等待条件(path / automount)

--type + --state 常用组合(实战)

1️⃣ 查看失败的服务

systemctl list-units --type=service --state=failed

2️⃣ 查看正在启动的服务(排卡启动)

systemctl list-units --state=activating

3️⃣ 查看所有定时任务

systemctl list-units --type=timer
systemctl list-timers

4️⃣ 查看所有 socket 激活服务

systemctl list-units --type=socket

5️⃣ 查看所有已启用但未运行的服务

systemctl list-unit-files --state=enabled
systemctl list-units --type=service --state=inactive

容易混淆的点(重点)

list-units vs list-unit-files

命令 看什么
list-units 当前 已加载 / 运行态
list-unit-files 磁盘上的 unit 文件

active ≠ running

  • socket / target 也可以是 active
  • 不一定有进程

完整速查表(收藏)

## 所有 unit 类型
systemctl list-units --type=service
systemctl list-units --type=socket
systemctl list-units --type=timer
systemctl list-units --type=mount
systemctl list-units --type=target

## 状态过滤
systemctl list-units --state=active
systemctl list-units --state=failed
systemctl list-units --state=activating

## unit 文件状态
systemctl list-unit-files --state=enabled
systemctl list-unit-files --state=masked

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

# 仅查看当前正在运行的服务
# 不一定包含所有“已启用”的服务
systemctl list-units --type=service --state=running

# 查看特定服务是否启用
systemctl is-enabled nginx

判断服务是否在运行

要判断 systemd 服务是否在运行,最常用和推荐的方法是使用 systemctl is-active <服务名称> 查看是否 active,或者用 systemctl status <服务名称> 获取详细信息,其中关键是查看 Active: 状态是 active (running)。脚本中常结合 if systemctl is-active --quiet <服务名称>; then ... fi 来判断并执行后续操作。

常用命令

systemctl status <服务名称>: 查看服务详细状态。

  • Active: active (running) 表示服务正在运行。
  • Active: inactive (dead) 表示服务已停止。
  • Active: failed 表示服务启动失败。
  • Active: activatingdeactivating 表示服务正在启动或停止中。

systemctl is-active <服务名称>: 仅返回服务的运行状态(active 或 inactive),适合在脚本中判断。

  • –quiet 选项 (如 systemctl is-active –quiet) 则不输出,只返回退出码,更适合自动化脚本。

systemctl list-units –type=service –state=running: 列出所有正在运行的服务。

pgrep <服务名>/pidof <服务名>: (辅助方式)通过进程名或 PID 检查,不依赖 systemctl 状态,但 systemctl 检查更全面。

示例:Shell脚本判断

#!/bin/bash
SERVICE_NAME="nginx" # 替换成你的服务名

if systemctl is-active --quiet "$SERVICE_NAME"; then
    echo "服务 $SERVICE_NAME 正在运行。"
else
    echo "服务 $SERVICE_NAME 未运行,正在尝试启动..."
    sudo systemctl start "$SERVICE_NAME"
fi

选择 systemctl is-active 进行判断,在脚本中获取服务的活动状态,是判断 systemd 服务运行情况最简洁高效的方法。


总结

--type 决定“看哪类 unit”,--state 决定“看什么状态”,两者组合就是 systemd 的核心过滤能力。


参考文档