centos8使用systemd/systemctl管理系统/服务

一,systemd的用途?

Systemd 是 Linux 系统工具,用来启动守护进程,已成为大多数发行版的标准配置

Systemd 的优点是功能强大,使用方便,

                 缺点是体系庞大,非常复杂

 

在centos6中,系统的启动管理使用initd,

从centos7开始,systemd成为了系统的默认启动守护进程

systemctl是systemd的管理控制工具

 

附上systemd的架构图:(来自网络)

 

说明:刘宏缔的架构森林是一个专注架构的博客,

网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/21/centos8linuxsystemdsystemctl-guan-li-xi-tong-fu-wu/

         对应的源码可以访问这里获取: https://github.com/liuhongdi/

 说明:作者:刘宏缔 邮箱: 371125307@qq.com

 

二,查看systemctl所属的包

[root@blog ~]$ whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

[root@blog ~]$ rpm -qf /usr/bin/systemctl
systemd-239-13.el8_0.5.x86_64 

 

三,查看systemctl的版本和帮助

1,查看版本

[root@blog ~]$ systemctl --version
systemd 239
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT 
+UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 
+SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 
default-hierarchy=legacy

 

2,查看帮助

[root@blog ~]$ systemctl --help 

 

四,systemd的核心概念unit是什么?

在systemd中,unit表示不同类型的systemd对象,通过配置文件进行标识和配置;

文件中主要包含了系统服务、监听socket、保存的系统快照以及其它与init相关的信息

 

五,Systemd中Unit 文件的存放目录

1,

Unit 文件按照 Systemd 约定,被放置指定的三个系统目录之一。

三个目录是有优先级的,如下所示,越上面的优先级越高。

在三个目录中有同名文件的时候,只有优先级最高的目录里的文件会被使用。

 

  • /etc/systemd/system:系统或用户自定义的配置文件

  • /run/systemd/system:软件运行时生成的配置文件

  • /usr/lib/systemd/system:系统或第三方软件安装时添加的配置文件。

 2,

Systemd 默认从目录 /etc/systemd/system/ 读取配置文件,

里面存放的大部分文件都是符号链接,指向目录 /usr/lib/systemd/system/

说明1:

        centos8中,/usr/lib/systemd/system 也是lib/systemd/system

        因为 /lib目录已经变成了 /usr/lib的一个符号链接

 

[root@blog systemd]# ll / | grep lib
lrwxrwxrwx.   1 root root    7 May 11  2019 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 May 11  2019 lib64 -> usr/lib64

说明2:

        /etc/systemd/system目录下通常不放置unit文件本身,

       而是放置/usr/lib/systemd/system中unit文件的符号链接

 

六,unit的类型

1,所有类型 

Service unit:系统服务

Target unit:多个 Unit 构成的一个组

Device Unit:硬件设备

Mount Unit:文件系统的挂载点

Automount Unit:自动挂载点

Path Unit:文件或路径

Scope Unit:不是由 Systemd 启动的外部进程

Slice Unit:进程组

Socket Unit:进程间通信的 socket

Swap Unit:swap 文件

Timer Unit:定时器

snapshot Unit:表示由 systemctl snapshot 命令创建的 Systemd Units 运行状态快照

 

2,原始文档请参见:

[root@centos8 ~]# man systemd
...
The following unit types are available:
        1. Service units, which start and control daemons and the processes they consist of. For details, see systemd.service(5).
        2. Socket units, which encapsulate local IPC or network sockets in the system, useful for socket-based activation. For details about socket units,
           see systemd.socket(5), for details on socket-based activation and other forms of activation, see daemon(7).
        3. Target units are useful to group units, or provide well-known synchronization points during boot-up, see systemd.target(5).
        4. Device units expose kernel devices in systemd and may be used to implement device-based activation. For details, see systemd.device(5).
        5. Mount units control mount points in the file system, for details see systemd.mount(5).
        6. Automount units provide automount capabilities, for on-demand mounting of file systems as well as parallelized boot-up. See
           systemd.automount(5).
        7. Timer units are useful for triggering activation of other units based on timers. You may find details in systemd.timer(5).
        8. Swap units are very similar to mount units and encapsulate memory swap partitions or files of the operating system. They are described in
           systemd.swap(5).
        9. Path units may be used to activate other services when file system objects change or are modified. See systemd.path(5).
       10. Slice units may be used to group units which manage system processes (such as service and scope units) in a hierarchical tree for resource
           management purposes. See systemd.slice(5).
       11. Scope units are similar to service units, but manage foreign processes instead of starting them as well. See systemd.scope(5).

 

七,例子一:管理服务的常用命令:

1,停止firewalld服务

# status    查询服务的状态

