关于linux中systemd的一些笔记(代码片段)

山河已无恙 山河已无恙     2022-12-04     469

关键词:

写在前面


  • 嗯,准备RHCA,学习整理这部分知识
  • 博文内容涉及:
    • systemd简述
    • 对于unit的信息的介绍
    • 通过systemctl命令控制Service unitDemo
    • Service unit配置文件内容,权值的一些介绍

傍晚时分,你坐在屋檐下,看着天慢慢地黑下去,心里寂寞而凄凉,感到自己的生命被剥夺了。当时我是个年轻人,但我害怕这样生活下去,衰老下去。在我看来,这是比死亡更可怕的事。--------王小波


一、Systemd 简述

systemd是Linux操作系统的系统和服务管理器。在系统启动时或者正在运行的系统上的一个守护进程,systemd负责激活系统资源、服务器守护程序和其他进程systemdRHEL 7之后出现的。

守护进程(daemon)是在后台执行各种任务的进程。通常,守护程序在引导时自动启动并继续运行直到系统关闭或手动停止。许多守护程序的名称的约定以字母d结尾

关于RHEL启动进程管理:

  • RHEL 5的时候,使用 Sys init,启动速度最慢的,无论进程相互之间有无依赖关系,都是串行启动过程,
  • RHEL 6的时候,使用 Upstart init相对启动速度快一点有所改进。有依赖的进程之间依次启动而其他与之没有依赖关系的则并行同步启动
  • RHEL 7 Systemd与以上都不同。所有进程无论有无依赖关系则都是并行启动(很多时候进程没有真正启动而是只有一个信号或者说是标记而已,在真正利用的时候才会真正启动)。

Systemd为了解决上文的问题而诞生。它的目标是,为系统的启动和管理提供一套完整的解决方案,

RHEL启动的第一个进程(PID 1)就是systemd,提供的功能包括:

  • 并行化功能(同时启动多个服务),可提高系统的启动速度。
  • 按需启动后台程序而无需单独的服务。
  • 自动管理服务依赖关系,可以防止长时间超时。例如,网络相关服务在网络可用之前不会尝试启动。
  • 使用Linux控制组(Cgroup)一起跟踪相关进程的方法。

systemd Targets 启动级别

二、systemctl 命令管理系统

