浅谈systemd

浅谈systemd
systemd的基本概念
Systemd的管理服务
Systemd的管理运行级别
systemd的基本概念
一、system的简要介绍

定义
  系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进程和其它进程
新特性
系统引导时实现服务并行启动
按需启动守护进程
自动化的服务依赖关系管理
同时采用socket式与D-Bus总线式激活服务
系统状态快照
systemd的核心概念:unit
unit表示不同类型的systemd对象,通过配置文件进行标识和配置;文件中主要包含了系统服务、监听socket、保存的系统快照以及其它与init相关的信息
systmed的配置文件
/usr/lib/systemd/system:每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/
/run/systemd/system:系统执行过程中所产生的服务脚本,比上面目录优先运行
/etc/systemd/system:管理员建立的执行脚本,类似于/etc/rcN.d/Sxx的功能,比上面目录优先运行
二、Systemd的Unit类型

systemctl -t help:查看unit类型
service unit:文件扩展名为.service,用于定义系统服务
target unit:文件扩展名为.target,用于模拟实现运行级别
device unit:文件扩展名为.device,用于定义内核识别的设备
mount unit:文件扩展名为.mount,定义文件系统挂载点
socket unit:文件扩展名为.socket,用于标识进程间通信用的socket文件,也可在系统启动时,延迟启动服务,实现按需启动
snapshot unit:文件扩展名为.snapshot,管理系统快照
swap unit:文件扩展名为.swap,用于标识swap设备
automount unit:文件扩展名为.automount,文件系统的自动挂载点
path unit:文件扩展名为.path,用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激活服务,如:spool 目录
三、Systemd的新特性

关键特性
基于socket的激活机制:socket与服务程序分离
基于d-bus的激活机制:
基于device的激活机制:
基于path的激活机制:
系统快照:保存各unit的当前状态信息于持久存储设备中向后兼容sysv init脚本
不兼容
systemctl命令固定不变,不可扩展
非由systemd启动的服务,systemctl无法与之通信和控制
Systemd的管理服务
  语法: systemctl COMMAND name.service
一、管理服务

启动:systemctl start name.service,相当于Centos6的service name.service start
停止:sysytemctl stop name.service,相当于Centos6的service name.service stop
重启:systemctl restart name.service,相当于Centos6的service name.service restart
状态:systemctl status name.service,相当于Centos6的service name.service status
条件式重启:已启动才重启,否则不做操作
sysytmectl try-restart name.service,相当于Centos6的service name condrestart
重载或重启服务:先加载,再启动
systmenctl reload-or-restart name.service
重载或条件式重启服务:
sysytemctl reload-or-try-restart name.service
禁止自启动和手动启动
systemctl mask name.service
取消禁止自启动和手动启动
systmectl unmask name.service
[root@Centos8 ~]#systemctl mask httpd
Created symlink /etc/systemd/system/httpd.service → /dev/null.
[root@Centos8 ~]#systemctl unmask httpd
Removed /etc/systemd/system/httpd.service.
#结论:mask和umask命令实际为建立/删除指向/dev/null的软链接

实验:修改unit
修改 /usr/lib/systemd/system/httpd.service文件,将Description 改为The Apache HTTP,此时查看systemctl status httpd状态,发现服务正常运行,但是描述没有改变
    Warning: The unit file, source configuration file or drop-ins of httpd.service changed on disk. Run 'systemctl daemon-reload' to reload units.
    #已经给出提示了
    ● httpd.service - The Apache HTTP Server
    Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
    Active: active (running) since Sun 2019-11-03 17:14:36 CST; 11min ago
    
    #据状态信息的提示,执行`sysytemctl daemon-reload`,再查看状态发现名称改变了
    [root@Centos8 /usr/lib/systemd/system]#systemctl daemon-reload
    [root@Centos8 /usr/lib/systemd/system]#systemctl status httpd
    ● httpd.service - The Apache HTTP

当服务正在运行的时候,对服务文件的unit进行改动,必须要重载才能更新
二、查看服务

查看某服务当前激活与否的状态:
sytemctl is-active name.service
查看所有已经激活的服务:
systemctl list-units -type service
systemctl list-units -t service
查看所有服务:
systemctl list-units -t service -a or -all
三、设置服务开机状态

