# Systemd

**systemd** 是一套 Linux 系统的基础构建模块，提供以 PID 1 运行的系统与服务管理器，即内核启动的第一个进程，系统的其余部分由它启动。<sup>[1](https://systemd.io/)</sup> 服务可通过套接字和 [D-Bus](https://www.edgepedia.org/zh/d-bus) 激活启动，守护进程可按需启动，进程由 Linux 控制组（cgroups）跟踪，系统高度并行化，管理挂载点和自动挂载点，并对服务控制采用事务性、基于依赖的逻辑。<sup>[1](https://systemd.io/)</sup> 该套件还包括日志守护进程，用于主机名、日期和区域设置等基本系统配置的工具，登录用户、容器和虚拟机、系统账户、运行时目录的管理，以及用于网络配置、网络时间同步、日志转发和名称解析的守护进程。<sup>[1](https://systemd.io/)</sup>

| 关键事实 | 详情 |
|---|---|
| 角色 | 以 PID 1 运行并启动系统其余部分的系统与服务管理器；在用户模式下还会为每个用户会话运行一个实例<sup>[1](https://systemd.io/)</sup><sup> • </sup><sup>[2](https://systemd.io/ARCHITECTURE/)</sup> |
| 配置 | 采用 INI 语法的声明式单元文件，从按优先级排序的搜索路径加载；/etc/systemd/system 中的管理员单元优先于运行时单元和软件包随附单元<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> |
| 单元类型 | 11 种类型，包括 .service、.socket、.timer、.mount、.path、.device、.swap、.slice、.scope、.target 和 .automount<sup>[4](https://manpages.debian.org/trixie/systemd/systemd.1.en.html)</sup><sup> • </sup><sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> |
| 进程隔离 | 每个派生进程都被放入一个以其单元命名的独立 Linux 控制组，使内核能够隔离服务并分配资源<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup><sup> • </sup><sup>[5](https://wiki.debian.org/systemd)</sup> |
| 激活方式 | 套接字和 D-Bus 激活、守护进程按需启动、基于路径和基于定时器的激活<sup>[1](https://systemd.io/)</sup><sup> • </sup><sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> |
| 默认状态 | 自 Debian 8（“jessie”，2015 年发布）起，Debian 中的默认 init 系统和服务管理器<sup>[5](https://wiki.debian.org/systemd)</sup><sup> • </sup><sup>[6](https://www.debian.org/doc/debian-policy/ch-opersys.html)</sup> |
| 主要工具 | systemctl 用于检视和控制管理器，journalctl 用于查询 systemd-journald 写入的日志<sup>[7](https://manpages.debian.org/trixie/systemd/systemctl.1.en.html)</sup><sup> • </sup><sup>[8](https://manpages.ubuntu.com/manpages/jammy/man1/journalctl.1.html)</sup> |

## 启动时如何工作

Debian 系统启动时，符号链接到 systemd 的 /usr/sbin/init 会作为 init 系统进程以 PID 1 启动，其所有者为 root（UID 0）。<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> init 进程根据以声明式风格编写的单元配置文件并行派生进程，而不是采用 SysV init 脚本的过程式风格。<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> 每个派生进程都被放入 systemd 私有 cgroup 层级中一个以其所属单元命名的独立 Linux 控制组。<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> cgroup 是一种内核机制，它以层级方式组织进程，并沿该层级分配系统资源；每个进程恰好属于一个 cgroup。<sup>[9](https://cdn.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html)</sup>

**启动目标。** 启动时，systemd 会尝试启动 /lib/systemd/system/default.target，在桌面安装中通常符号链接到 graphical.target。<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup><sup> • </sup><sup>[10](https://wiki.debian.org/systemd/Unit)</sup> local-fs.target、swap.target 和 cryptsetup.target 等特殊目标单元会先被拉入以挂载文件系统，随后其他目标由目标单元依赖拉入。<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> 目标是单元的一种类型，它把其他单元配置文件分组，以在启动期间创建一个同步点。<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup>

## 单元文件与依赖

单元文件使用 INI 语法，节标题放在方括号中，后跟变量声明；一个服务单元通常包含 [Unit]、[Service] 和 [Install] 节，最后一节告诉 systemd 该单元应在哪个目标下启动。<sup>[10](https://wiki.debian.org/systemd/Unit)</sup><sup> • </sup><sup>[11](https://wiki.debian.org/systemd/Services)</sup> 文件后缀编码单元类型：.service 描述受监督的进程，.socket 描述用于基于套接字激活的套接字，.timer 描述用于基于定时器激活的定时器，.path 描述用于基于路径激活的受监控路径，.mount 和 .automount 描述文件系统挂载点，.swap 描述交换设备或交换文件，.device 描述在 sysfs/udev 设备树中暴露的设备，.slice 描述基于 cgroup 的资源管理器，.scope 描述以编程方式创建的系统进程组，.target 描述将其他单元分组的同步点。<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup>

**依赖与排序。** 单元之间的依赖通过 Wants=、Requires=、Before= 和 After= 等指令声明。<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> Wants= 会随配置单元一起启动所列单元，若某个所列单元失败，整个事务不受影响；Requires= 声明更强的依赖，若某个必需单元未能激活，且同时设置了对其的 After= 依赖，则配置单元不会启动。<sup>[12](https://manpages.debian.org/bullseye/systemd/systemd.unit.5.en.html)</sup> [需求](https://www.edgepedia.org/zh/requirement)依赖不影响服务启动顺序；顺序通过 After= 和 Before= 独立配置，其中 After= 确保所列单元在配置单元启动之前完全启动。<sup>[12](https://manpages.debian.org/bullseye/systemd/systemd.unit.5.en.html)</sup> 当两个具有顺序依赖的单元关闭时，适用与启动顺序相反的顺序。<sup>[12](https://manpages.debian.org/bullseye/systemd/systemd.unit.5.en.html)</sup>

单元从按优先级排序的搜索路径加载。在系统模式下，主要目录按优先级依次为 /etc/systemd/system（管理员单元）、/run/systemd/system（运行时单元）和 /lib/systemd/system（由发行版包管理器安装的单元）。<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> 较高优先级目录中的单元文件优先于任何同名的默认单元文件。<sup>[11](https://wiki.debian.org/systemd/Services)</sup>

## 服务配置

服务单元规定守护进程如何启动、受监督和重启。Type= 设置描述 systemd 如何判定服务已启动；支持的值包括 simple、forking、oneshot、dbus、notify 和 idle。<sup>[13](https://cgit.freedesktop.org/systemd/systemd-stable/tree/src/core/service.c?h=v208-stable)</sup> 对于不自行转入后台的守护进程，Type=notify 使用基于 sd_notify(3) 的简单通知协议，守护进程借此告诉 systemd 它已完成初始化；systemd 会让该单元保持在“starting”状态，直到就绪通知到达；若守护进程始终不发送通知，则在超时后杀死该服务。<sup>[14](https://manpages.debian.org/bookworm-backports/systemd/systemd.service.5.en.html)</sup> Type=oneshot 让管理器等到配置的命令完成后才报告该单元已完成；当其他单元必须在一个脚本完成其工作后才运行时，这一点很重要。<sup>[15](https://lists.freedesktop.org/archives/systemd-devel/2024-April/050214.html)</sup>

Restart= 设置控制服务在其进程退出、被杀死或达到超时时是否重启；可取值为 no（默认）、on-success、on-failure、on-abnormal、on-watchdog、on-abort 或 always；由 systemd 自身操作（例如停止或重启请求）导致的终止不会触发重启。<sup>[16](https://manpages.debian.org/unstable/systemd/systemd.service.5.en.html)</sup>

**沙箱。** 单元设置可以限制服务能接触什么。ProtectSystem= 会为该单元的进程以只读方式挂载 /usr 和引导加载程序目录，在“full”级别下还会加入 /etc，在“strict”级别下则除 /dev、/proc 和 /sys 外将整个层级以只读方式挂载；ProtectHome= 使 /home、/root 和 /run/user 不可访问、只读，或用只读 tmpfs 覆盖；PrivateTmp= 为服务提供私有的 /tmp 和 /var/tmp 命名空间；NoNewPrivileges= 防止进程通过 setuid 或 setgid 位或文件系统能力获得新特权。<sup>[17](https://manpages.debian.org/trixie/systemd/systemd.exec.5.en.html)</sup> 建议对长期运行的服务，尤其是面向网络的服务使用这些设置。<sup>[17](https://manpages.debian.org/trixie/systemd/systemd.exec.5.en.html)</sup>

## 套接字与 D-Bus 激活

systemd 支持在连接到达时启动服务，而不是在系统引导时启动。一个 .socket 单元监听一个套接字，关联的服务按需启动。<sup>[1](https://systemd.io/)</sup><sup> • </sup><sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> 套接字通过文件描述符传递给守护进程，并通过 $LISTEN_FDS 环境变量告知；D-Bus 规范描述了一种 systemd 传输方式，其中由套接字激活派生的进程从 systemd 获取这些描述符，并将其用作自己的 D-Bus 传输。<sup>[18](https://dbus.freedesktop.org/doc/dbus-specification.html)</sup> 独立的 systemd-socket-activate 工具可以从命令行启动一个由套接字激活的服务以进行测试，并按原始位置传递套接字。<sup>[19](https://www.freedesktop.org/software/systemd/man/252/systemd-socket-activate.html)</sup>

## 网络排序目标

有三个目标单元承担传统 $network 依赖在 init 脚本中所扮演的角色。network.target 在启动期间意义不大；它的主要用途是关闭顺序，因为关闭顺序与启动顺序相反，所以一个设置为 After=network.target 的单元会在网络关闭之前停止。它是一个被动单元，由网络管理服务自身拉入；使用网络的服务应只设置 After=network.target 依赖，避免对其使用 Wants= 或 Requires=。<sup>[20](https://wiki.freedesktop.org/www/Software/systemd/NetworkTarget/)</sup> network-online.target 是一个主动单元，它会等待网络达到网络管理软件所定义的“up”状态，并在 90 秒后超时；严格需要已配置网络的服务应同时 Want= 它并将自身排序为 After= 它，且还必须启用相应的等待服务，例如 NetworkManager-wait-online.service 或 systemd-networkd-wait-online.service。<sup>[20](https://wiki.freedesktop.org/www/Software/systemd/NetworkTarget/)</sup> network-pre.target 将服务（例如防火墙）的启动排在任何网络接口的配置之前。<sup>[20](https://wiki.freedesktop.org/www/Software/systemd/NetworkTarget/)</sup>

## 用户会话

除系统管理器外，systemd 还会为每个用户会话运行一个服务管理器实例。当用户通过 gdm3、sshd 或类似方式登录 Debian 系统时，/lib/systemd/system --user 会作为该用户拥有的用户服务管理器进程启动，并以与系统管理器相同的方式从声明式单元文件并行派生进程。<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> 用户单元从按优先级排序的搜索路径加载：~/.config/systemd/user（用户配置）、/etc/systemd/user（管理员单元）、/run/systemd/user（运行时单元）和 /lib/systemd/user（软件包安装的单元）。<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> systemctl 工具可以操作任一管理器，--system 是隐含的默认值，--user 则与调用用户的管理器通信。<sup>[7](https://manpages.debian.org/trixie/systemd/systemctl.1.en.html)</sup>

## 控制与检视系统

systemctl 命令检视并控制系统与服务管理器的状态。其 status 命令显示整个系统或单个单元的运行时状态以及最近的日志条目；start、stop、restart 和 reload 操作单元；daemon-reload 会重新运行所有生成器、重新加载所有单元文件并重建依赖树，这与针对单个服务的 reload 命令不同。<sup>[7](https://manpages.debian.org/trixie/systemd/systemctl.1.en.html)</sup> enable 命令会创建单元文件 [Install] 节中编码的符号链接并重新加载管理器配置，但除非与 --now 组合使用，否则不会启动该单元。<sup>[7](https://manpages.debian.org/trixie/systemd/systemctl.1.en.html)</sup> 要在不直接编辑软件包随附单元的情况下自定义它，systemctl edit <service> 会创建一个覆盖用 drop-in 文件（例如 /etc/systemd/system/apache2.service.d/override.conf）并打开编辑器。<sup>[21](https://ubuntu.com/server/docs/changing-package-files/)</sup> 日志通过 journalctl 查询，它读取 systemd-journald.service 写入的日志，并可按单元过滤，例如 journalctl _SYSTEMD_UNIT=avahi-daemon.service。<sup>[8](https://manpages.ubuntu.com/manpages/jammy/man1/journalctl.1.html)</sup><sup> • </sup><sup>[22](https://manpages.ubuntu.com/manpages/resolute/man1/journalctl.1.html)</sup>

## 兼容性与采用

systemd 提供与其所取代的 SysV init 系统之间的向后兼容性。/etc/init.d/rc[0123456S].d/[KS]name 中的 SysV 风格启动脚本仍会被解析，telinit(8) 请求会被转换为 systemd 单元激活请求；模拟的运行级别 2 到 4 都符号链接到同一个 multi-user.target。<sup>[3](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)</sup> 如果 /etc/rc.local 存在且可执行，生成器 systemd-rc-local-generator 会将 rc-local.service 拉入启动过程；这是针对特定 System V 系统的兼容性功能，文档建议避免使用它，而改用适当的单元文件。<sup>[23](https://www.freedesktop.org/software/systemd/man/257/rc-local.service.html)</sup>

Debian 在 2015 年随 Debian 8（“jessie”）采用 systemd 作为默认 init 系统，Debian 政策要求提供系统服务的软件包包含 systemd 服务单元；在常见的单服务情形下，单元以软件包名命名并加上 .service 扩展名。<sup>[5](https://wiki.debian.org/systemd)</sup><sup> • </sup><sup>[6](https://www.debian.org/doc/debian-policy/ch-opersys.html)</sup> Debian 和 Ubuntu 上的许多软件包都随附服务单元文件，这些文件放在 /lib/systemd/system/ 中。<sup>[21](https://ubuntu.com/server/docs/changing-package-files/)</sup> Debian 中仍可使用替代 init 系统；在 Bullseye 版本中，对 systemd 以外 init 系统的支持得到显著改善。<sup>[24](https://wiki.debian.org/Init)</sup>

在内部，当启动某个单元需要派生新进程时，管理器会序列化该单元的配置，并通过 posix_spawn() 调用以 systemd-executor 二进制程序创建进程；该程序反序列化配置，并在 exec 配置的可执行文件之前应用沙箱、命名空间和 cgroup 成员资格等选项。<sup>[2](https://systemd.io/ARCHITECTURE/)</sup>

## References

1. [System and Service Manager, systemd.io](https://systemd.io/)
2. [systemd Repository Architecture, systemd.io](https://systemd.io/ARCHITECTURE/)
3. [Chapter 3. The system initialization, Debian Reference](https://www.debian.org/doc/manuals/debian-reference/ch03.en.html)
4. [systemd(1), Debian Manpages](https://manpages.debian.org/trixie/systemd/systemd.1.en.html)
5. [systemd, Debian Wiki](https://wiki.debian.org/systemd)
6. [9. The Operating System, Debian Policy Manual](https://www.debian.org/doc/debian-policy/ch-opersys.html)
7. [systemctl(1), Debian Manpages](https://manpages.debian.org/trixie/systemd/systemctl.1.en.html)
8. [journalctl(1), Ubuntu Manpages](https://manpages.ubuntu.com/manpages/jammy/man1/journalctl.1.html)
9. [Control Group v2, The Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html)
10. [systemd/Unit, Debian Wiki](https://wiki.debian.org/systemd/Unit)
11. [systemd/Services, Debian Wiki](https://wiki.debian.org/systemd/Services)
12. [systemd.unit(5), Debian Manpages (bullseye)](https://manpages.debian.org/bullseye/systemd/systemd.unit.5.en.html)
13. [src/core/service.c, systemd source tree](https://cgit.freedesktop.org/systemd/systemd-stable/tree/src/core/service.c?h=v208-stable)
14. [systemd.service(5), Debian Manpages (bookworm-backports)](https://manpages.debian.org/bookworm-backports/systemd/systemd.service.5.en.html)
15. [How to chain services driven by a timer?, systemd-devel mailing list](https://lists.freedesktop.org/archives/systemd-devel/2024-April/050214.html)
16. [systemd.service(5), Debian Manpages (unstable)](https://manpages.debian.org/unstable/systemd/systemd.service.5.en.html)
17. [systemd.exec(5), Debian Manpages (trixie)](https://manpages.debian.org/trixie/systemd/systemd.exec.5.en.html)
18. [D-Bus Specification](https://dbus.freedesktop.org/doc/dbus-specification.html)
19. [systemd-socket-activate(1), freedesktop.org](https://www.freedesktop.org/software/systemd/man/252/systemd-socket-activate.html)
20. [NetworkTarget, freedesktop.org wiki](https://wiki.freedesktop.org/www/Software/systemd/NetworkTarget/)
21. [Changing package files, Ubuntu Server documentation](https://ubuntu.com/server/docs/changing-package-files/)
22. [journalctl(1), Ubuntu Manpages (resolute)](https://manpages.ubuntu.com/manpages/resolute/man1/journalctl.1.html)
23. [systemd-rc-local-generator(8), freedesktop.org](https://www.freedesktop.org/software/systemd/man/257/rc-local.service.html)
24. [Init, Debian Wiki](https://wiki.debian.org/Init)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/systemd

English original: https://www.edgepedia.org/systemd