systemctl Systemd 的主命令,systemctl命令用于管理不同类型的系统对象,这些对象称之为units

  • Service unit:用于定义系统服务,文件扩展名为.service
  • Target unit:用于模拟实现“运行级别”,文件扩展名为.target,可以理解Target 就是一个 Unit 组,包含许多相关的 Unit
  • Device unit:用于定义内核识别的设备,文件扩展名为.device
  • Mount unit:用于定义文件系统挂载点,文件扩展名为.mount
  • Socket unit:用于标识进程间通信用的socket文件,文件扩展名为.socket
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl status cockpit.socket #CocKpit驾驶舱 和SSH类似,用于远程控制,类似于阿里云的控制台。为初级管理员提供基本操作,通过web端的控制台,访问地址:服务器IP:9090
● cockpit.socket - Cockpit Web Service Socket
   Loaded: loaded (/usr/lib/systemd/system/cockpit.socket; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:cockpit-ws(8)
   Listen: [::]:9090 (Stream)
  • Snapshot unit:管理系统快照,文件扩展名为.snapshot
  • Swap unit:用于标识swap设备,文件扩展名为.swap
  • Automount unit:文件系统的自动挂载点,文件扩展名为.automount
  • Path unit:用于根据文件系统上特定对象的变化来启动其他服务,文件扩展名为.path
  • Timer unit:用于管理计划任务,文件扩展名为.timer
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl status systemd-tmpfiles-clean.timer #用于定时清理 `/tmp`,`/var/tmp`之类的临时文件
● systemd-tmpfiles-clean.timer - Daily Cleanup of Temporary Directories
   Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.timer; static; vendor preset: disabl>
   Active: active (waiting) since Sun 2022-04-03 11:24:00 CST; 1h 48min ago
  Trigger: Mon 2022-04-04 11:39:07 CST; 22h left
     Docs: man:tmpfiles.d(5)
           man:systemd-tmpfiles(8)

Apr 03 11:24:00 liruilongs.github.io systemd[1]: Started Daily Cleanup of Temporary Directories.
┌──[root@liruilongs.github.io]-[~]
└─$
  • Slice unit:用于资源管理,文件扩展名为.slice

1.查看units列表信息

列出loaded(配置文件以加载)的units

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl list-units
  UNIT                                               LOAD   ACTIVE SUB       DESCRIPTION
  proc-sys-fs-binfmt_misc.automount                  loaded active waiting   Arbitrary Executable File>
 .............

命令输出说明:

  • UNIT:服务单元名称。
  • LOAD: systemd是否正确解析了单元的配置并将该单元加载到内存中
  • ACTIVE:单元的高级别激活状态。此信息表明单元是否已成功启动
  • SUB:单元的低级别激活状态。此信息指示有关该单完的更多详细信息。信息视单元类型、状态以及单元的执行方式而异。
  • DESCRIPTION:单元的简短描述

列出全部的units

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl list-unit-files
UNIT FILE                                  STATE
proc-sys-fs-binfmt_misc.automount          static
-.mount                                    generated
boot.mount                                 generated
dev-hugepages.mount                        static
dev-mqueue.mount                           static
proc-sys-fs-binfmt_misc.mount              static
sys-fs-fuse-connections.mount              static
sys-kernel-config.mount                    static
sys-kernel-debug.mount                     static
tmp.mount                                  disabled
systemd-ask-password-console.path          static
.....

查看指定的系统对象

查看timer,系统定时任务

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl list-units -i timer
0 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl list-unit-files -t timer
UNIT FILE                    STATE
dnf-makecache.timer          enabled  #yaml仓库定时更新
fstrim.timer                 disabled  
systemd-tmpfiles-clean.timer static  # 临时文件清理
unbound-anchor.timer         enabled

4 unit files listed.
┌──[root@liruilongs.github.io]-[~]
└─$

查看socket套接字

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl list-unit-files -t socket
UNIT FILE                               STATE
cockpit-wsinstance-http-redirect.socket static
cockpit-wsinstance-http.socket          static
cockpit-wsinstance-https-factory.socket static
cockpit-wsinstance-https@.socket        static
cockpit.socket                          enabled
dbus.socket                             static

列出类型为service,状态为loadedactive的 unit

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl list-units -t service
  UNIT                                                 LOAD   ACTIVE SUB     DESCRIPTION
  auditd.service                                       loaded active running Security Auditing Service

列出类型为service,状态为activeinactive 的unit

┌──[root@liruilongs.github.io]-[~]
└─$  systemctl list-units --type service --all

查看失败的服务

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl --failed -t service
  UNIT          LOAD   ACTIVE SUB    DESCRIPTION
● kdump.service loaded failed failed Crash recovery kernel arming
.....

三、systemctl操作Service unit

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl status sshd 
● sshd.service - OpenSSH server daemon  #单元的描述信息
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) #子状态,加载的配置文件,默认开机自启
   Active: active (running) since Sun 2022-04-03 11:24:03 CST; 2h 16min ago #运行状态,时间
     Docs: man:sshd(8)  #帮助文档
           man:sshd_config(5)
 Main PID: 880 (sshd) #主进程
    Tasks: 1 (limit: 6044) # 任务量 
   Memory: 7.2M   #消耗的内存
   CGroup: /system.slice/sshd.service  # 用的什么slice
           └─880 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes2>
Apr 03 11:24:02 liruilongs.github.io systemd[1]: Starting OpenSSH server daemon...  #启动日志,什么进程引导激活的
Apr 03 11:24:03 liruilongs.github.io sshd[880]: Server listening on 0.0.0.0 port 22. #监听的端口
Apr 03 11:24:03 liruilongs.github.io sshd[880]: Server listening on :: port 22.
Apr 03 11:24:03 liruilongs.github.io systemd[1]: Started OpenSSH server daemon.  #启动服务
Apr 03 11:25:30 liruilongs.github.io sshd[1112]: Accepted publickey for root from 172.25.254.254 port > #连接日志
Apr 03 11:25:30 liruilongs.github.io sshd[1112]: pam_unix(sshd:session): session opened for user root >
lines 1-17/17 (END)

查看Service单元状态信息

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl is-active sshd
active
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl is-enabled sshd
enabled
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl is-
is-active          is-enabled         is-failed          is-system-running

