「systemd」 – 创建和修改单元文件

问题描述

作为一个系统管理员,要遵循系统管理规范,而不能说”能用就行“。本文将介绍如何编写systemd单元文件。

注意事项

这不是一个详细的教程。这篇文章是在我学习如何编写systemd单元文件时的一个摘要笔记。文章的内容只是一个概括,涵盖了创建单元文件可能用到的知识点和相关的参考手册,并不涉及太多的细节,细节上的东西只能去读文档了。

单元文件的存储位置

单元文件的保存位置:Table 10.2, “Systemd Unit Files Locations

/usr/lib/systemd/system/
Systemd unit files distributed with installed RPM packages.

/run/systemd/system/
Systemd unit files created at run time. This directory takes precedence over the directory with installed service unit files.

/etc/systemd/system/
Systemd unit files created by systemctl enable as well as unit files added for extending a service. This directory takes precedence over the directory with runtime unit files. the /etc/systemd/system/ directory is reserved for unit files created or customized by the system administrator.

单元文件的命名方式

形如:unit_name.type_extension

unit_name:单元名。
type_extension:单元类型。可用的类型参考:Table 10.1, “Available systemd Unit Types

单元文件结构

单元文件由 [Unit]、[unit type]、[Install] 三部分组成,每个部分都包含多种不同的指令。

[Unit]

解释:包含描述、依赖等指令,是一些更通用的指令,这些指令与单元类型无关。

参考 systemd.unit/[Unit] Section Options 文档,获取 [Unit] 部分的指令说明。

[<unit type>]

参数unit type不是固定的,可以是ServiceTimer等等,它代表单元文件的类型。比如,如果服务类型的单元文件,则该部分应为[Service],而对于定时任务类型的单元文件,该部分应为[Timer]

与[Service]相关的指令可以参考Table 10.10, “Important [Service] Section Options”,或者参考system.service(5)手册。

[Install]

单元文件安装相关的信息。所谓”安装“指systemctl enablesystemctl disable命令。该部分中可用的指令参考Table 10.11, “Important [Install] Section Options

与某个单元文件相关的目录

除了直接修改单元文件外,也可以加入自定义的配置选项,但这是通过使用补充目录实现的。参考Section 10.6.4, “Modifying Existing Unit Files

也可以通过补充目录的方式创建服务的依赖项目。补充目录里保存了当前服务所依赖的服务。

单元文件中使用参数

单元文件中还可以使用命令行传入的参数。参考Section 10.6.5, “Working with Instantiated Units

修改单元文件

How do I override or configure systemd services?
Systemd delete overrides
systemd.unit — Unit configuration
How to override systemd unit file settings?
How to find out the systemd version on Raspbian

系统安装某个服务,但是需要修改它的单元文件的参数。

由于这些服务是通过包管理器安装的,比如 YUM 安装,不建议直接修改包中的文件。因为随着软件的升级,该单元文件会被覆盖或者删除,因此不应该直接修改属于软件包的文件。

比如,安装 MySQL 服务,单元文件 /usr/lib/systemd/system/mysqld.service 为包中自带的,但是我们需要调整它的配置。而不应该直接修改这个文件,因为如果升级 MySQL 版本,该文件会被覆盖。

修改文件

正确的做法是使用 systemctl edit 命令。

基于上面的例子,执行systemctl edit mysqld.service命令,该会打开编辑器,然后便能够在这其中输入自定义配置,例如:

[Service]
LimitNOFILE=65535

然后保存退出。

实际上,上述操作的背后流程就是:
1)创建 /etc/systemd/system/mysqld.service.d/override.conf 文件,并将编辑器的内容写入到 override.conf 文件中。
2)我们也可以在定义自己的.conf文件。

另外有几个需要注意的地方:
1)当使用systemctl edit命令进行修改的时候,需要指定章节,比如示例中的[Service]部分。
2)覆盖是以指令单位的。例如,上面虽然指定了[Service]部分,但是只会覆盖[Service]部分的LimitNOFILE配置。
3)如果它是一个列表,那你需要清空在定义。比如,先指定LimitNOFILE=后指令,再指定LimitNOFILE=65535指令。
4)不需要运行systemctl daemon-reload命令。

查看修改

执行命令 systemctl cat 来查看修改。

例如,执行systemctl cat mysqld.service命令,会产生类似如下的输出:

....

# /etc/systemd/system/mysqld.service.d/override.conf
[Service]
LimitNOFILE=65535

它同时输出哪些指令是来自于/usr/lib/systemd/system/mysqld.service文件,哪些是来自于/etc/systemd/system/mysqld.service.d/override.conf文件。

环境变量

环境变量的修改也是类似的方法,但是使用的是Environment指令,如同Environment=FOO=bar的形式。

还原修改

执行systemctl revert命令来还原修改。它会还原至原始版本,删除所有的覆盖,还原单元文件的属性到默认值,并且进行unmask操作。

执行systemctl revert mysqld.service命令来还原之前的修改。

注意事项,但是要高于229版本(systemctl --version),而CentOS Linux release 7.4.1708 (Core)是systemd 219版本。

注意事项

修改了单元文件之后,要运行systemctl daemon-reload命令来重新加载systemd管理配置。

相关链接

systemd.unit — Unit configuration

参考文献

Systemd 入门教程:命令篇
systemd.unit — Unit configuration
Creating and Modifying systemd Unit Files
更多的用法可以参考官方的手册,或者systemd.unt(5)手册
Do systemd unit files have to be reloaded when modified?