设定某服务开机自启动
systemctl enable name.service,相当于Centos6的chkconfig name on
设定某服务开机禁止自启动
systemctl disable name.service,相当于Centos6的chkconfig name off
查看所有服务的开机自启状态
systemctl list-unit-files,相当于Centos6的 chkconfig --list
用来列出该服务在哪些运行级别下启用和禁用:
ls /etc/systemd/system/*wants/sshd.service,相当于Centos6的 chkconfig sshd --list
实验:设置服务自启和禁止自启
[root@Centos8 ~]#systemctl list-unit-files | grep httpd
httpd.service                                             disabled
#查看httpd服务原来状态是禁止自启
[root@Centos8 ~]#systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
#让httpd服务开机自启后,提示创建了软链接
[root@Centos8 ~]rm -f /etc/systemd/system/multi-user.target.wants/httpd.service
#删除这个软链接文件
[root@Centos8 ~]#systemctl list-unit-files | grep httpd
httpd.service                                              disabled 
#查看httpd状态,发现仍然是禁止自启动
[root@Centos8 ~]#ln -s /usr/lib/systemd/system/httpd.service /etc/systemd/system/multi-user.target.wants/httpd.service
#创建原来的软链接文件
[root@Centos8 ~]#systemctl list-unit-files | grep httpd
httpd.service                                              enabled
#此时查看httpd状态,已经是开机自启动
[root@Centos8 ~]#systemctl disable httpd
Removed /etc/systemd/system/multi-user.target.wants/httpd.service
#设置httpd开机禁止自启,提示删除了软链接文件
[root@Centos8 ~]#systemctl is-enabled httpd
disabled
#查看状态,已经变为禁止开机自启动

总结:将某个服务设为自启或者禁止自启,实际上就是在相应的文件夹建立软链接文件的过程,systemd通过查询软链接文件的存在与否判断启用/禁止自启的状态。
四、其他命令

查看服务是否开机自启:
systemctl is-enabled name.service
systemctl list-unit-files | grep name.service
查看服务的依赖关系:
`systemctl list-dependencies httpd.service
[root@Centos8 ~]#systemctl list-dependencies httpd.service
httpd.service
● ├─-.mount
● ├─httpd-init.service
● ├─system.slice
● └─sysinit.target
●   ├─dev-hugepages.mount
●   ├─dev-mqueue.mount
●   ├─dracut-shutdown.service
●   ├─import-state.service
●   ├─iscsi.service
●   ├─kmod-static-nodes.service
●   ├─ldconfig.service
●   ├─loadmodules.service
●   ├─lvm2-lvmpolld.socket
●   ├─lvm2-monitor.service
●   ├─multipathd.service
●   ├─nis-domainname.service
●   ├─plymouth-read-write.service
●   ├─plymouth-start.service
●   ├─proc-sys-fs-binfmt_misc.
.

杀掉进程:
systemctl kii unitname
五、服务状态

显示状态:systemctl list-unit-files -t service -a
loaded:unit配置文件已处理
active(running):一次或多次持续处理的运行
active(exited):成功完成一次性的配置
active(waiting):运行中,等待一个事件
inactive:不运行
enabled:开机启动
disabled:开机不启动
static:开机不启动,但可被另一个启用的服务激活
Systemd的管理运行级别
一、target的概念

target:systemd的运行级别管理unit
路径:ls /etc/lib/systemd/system/*.target
显示状态:systemctl list-unit-files
systemd的运行级别与传统运行级别的对应关系
runlevel0.target, poweroff.target ==> 0
runlevel1.target, rescue.target ==> 1
runlevel2.target, multi-user.target ==> 2
runlevel3.target, multi-user.target ==> 3
runlevel4.target, multi-user.target ==> 4
runlevel5.target, graphical.target ==> 5
runlevel6.target, reboot.target ==> 6
查看target的依赖关系:systemctl list-dependencies name.target
二、级别切换

级别切换:systemctl isolate name.target
注:只有/lib/systemd/system/*.target文件中AllowIsolate=yes 才能切换(修改文件需执行systemctl daemon-reload才能生效)
显示状态:
systemctl list-unit-files -t target -a
systemctl list-unit-files | grep target
查看target:who -r runlevel
获取默认运行级别:systemctl get-default
修改默认运行级别:systemctl set-default name.target
三、Systemd其他命令

切换紧急救援模式: systemctl rescue
切换至emergency模式:systemctl emergency
其他常用命令
传统命令init,poweroff,halt,reboot都成为systemctl的软链接
关机:systemctl halt、systemctl poweroff
重启:systemctl reboot
挂起:systemctl suspend
休眠:systemctl hibernate
休眠并挂起:systemctl hybrid-sleep
[root@Centos8 ~]#ls /sbin/poweroff -l
lrwxrwxrwx. 1 root root 16 May 23 22:48 /sbin/poweroff -> ../bin/systemctl
[root@Centos8 ~]#ls /sbin/reboot -l
lrwxrwxrwx. 1 root root 16 May 23 22:48 /sbin/reboot -> ../bin/systemctl
[root@Centos8 ~]#ls /sbin/shutdown -l
lrwxrwxrwx. 1 root root 16 May 23 22:48 /sbin/shutdown -> ../bin/systemctl
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值