配置文件加载

优先级从高到低

  • 本地配置的系统单元: /etc/systemd/system
  • 运行时配置的系统单元: /run/systemd/system
  • 软件包安装的系统单元: /usr/lib/systemd/system

默认的服务配置文件位置:/usr/lib/systemd/system

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl status sshd | head  -2
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)

添加高优先级配置文件:/run/systemd/system

┌──[root@liruilongs.github.io]-[~]
└─$ cp /usr/lib/systemd/system/sshd.service /run/systemd/system/sshd.service
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl daemon-reload
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl status sshd | head  -2
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/run/systemd/system/sshd.service; enabled; vendor preset: enabled)

添加高优先级配置文件,/etc/systemd/system

┌──[root@liruilongs.github.io]-[~]
└─$ cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl daemon-reload
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl status sshd | head  -2
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/etc/systemd/system/sshd.service; enabled; vendor preset: enabled)
┌──[root@liruilongs.github.io]-[~]
└─$

配置文件内容

┌──[root@liruilongs.github.io]-[~]
└─$ cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon #描述信息
Documentation=man:sshd(8) man:sshd_config(5) #帮助文档
After=network.target sshd-keygen.target  #依赖关系,在...之后执行
Wants=sshd-keygen.target  # 强依赖

[Service] 
Type=notify  #notify类型
EnvironmentFile=-/etc/crypto-policies/back-ends/opensshserver.config  #环境变量读取
EnvironmentFile=-/etc/sysconfig/sshd   # - 号为文件可以不存在
ExecStart=/usr/sbin/sshd -D $OPTIONS $CRYPTO_POLICY  # 启动命令
ExecReload=/bin/kill -HUP $MAINPID  # reload从新初始化
KillMode=process  #设置在单元停止时,杀死进程的方法 process 表示仅杀死主进程
Restart=on-failure  #仅在服务进程异常退出时重启
RestartSec=42s # 42秒后重启

[Install]
WantedBy=multi-user.target #属于那个target
┌──[root@liruilongs.github.io]-[~]
└─$

手册查看,不错的翻译 systemd.service 中文手册

┌──[root@liruilongs.github.io]-[~]
└─$ man -k systemd

控制Service unit

启动停止服务

systemctl start sshd.service 
systemctl stop sshd.service

重新加载配置文件

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl reload sshd #还是原来的进程,只是从新加载配置文件  

重启服务

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl restart  sshd #一个新的进程

查看服务状态

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-04-03 11:46:36 CST; 3h 5min ago
     Docs: man:httpd.service(8)
 Main PID: 1616 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 6044)
   Memory: 33.7M
   CGroup: /system.slice/httpd.service
           ├─1616 /usr/sbin/httpd -DFOREGROUND
           ├─1617 /usr/sbin/httpd -DFOREGROUND
           ├─1618 /usr/sbin/httpd -DFOREGROUND
           ├─1619 /usr/sbin/httpd -DFOREGROUND
           └─1620 /usr/sbin/httpd -DFOREGROUND

Apr 03 11:46:35 liruilongs.github.io systemd[1]: Starting The Apache HTTP Server...
Apr 03 11:46:36 liruilongs.github.io systemd[1]: Started The Apache HTTP Server.
Apr 03 11:46:36 liruilongs.github.io httpd[1616]: Server configured, listening on: port 80

设置开机自启,添加一个软链接

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl enable httpd --now #立刻激活
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl is-enabled httpd
enabled
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl is-active  httpd
active
┌──[root@liruilongs.github.io]-[~]
└─$

设置服务开机不启动

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl disable  httpd
Removed /etc/systemd/system/multi-user.target.wants/httpd.service.

服务禁用

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl mask httpd
Created symlink /etc/systemd/system/httpd.service → /dev/null.
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl start httpd
Failed to start httpd.service: Unit httpd.service is masked.
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl status  httpd | head -2
● httpd.service
   Loaded: masked (Reason: Unit httpd.service is masked.)

可以删除软链接取消禁用

