Systemd 是 Linux 系统工具,用来启动守护进程,已成为大多数发行版的标配。
历史背景
过去 Linux 启动一直采用init进程。下面的命令用来启动 apache2 服务
$ sudo /etc/init.d/apache2 start
# 或者
$ service apache2 start
这种方法有两个缺点:
- 启动时间长,init进程是串行启动,前一个进程启动完,才会启动下一个进程
- 启动脚本复杂,init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,使得脚本变得又臭又长
Systemd 概述
Systemd 就是为了解决这些问题而诞生的。其设计目标是——为系统的启动和管理提供一套完整的解决方案。 根据 Linux 惯例,字母d是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。
使用了 Systemd,就不需要再用 init 了。Systemd 取代了 initd,成为系统的第一个进程(PID 为 1),其他进程都是它的子进程。
$ systemctl --version # 查看 systemd 版本
Systemd 功能强大,使用方便,但是体系庞大,非常复杂。事实上,很多人反对使用 Systemd,理由就是它与操作系统的其他部分强耦合,违反“keep simple, keep stupid”的 Unix 哲学。
系统管理
Systemd 是一组命令,涉及到系统管理的各方面。
systemctl
systemctl 是 Systemd 的主命令,用于管理系统。
$ sudo systemctl reboot # 重启系统
$ sudo systemctl poweroff # 关闭系统,切断电源
$ sudo systemctl halt # CPU停止工作
$ sudo systemctl suspend # 暂停系统
$ sudo systemctl hibernate # 让系统进入冬眠状态
$ sudo systemctl hybrid-sleep # 让系统进入交互式休眠状态
$ sudo systemctl rescue # 启动进入救援状态(单用户状态)
systemd-analyze
systemd-analyze 用于查看启动耗时
$ systemd-analyze # 查看启动耗时
$ systemd-analyze blame # 查看每个服务的启动耗时
$ systemd-analyze critical-chain # 显示瀑布状的启动过程流
$ systemd-analyze critical-chain atd.service # 显示指定服务的启动流
hostnamectl
hostnamectl 用于查看当前主机信息
$ hostnamectl # 显示当前主机的信息
$ sudo hostnamectl set-hostname rhel7 # 设置主机名
localectl
localectl 用于查看本地化设置
$ localectl # 查看本地化设置
$ sudo localectl set-locale LANG=en_GB.utf8 # 设置本地化参数
$ sudo localectl set-keymap en_GB
timedatectl
timedatectl 用于查看当前时区设置
$ timedatectl # 查看当前时区设置
$ timedatectl list-timezones # 显示所有可用的时区
$ sudo timedatectl set-timezone America/New_York # 设置当前时区
$ sudo timedatectl set-time YYYY-MM-DD
$ sudo timedatectl set-time HH:MM:SS
loginctl
loginctl 用于查看当前登录的用户
$ loginctl list-sessions # 列出当前session
$ loginctl list-users # 列出当前登录用户
$ loginctl show-user ruanyf # 列出显示指定用户的信息
Unit
Systemd 可以管理所有系统资源。不同的资源统称为 Unit(单位)。 Unit 共分为 12 种:
Service unit:系统服务
Target unit:多个 Unit 构成的一个组
Device Unit:硬件设备
Mount Unit:文件系统的挂载点
Automount Unit:自动挂载点
Path Unit:文件或路径
Scope Unit:不是由 Systemd 启动的外部进程
Slice Unit:进程组
Snapshot Unit:Systemd 快照,可以切回某个快照
Socket Unit:进程间通信的 socket
Swap Unit:swap 文件
Timer Unit:定时器
systemctl list-units命令可以查看当前系统的所有 Unit 。
$ systemctl list-units # 列出正在运行的 Unit
$ systemctl list-units --all # 列出所有Unit,包括没有找到配置文件的或者启动失败的
$ systemctl list-units --all --state=inactive # 列出所有没有运行的 Unit
$ systemctl list-units --failed # 列出所有加载失败的 Unit
$ systemctl list-units --type=service # 列出所有正在运行的、类型为 service 的 Unit
Unit 的状态
systemctl status 用于查看系统状态和单个 Unit 的状态
$ systemctl status # 显示系统状态
$ sysystemctl status bluetooth.service # 显示单个 Unit 的状态
$ systemctl -H root@rhel7.example.com status httpd.service # 显示远程主机的某个 Unit 的状态
除了status命令,systemctl还提供了三个查询状态的简单方法,主要供脚本内部的判断语句使用。
$ systemctl is-active application.service # 显示某个 Unit 是否正在运行
$ systemctl is-failed application.service # 显示某个 Unit 是否处于启动失败状态
$ systemctl is-enabled application.service # 显示某个 Unit 服务是否建立了启动链接
Unit 管理
以下命令用于启动和停止 Unit(主要是 service)
$ sudo systemctl start apache.service # 立即启动一个服务
$ sudo systemctl stop apache.service # 立即停止一个服务
$ sudo systemctl restart apache.service # 重启一个服务
$ sudo systemctl kill apache.service # 杀死一个服务的所有子进程
$ sudo systemctl reload apache.service # 重新加载一个服务的配置文件
$ sudo systemctl daemon-reload # 重载所有修改过的配置文件
$ systemctl show httpd.service # 显示某个 Unit 的所有底层参数
$ systemctl show -p CPUShares httpd.service # 显示某个 Unit 的指定属性的值
$ sudo systemctl set-property httpd.service CPUShares=500 # 设置某个 Unit 的指定属性
依赖关系
Unit 之间存在依赖关系:A 依赖于 B,就意味着 Systemd 在启动 A 的时候,同时会去启动 B。
systemctl list-dependencies 出一个 Unit 的所有依赖
$ systemctl list-dependencies nginx.service
上面命令的输出结果之中,有些依赖是 Target 类型,默认不展开显示。如果要展开 Target,需要使用--all参数。
Unit 的配置文件
概述
每一个 Unit 都有一个配置文件,告诉 Systemd 怎么启动这个 Unit
Systemd 默认从目录 /etc/systemd/system/ 读取配置文件。里面存放的大部分文件都是符号链接,指向目录 /lib/systemd/system/(真正的配置文件存放的目录)
systemctl enable命令用于在上面两个目录之间建立符号链接关系
$ sudo systemctl enable xxx.service # 等同于
$ sudo ln -s '/usr/lib/systemd/system/xxx.service' '/etc/systemd/system/multi-user.target.wants/xxx.service'
如果配置文件里面设置了开机启动,systemctl enable 相当于激活开机启动。 与之对应的,systemctl disable 用于在两个目录之间撤销符号链接关系,相当于撤销开机启动。
配置文件的后缀名就是该 Unit 的种类,比如sshd.socket。缺省值为.service,所以 sshd 会被理解为 sshd.service
配置文件的状态
systemctl list-unit-files 用于列出所有配置文件
$ systemctl list-unit-files # 列出所有配置文件
$ systemctl list-unit-files --type=service # 列出指定类型的配置文件
这个命令会输出一个列表
$ systemctl list-unit-files
UNIT FILE STATE
chronyd.service enabled
clamd@.service static
clamd@scan.service disabled
这个列表显示每个配置文件的状态,一共有四种。
enabled:已建立启动链接
disabled:没建立启动链接
static:该配置文件没有[Install]部分(无法执行),只能作为其他配置文件的依赖
masked:该配置文件被禁止建立启动链接
Note:从配置文件状态无法看出该 Unit 是否运行中。这必须执行前面提到的systemctl status命令。
$ systemctl status bluetooth.service
一旦修改配置文件,就要让 Systemd 重新加载配置文件,然后重新启动,否则修改不会生效。
$ sudo systemctl daemon-reload
$ sudo systemctl restart httpd.service
配置文件的格式
配置文件就是普通的文本文件。 systemctl cat 可查看配置文件的内容。
$ systemctl cat atd.service
[Unit]
Description=ATD daemon
[Service]
Type=forking
ExecStart=/usr/bin/atd
[Install]
WantedBy=multi-user.target
每个区块内部是一些等号连接的 key=value 对,键值对的等号两侧不能有空格
[Section]
Directive1=value
Directive2=value
...
配置文件的区块
[Unit]
配置文件的第一个区块,用来定义 Unit 的元数据以及配置与其他 Unit 的关系。它的主要字段如下:
Description:简短描述
Documentation:文档地址
Requires:当前 Unit 依赖的其他 Unit,如果它们没有运行,当前 Unit 会启动失败
Wants:与当前 Unit 配合的其他 Unit,如果它们没有运行,当前 Unit 不会启动失败
BindsTo:与Requires类似,它指定的 Unit 如果退出,会导致当前 Unit 停止运行
Before:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之后启动
After:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之前启动
Conflicts:这里指定的 Unit 不能与当前 Unit 同时运行
Condition...:当前 Unit 运行必须满足的条件,否则不会运行
Assert...:当前 Unit 运行必须满足的条件,否则会报启动失败
[Install]
最后一个区块,用来定义如何启动,以及是否开机启动。主要字段如下:
WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入 /etc/systemd/system 目录下以 Target 名 + .wants 后缀构成的子目录中
RequiredBy:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入 /etc/systemd/system 目录下以 Target 名 + .required 后缀构成的子目录中
Alias:当前 Unit 可用于启动的别名
Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit
[Service]
Service 的配置,只有 Service 类型的 Unit 才有这个区块
Type:定义启动时的进程行为。它有以下几种值。
Type=simple:默认值,执行ExecStart指定的命令,启动主进程
Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出
Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
Type=dbus:当前服务通过D-Bus启动
Type=notify:当前服务启动完毕,会通知Systemd,再继续往下执行
Type=idle:若有其他任务执行完毕,当前服务才会运行
ExecStart:启动当前服务的命令
ExecStartPre:启动当前服务之前执行的命令
ExecStartPost:启动当前服务之后执行的命令
ExecReload:重启当前服务时执行的命令
ExecStop:停止当前服务时执行的命令
ExecStopPost:停止当其服务之后执行的命令
RestartSec:自动重启当前服务间隔的秒数
Restart:定义何种情况 Systemd 会自动重启当前服务,可能的值包括 always(总是重启)、on-success、on-failure、on-abnormal、on-abort、on-watchdog
TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数
Environment:指定环境变量
Target
启动计算机的时候,需要启动大量的 Unit。Target 即一个 Unit 组,包含许多相关的 Unit。启动某个 Target 时,Systemd 就会启动里面所有的 Unit。Target 这个概念类似于"状态点",启动某个 Target 就好比启动到某种状态。
与传统的 init 启动模式中 RunLevel 的概念不同的是,RunLevel 是互斥的,不可能多个 RunLevel 同时启动,但是多个 Target 可以同时启动。
# 查看当前系统的所有 Target
$ systemctl list-unit-files --type=target
# 查看一个 Target 包含的所有 Unit
$ systemctl list-dependencies multi-user.target
# 查看启动时的默认 Target
$ systemctl get-default
# 设置启动时的默认 Target
$ sudo systemctl set-default multi-user.target
# 切换 Target 时,默认不关闭前一个 Target 启动的进程,
# systemctl isolate 命令改变这种行为,
# 关闭前一个 Target 里面所有不属于后一个 Target 的进程
$ sudo systemctl isolate multi-user.target
Target 与 传统 RunLevel 的对应关系如下:
Traditional runlevel New target name Symbolically linked to...
Runlevel 0 | runlevel0.target -> poweroff.target
Runlevel 1 | runlevel1.target -> rescue.target
Runlevel 2 | runlevel2.target -> multi-user.target
Runlevel 3 | runlevel3.target -> multi-user.target
Runlevel 4 | runlevel4.target -> multi-user.target
Runlevel 5 | runlevel5.target -> graphical.target
Runlevel 6 | runlevel6.target -> reboot.target
它与 init 进程的主要差别:
- 默认的 RunLevel 现在被默认的 Target 取代,位置是 /etc/systemd/system/default.target,通常符号链接到 graphical.target(图形界面)或者 multi-user.target(多用户命令行)。
- 启动脚本的位置,以前是 /etc/init.d 目录,符号链接到不同的 RunLevel 目录 (比如 /etc/rc3.d、/etc/rc5.d 等),现在则存放在 /lib/systemd/system 和 /etc/systemd/system 目录。
- 配置文件的位置,以前 init 下各种服务的配置文件放在 /etc/sysconfig 。现在的配置文件放在 /lib/systemd 目录,在 /etc/systemd 目录里面的修改可以覆盖原始设置。
参考资料:www.ruanyifeng.com/blog/2016/0… www.ruanyifeng.com/blog/2016/0…