[root@centos8 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-04-07 17:22:23 CST; 2min 0s ago
     Docs: man:firewalld(1)
 Main PID: 1125 (firewalld)
    Tasks: 2 (limit: 23837)
   Memory: 31.6M
   CGroup: /system.slice/firewalld.service
           └─1125 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid

4月 07 17:22:22 centos8 systemd[1]: Starting firewalld - dynamic firewall daemon...
4月 07 17:22:23 centos8 systemd[1]: Started firewalld - dynamic firewall daemon.

可以看到状态是active

 

#is-active 只返回是否在运行中

[root@centos8 ~]# systemctl is-active firewalld
active

 

#stop: 用来停止服务

[root@centos8 ~]# systemctl stop firewalld

 

#再次查询状态:注意已变成了inactive

[root@centos8 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Tue 2020-04-07 17:24:34 CST; 3s ago
     Docs: man:firewalld(1)
  Process: 1125 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 1125 (code=exited, status=0/SUCCESS)

4月 07 17:22:22 centos8 systemd[1]: Starting firewalld - dynamic firewall daemon...
4月 07 17:22:23 centos8 systemd[1]: Started firewalld - dynamic firewall daemon.
4月 07 17:24:33 centos8 systemd[1]: Stopping firewalld - dynamic firewall daemon...
4月 07 17:24:34 centos8 systemd[1]: Stopped firewalld - dynamic firewall daemon.

 

#is-active   只返回是否在运行中

[root@centos8 ~]# systemctl is-active firewalld
inactive

 

2,启动firewalld服务

#start 用来启动服务

[root@centos8 ~]# systemctl start firewalld

 

#is-active 只返回是否在运行中

[root@centos8 ~]# systemctl is-active firewalld
active

 

3,禁止firewalld开机自启动

#is-enabled  判断是否开机自启动

[root@centos8 ~]# systemctl is-enabled firewalld
enabled

 

#disable  用来禁止开机自启动

[root@centos8 ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

 

#is-enabled  判断是否开机自启动

[root@centos8 ~]# systemctl is-enabled firewalld
disabled

 

4,实现firewalld开机自启动

#eable  用来实现开机自启动

[root@centos8 ~]# systemctl enable firewalld
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.

可以看到:使开机自启动,systemctl就是给service文件在/etc/systemd/system/multi-user.target.wants目录下创建了符号链接

 

#is-enabled  判断是否开机自启动

[root@centos8 ~]# systemctl is-enabled firewalld
enabled

 

5,重新启动firewalld服务

[root@centos8 ~]# systemctl restart firewalld

 

6,注销cups服务

#mask用来注销服务,

[root@centos8 ~]# systemctl mask cups
Created symlink /etc/systemd/system/cups.service → /dev/null.

说明:可以看到它的操作是把cups服务创建成为了/dev/null的符号链接

看是否还可以做enable和start操作?

[root@centos8 ~]# systemctl enable cups
Failed to enable unit: Unit file /etc/systemd/system/cups.service is masked.

[root@centos8 ~]# systemctl start cups
Failed to start cups.service: Unit cups.service is masked.

系统都会给出提示已经masked

 

7,取消注销cups服务

[root@centos8 ~]# systemctl unmask cups
Removed /etc/systemd/system/cups.service.

 

八,例子二:对所有unit的操作 

1,列出所有ative的unit,注意:列出的unit是显示是否active

[root@centos8 ~]# systemctl list-units

说明:systemctl 与 systemctl list-units功能相同

 

2,列出所有unit,包括inactive状态的

[root@centos8 ~]# systemctl list-units --all

 

3,列出所有未运行的unit,即状态是inactive的

[root@blog ~]# systemctl list-units --all --state=inactive 

 

4,列出当前活动状态(active)的所有服务单元

[root@centos8 ~]# systemctl list-units --type=service

 

5,列出所有状态(包括active和inactive)的所有服务unit

[root@centos8 ~]# systemctl list-units --type=service --all

 

6,列出所有的unit文件,注意:这里显示是否enabled

[root@centos8 ~]# systemctl list-unit-files

 

针对四种state的说明:

enabled:已建立自启动链接
disabled:没建立自启动链接
static:该配置文件没有[Install]部分(无法执行),只能作为其他配置文件的依赖
masked:该配置文件被禁止建立启动链接

 

7,列出一个服务所依赖的单元

#list-dependencies 列出一个 Unit 的所有依赖

[root@centos8 ~]# systemctl list-dependencies firewalld.service 

 

8,查看unit文件

#cat : 用来指定要查看的unit文件

[root@blog ~]# systemctl cat openresty.service
# /usr/lib/systemd/system/openresty.service
[Unit]
Description=The OpenResty Application Platform
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 

9,重新加载所有修改过的unit文件

#daemon-reload : 当我们手动修改/添加 unit文件后,需要执行daemon-reload

[root@blog ~]# systemctl daemon-reload

 

10,显示指定unit的所有底层参数

[root@blog ~]# systemctl show openresty.service 

 

也可只看其中的一项:

[root@blog ~]# systemctl show -p PIDFile openresty.service
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid

 

九,service文件的格式说明:

1,看一个例子:

# openresty的service文件

[root@centos8 ~]# more /usr/lib/systemd/system/openresty.service 
[Unit]
Description=The OpenResty Application Platform
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 

2,[Unit]区块

[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 运行必须满足的条件,否则会报启动失败

 

3,[Install]通常是配置文件的最后一个区块,用来定义如何启动,以及是否开机启动

WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system目录下面以 Target 名 + .wants后缀构成的子目录中(弱依赖)

RequiredBy:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入/etc/systemd/system目录下面以 Target 名 + .required后缀构成的子目录中(强依赖)

Alias:当前 Unit 可用于启动的别名

Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit

 

4,[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:指定环境变量

PrivateTmp: 否使用私有的tmp目录

 

十,查看centos的版本

[root@blog ~]$ cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core) 

 

十一,systemctl管理系统的运行级别

参见:

https://blog.imgtouch.com/index.php/2023/05/21/centos8linux-yong-systemctl-guan-li-yun-xing-ji-bie/

posted @ 2020-04-11 11:06  刘宏缔的架构森林  阅读(15386)  评论(0编辑  收藏  举报