┌──[root@liruilongs.github.io]-[~]
└─$ ll  /etc/systemd/system/httpd.service
lrwxrwxrwx. 1 root root 9 Apr  3 16:08 /etc/systemd/system/httpd.service -> /dev/null
┌──[root@liruilongs.github.io]-[~]
└─$ rm -r   /etc/systemd/system/httpd.service
rm: remove symbolic link '/etc/systemd/system/httpd.service'? y
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl daemon-reload
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl start httpd

可以通过unmask来取消禁用

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl mask httpd
Created symlink /etc/systemd/system/httpd.service → /dev/null.
┌──[root@liruilongs.github.io]-[~]
└─$ systemctl unmask httpd
Removed /etc/systemd/system/httpd.service.
┌──[root@liruilongs.github.io]-[~]
└─$

unit依赖

可以通过如下命令查看unit的启动依赖关系

systemctl list-dependencies cups.service 
systemctl list-dependencies cups.service --reverse

查看httpd Service 正向依赖,在httpd之前启动

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl list-dependencies httpd
httpd.service
● ├─-.mount
● ├─httpd-init.service
● ├─system.slice
● └─sysinit.target
●   ├─dev-hugepages.mount
●   ├─dev-mqueue.mount
●   ├─dracut-shutdown.service
●   ├─import-state.service
●   ├─iscsi-onboot.service
●   ├─kmod-static-nodes.service
......
......
┌──[root@liruilongs.github.io]-[~]
└─$

查看httpd Service 反向依赖,在httpd之后启动

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl list-dependencies httpd --reverse
httpd.service
● └─multi-user.target
●   └─graphical.target

查看multi-user.target正向依赖是否有httpd

┌──[root@liruilongs.github.io]-[~]
└─$ systemctl list-dependencies multi-user.target | grep httpd
● ├─httpd.service

嗯,关于systemd和小伙伴们分享到这里,这块涉及的内容很多,这里简单介绍,在Linux生态系统中,systemd被部署到了大多数的标准Linux发行版中,只有为数不多的几个发行版尚未部署。关于systemd启动过程、对其他unit的控制和管理、对系统运行级别等的控制,感兴趣小伙伴可以了解下。

关于linux中systemd的一些笔记(代码片段)

...f0c;准备RHCA,学习整理这部分知识博文内容涉及:systemd简述对于unit的信息的介绍通过systemctl命令控制Serviceunit的DemoServiceunit配置文件内容,权值的一些介绍傍晚时分,你坐在屋檐下,看着天慢慢地黑下去,心里... 查看详情

关于linux挂载fstab和systemd.mount使用场景的一些笔记(代码片段)

写在前面在stackoverflow的Unix&Linux社区看到相关的问题。有大佬做了解答,感觉问题不错,答案也不错,整理分享给小伙伴原问题地址:tmpontmpfs:fstabvstmp.mountwithsystemd?https://unix.stackexchange.com/questions/722496/ 查看详情

关于linux中通过systemdpathunit监听配置更新自动重启服务的一些笔记(代码片段)

...问题。这里整理分享给小伙伴,博文内容涉及:Systemd的PathUnits常用命令手册学习PathUnits的应用通过httpd服务演示监听配置文件自动重启服务食用方式:需要了解一点Systemd理解不足小伙伴帮忙指正在路上,我们永远... 查看详情

关于linux下通过ping/mtr长期监控网络输出日志报告的一些笔记(代码片段)

...ping、mtr长期监控网络输出报告的笔记博文内容涉及通过systemd-run启动一个临时的pingService实现长ping日志输出通过systemd-run启动一个临时的mtrtime&mtrSrevice实现mtr定时报告日志输出理解不足小伙伴帮忙指正我所渴求的,無非是... 查看详情

关于linux下redis自动化部署的一些笔记(代码片段)

...及:通过源码编译和yum安装redisDemo通过二进制文件和systemd运行redis的配置方式服务管理,配置的文件的简单介绍ansibleredis角色ansible-role-redis编写通过ansible自动化安装理解不足小伙伴帮忙指正傍晚时分,你坐在屋檐下&#... 查看详情

关于linux中如何使用systemd-run创建临时cgroup来限制ad-hoc的资源消耗(代码片段)

...面分享一些临时命令资源限制的笔记博文内容涉及:systemd-run限制ad-hoc资源消耗基本原理前后台ad-hoc资源限制demo理解不足小伙伴帮忙指正地球上人的博爱将只可能以媚俗作态为基础--------《生命中不能承受之轻》简单介绍通... 查看详情

