12.1.1 systemd 特性
systemd:从 rhel 7 版本之后开始用 systemd 实现init进程,系统启动和服务器守护进程管理器,
负责在系统启动或运行时,激活系统资源,服务器进程和其它进程
Systemd新特性
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
unit的配置文件
/usr/lib/systemd/system:每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/
/run/systemd/system:系统执行过程中所产生的服务脚本,比上面目录优先运行
/etc/systemd/system:管理员建立的执行脚本,类似于/etc/rcN.d/Sxx的功能,比上面目录优先运行
12.2.1 systemctl管理系统服务service unit
命令:
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 或 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
/etc/systemd/system:系统管理员和用户使用
/usr/lib/systemd/system:发行版打包者使用
帮助参考:
systemd.directives(7),systemd.unit(5),systemd.service(5), systemd.socket(5),
systemd.target(5),systemd.exec(5)
unit 格式说明:
service unit file文件通常由三部分组成:
Unit段的常用选项:
Service段的常用选项:
。 simple:默认值,这个daemon主要由ExecStart接的指令串来启动,启动后常驻于内存中
。forking:由ExecStart启动的程序透过spawns延伸出其他子程序来作为此daemon的主要服务。原
生父程序在启动结束后就会终止
。 oneshot:与simple类似,不过这个程序在工作完毕后就结束了,不会常驻在内存中
。 dbus:与simple类似,但这个daemon必须要在取得一个D-Bus的名称后,才会继续运作.因
此通常也要同时设定BusNname= 才行
。 notify:在启动完成后会发送一个通知消息。还需要配合 NotifyAccess 来让 Systemd 接收消
息
。idle:与simple类似,要执行这个daemon必须要所有的工作都顺利执行完毕后才会执行。这
类的daemon通常是开机到最后才执行即可的服务
目录
Install段的常用选项:
注意:对于新创建的unit文件,或者修改了的unit文件,要通知systemd重载此配置文件,而后可以选择重启
systemctl daemon-reload
12.2.3 systemctl管理运行级别
target units:相当于Rhel 6之前的runlevel ,unit配置文件:.target
ls /usr/lib/systemd/system/*.target
systemctl list-unit-files --type target --all
和运行级别对应关系init
0 ==> runlevel0.target, poweroff.target
1 ==> runlevel1.target, rescue.target
2 ==> runlevel2.target, multi-user.target
3 ==> runlevel3.target, multi-user.target
4 ==> runlevel4.target, multi-user.target
5 ==> runlevel5.target, graphical.target
6 ==> runlevel6.target, reboot.target
查看依赖性:
systemctl list-dependencies graphical.target
级别切换:相当于 init N
systemctl isolate name.target
进入默认target
systemctl default
范例:
#切换至字符模式,相当于init 3
systemctl isolate multi-user.target
阅读量:2003
点赞量:0
收藏量:0