systemd特性:
系统引导时实现服务并行启动
按需启动守护进程
自动化的服务依赖关系管理
同时采用socket式与D-Bus总线式激活服务
socket与服务程序分离
向后兼容sysv init脚本
使用systemctl 命令管理,systemctl命令固定不变,不可扩展,非由systemd启动的服务,
systemctl无法与之通信和控制
systemd核心概念:unit(单元)
unit表示不同类型的systemd对象,通过配置文件进行标识和配置,文件中包含系统服务,监听socket,保存的系统快照以及其他init相关的信息
Unit类型:
#查看unit类型
[root@servera ~]# systemctl -t help
Available unit types:
service
socket
target
device
mount
automount
swap
timer
path
slice
scope
-----------------------------------------------
service unit: 文件扩展名为.service, 用于定义系统服务
Socket unit: .socket, 定义进程间通信用的socket文件,也可在系统启动时,延迟启动服务,实现
按需启动
Target unit: 文件扩展名为.target,用于模拟实现运行级别 init 0-6
Device unit: .device, 用于定义内核识别的设备
Mount unit: .mount, 定义文件系统挂载点
Snapshot unit: .snapshot, 管理系统快照
Swap unit: .swap, 用于标识swap设备
Automount unit: .automount,文件系统的自动挂载点
Path unit: .path,用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激
活服务,如:spool 目录
unit配置文件:
/usr/lib/systemd/system:每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/
/run/systemd/system:系统执行过程中所产生的服务脚本,比上面目录优先运行
/etc/systemd/system:管理员建立的执行脚本,类似于/etc/rcN.d/Sxx的功能,比上面目录优先运行
systemctl管理工具:
语法:systemctl COMMAND name.service
使用方法:
#启动:相当于service name start
systemctl start name.service
#停止:相当于service name stop
systemctl stop name.service
#重启:相当于service name restart
systemctl restart name.service
#查看状态:相当于service name status
systemctl status name.service
#禁止自动和手动启动:
systemctl mask name.service
#取消禁止
systemctl unmask name.service
#查看某服务当前激活与否的状态:
systemctl is-active name.service
#查看所有已经激活的服务:
systemctl list-units --type|-t service
#查看所有服务:
systemctl list-units --type service --all|-a
#设定某服务开机自启,相当于chkconfig name on
systemctl enable name.service
#设定某服务开机禁止启动:相当于chkconfig name off
systemctl disable name.service
#查看所有服务的开机自启状态,相当于chkconfig --list
systemctl list-unit-files --type service
#用来列出该服务在哪些运行级别下启用和禁用:chkconfig –list name
ls /etc/systemd/system/*.wants/name.service
#查看服务是否开机自启:
systemctl is-enabled name.service
#列出失败的服务
systemctl --failed --type=service
#开机并立即启动或停止
systemctl enable --now httpd
systemctl disable --now httpd
#查看服务的依赖关系:
systemctl list-dependencies name.service
#杀掉进程:
systemctl kill unitname
服务状态:
#显示状态
systemctl list-unit-files --type service --all
使用实例:
#显示所有单元状态
systemctl 或 systemctl list-units
#只显示服务单元的状态
systemctl --type=service
#显示sshd服务单元
systemctl –l status sshd.service
#验证sshd服务当前是否活动
systemctl is-active sshd
#启动,停止和重启sshd服务
systemctl start sshd.service
systemctl stop sshd.service
systemctl restart sshd.service
#重新加载配置
systemctl reload sshd.service
#列出活动状态的所有服务单元
systemctl list-units --type=service
#列出所有服务单元
systemctl list-units --type=service --all
#查看服务单元的启用和禁用状态
systemctl list-unit-files --type=service
#列出依赖的单元
systemctl list-dependencies sshd
#验证sshd服务是否开机启动
systemctl is-enabled sshd
#禁用network,使之不能自动启动,但手动可以
systemctl disable network
#启用network
systemctl enable network
#禁用network,使之不能手动或自动启动
systemctl mask network
#启用network
systemctl unmask network
对于新创建的unit文件,或者修改了unit文件,需要通知systemd重载此配置文件,而后可以选择重启
systemctl daemon-reload