关于linux中安全方面的一些笔记(代码片段)

写在前面笔记是学习整理的,大都老师讲课内容,适合温习,不适合新手。整理了一份,希望对小伙伴有帮助.生活加油,天天开心!博文主要围绕以几个方面:Linux基本防护:账户安全、文件系统安全、... 查看详情

关于linux性能调优中网络i/o的一些笔记(代码片段)

写在前面和小伙伴分享一些Linux网络优化的笔记,内容很浅,可以用作入门博文内容结合《Linux性能优化》读书笔记整理涉及内容包括常用的优化工具(mii-tool,ethtool,ifconfig,ip,sar,iptraf,netstat)使用Demo及对应的输出解释具体的调优策... 查看详情

关于linux性能调优中网络i/o的一些笔记(代码片段)

写在前面和小伙伴分享一些Linux网络优化的笔记,内容很浅,可以用作入门博文内容结合《Linux性能优化》读书笔记整理涉及内容包括常用的优化工具(mii-tool,ethtool,ifconfig,ip,sar,iptraf,netstat)使用Demo及对应的输出解释具体的调优策... 查看详情

关于linux中监控方面的一些笔记(代码片段)

写在前面笔记是学习后整理的,适合温习,不适合新手。蹭着国庆长假整理了一份,希望对小伙伴有帮助.生活加油,天天开心!博文主要围绕以几个方面:监控的基本知识zabbix的安装部署教程,多节点监控... 查看详情

关于linux中shell等知识的一些笔记(代码片段)

写在前面笔记是上课后整理的笔记,适合温习,不适合新手,希望对小伙伴有帮助笔记内容包括:Shell一些常见情境傍晚时分,你坐在屋檐下,看着天慢慢地黑下去,心里寂寞而凄凉,感到自己的生... 查看详情

关于linux中shell等知识的一些笔记(代码片段)

写在前面笔记是上课后整理的笔记,适合温习,不适合新手,希望对小伙伴有帮助笔记内容包括:Shell一些常见情境傍晚时分,你坐在屋檐下,看着天慢慢地黑下去,心里寂寞而凄凉,感到自己的生... 查看详情

关于linux中批量配置ssh免密的一些笔记(代码片段)

写在前面今天和小伙伴们分享批量配置SSH免密的一些笔记传统的运维场景可以通过Ansible等运维工具处理在安装使用一些运维工具,比如Ansible,或则一些pass组件,需要配置ssh免密,sudo免密的操作今天和小伙伴分享... 查看详情

关于linux中批量配置ssh免密的一些笔记(代码片段)

写在前面今天和小伙伴们分享批量配置SSH免密的一些笔记传统的运维场景可以通过Ansible等运维工具处理在安装使用一些运维工具,比如Ansible,或则一些pass组件,需要配置ssh免密,sudo免密的操作今天和小伙伴分享... 查看详情

关于linux中批量配置ssh免密的一些笔记(代码片段)

写在前面今天和小伙伴们分享批量配置SSH免密的一些笔记传统的运维场景可以通过Ansible等运维工具处理在安装使用一些运维工具,比如Ansible,或则一些pass组件,需要配置ssh免密,sudo免密的操作今天和小伙伴分享... 查看详情

关于linux性能调优中磁盘io调优的一些笔记(代码片段)

写在前面和小伙伴分享一些Linux磁盘IO优化的笔记,内容很浅,可以用作入门博文内容结合《Linux性能优化》读书笔记整理涉及内容包括使用vmstat统计系统内磁盘分区I/O属性的性能使用iostat分析磁盘分区的饱和度,利用率... 查看详情

关于linux中控制群组cgroup(资源管理)的一些笔记(代码片段)

写在前面学习遇到容器资源限制的处理问题,所以研究下。博文内容涉及:容器中cgroup的应用控制群组部分属节译,介意小伙伴请看原文。原文链接:https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html-single/reso... 查看详情

关于linux中系统调优的一些笔记(代码片段)

写在前面推送的的邮件里看到有大佬讲的公共课,听了之后这里整理学习笔记。因为是公开课,所以讲的很浅,没接触过,这里做为了解,长长见识。博文内容包括系统调优原理概述如何检测系统的性能瓶颈... 查看详情