centos8使用systemd/systemctl管理系统/服务
一,systemd的用途?
Systemd 是 Linux 系统工具,用来启动守护进程,已成为大多数发行版的标准配置
Systemd 的优点是功能强大,使用方便,
缺点是体系庞大,非常复杂
在centos6中,系统的启动管理使用initd,
从centos7开始,systemd成为了系统的默认启动守护进程
systemctl是systemd的管理控制工具
附上systemd的架构图:(来自网络)

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: 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管理系统的运行级别
参见
centos8使用systemd/systemctl管理系统/服务的更多相关文章
- Systemed systemctl 创建服务 详解
原文:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html 一.由来 历史上,Linux 的启动一直采用init进 ...
- systemd&systemctl
systemd is a system and service manager for Linux operating systems. When run as first process on bo ...
- CentOS7使用systemctl添加自定义服务
一.简介 Centos7开机第一个程序从init完全换成了systemd这种启动方式,同centos 5 6已经是实质差别.systemd是靠管理unit的方式来控制开机服务,开机级别等功能. 在/u ...
- 编写使用systemctl启动服务脚本
CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,还是存在系统服务里吧,即:/usr ...
- 使用systemctl管理服务
系统服务,开机不需要登录就能运行的程序(相当于开机自启)/usr/lib/systemd/system 用户服务,需要登录后才能运行的程序/usr/lib/systemd/user 目录下又存在两种类 ...
- 二进制编译安装nginx并加入systemctl管理服务
一.安装nginx所需环境 # yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y 二.安装ngi ...
- Linux_CentOS 中systemctl 管理服务、防火墙 firewalld 以及 SELinux 配置
使用 systemctl 管理服务 systemctl 就是 service 和 chkconfig 这两个命令的整合,在 CentOS 7 就开始被使用了,systemctl是系统服务管理器命令,它 ...
- Linux学习-透过 systemctl 管理服务
透过 systemctl 管理单一服务 (service unit) 的启动/开机启动与观察状态 一般来说,服务的启动有两个阶段,一 个是『开机的时候设定要不要启动这个服务』, 以及『你现在要不要启动 ...
- 使用 Centos 7 的 systemctl 管理服务
CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,存系统服务里即:/usr/lib/ ...
随机推荐
- tomcat在linux服务器启动时报错Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000794500000, 576716800, 0) failed; error='Cannot allocate memory' (errno=12)
原因是内存不足了 解决办法:1.top命令查看后台进程,看哪些可以关闭 2.ps -ef |grep tomcat看哪些不用的tomcat起着,也可以从这里关 3.加大这个tomcat的内存, 在ca ...
- 恭喜!Apache Hudi社区新晋多位Committer
1. 介绍 经过Apache Hudi项目委员会讨论及投票,向Udit Mehrotra.Gary Li.Raymond Xu.Pratyaksh Sharma 4人发出Committer邀请,4人均 ...
- 乔悟空-CTF-i春秋-Web-SQLi
2020.09.05 今天注定是高产的一天,这题真是坑,不看wp真的不会做,现在回头来复习一遍. 做题 题目 题目地址 做题 这题坑人的地方就在于重定向了两次,并且重要信息在第一次里藏着,第二次还给了 ...
- [LeetCode]301. 删除无效的括号(DFS)
题目 题解 step1. 遍历一遍,维护left.right计数器,分别记录不合法的左括号.右括号数量. 判断不合法的方法? left维护未匹配左括号数量(增,减)(当left为0遇到右括号,则交由r ...
- IDEA使用maven搭建SSM框架整合项目(超级详细,值得一看)
目录 温馨提示 简单介绍下SSM 搭建过程 一.框架介绍 二.下载Maven 三.创建Maven项目 四.Maven工程需要引入的Jar 包 五.整合SSM框架.需要的相关配置文件配置项目 六.工程导 ...
- CSDN博客图片水印自定义及去除方法
小图上传说明 尺寸较小的图片上传后默认不加水印,而且默认水平居中显示.你可以更改markdown格式后面的#pic_center,这是设置图片水平对齐的方式.#pic_left就是左对齐,#pic_r ...
- 深入理解python语言
2008年,安卓操作系统诞生:PC时代向移动时代转换 互联网,视窗 2017/5/27柯洁最终0:3AlphaGo 计算机技术的演进过程 不同编程语言的设计初心和适用对象 C语言核心解决的是性能问题, ...
- 预科班D11
2020.09.21星期一 预科班D11 学习内容: 一.基本数据类型及内置方法 1.整形int及浮点型float + - * / // ** % > < >= <= 2.字符 ...
- 爬虫必看,每日JS逆向之爱奇艺密码加密,今天你练了吗?
友情提示:优先在公众号更新,在博客园更新较慢,有兴趣的关注一下知识图谱与大数据公众号,本次目标是抠出爱奇艺passwd加密JS代码,如果你看到了这一篇,说明你对JS逆向感兴趣,如果是初学者,那不妨再看 ...
- IDEA搭建多模块maven项目
目录 新建maven项目作为多模块的parent项目 新建模块 在parent项目中配置 pom api 模块搭建restful风格api Mybatis Generator的使用(mybatis 逆 ...