systemd管理实践指南
管理systemd
CentOS 7 使用systemd替换了SysV。Systemd目的是要取代Unix时代以来一直在使用的init系统,兼容SysV和LSB的启动脚本,而且够在进程启动过程中更有效地引导加载服务。
systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。
Systemd :系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进程和其它进程
Systemd 新特性:
系统引导时实现服务并行启动 按需启动守护进程 自动化 的 服务 依赖 关系管理 同时 采用socket 式与D-Bus 总线式激活 服务 系统状态快照
核心概念:unit
unit 表示不同类型的systemd 对象,通过配置文件进行标识和配置;文件中主要包含了系统服务、监听socket 、保存的系统快照以及其它与init
配置文件:
/usr/lib/systemd/system: 每个 服务最主要的启动脚本设置 ,类似于之前的/etc/init.d/ /run/systemd/system :系统执行过程中所产生的服务脚本,比上面目录优先运行 /etc/systemd/system :管理员建立的执行脚本,类似于 于/etc/rc.d/rcN.d/Sxx 类的功能,比上面目录优先运行
Unit类型
Systemctl –t help 查看unit 类型
[root@localhost ~]# systemctl -t help Available unit types: service socket busname target snapshot device mount automount swap timer path slice scope [root@localhost ~]# ll /usr/lib/systemd/system | head -5 total 1220 -rw-r--r--. 1 root root 275 Dec 1 2015 abrt-ccpp.service -rw-r--r--. 1 root root 380 Dec 1 2015 abrtd.service -rw-r--r--. 1 root root 361 Dec 1 2015 abrt-oops.service -rw-r--r--. 1 root root 266 Dec 1 2015 abrt-pstoreoops.service
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 目录 特性
关键特性:
基于socket 的激活机制:socket 与服务程序分离 基于d-bus 的激活机制: 基于device 的激活机制: 基于path 的激活机制: 系统快照:保存各unit 的当前状态信息于持久存储设备中向后兼容sysv init 脚本
不兼容:
systemctl 命令固定不变,不可扩展 非由systemd 启动的服务,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 条件式重启:已启动才重启,否则不做操作: service name cond restart ==> systemctl try-restart name.service 重载或重启服务:先加载,再启动: systemctl reload-or-restart name.service 重载或条件式重启服务:systemctl reload-or-try-restart 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 -a
服务状态
systemctl list-units --type service --all 显示状态 loaded:Unit 配置文件已处理 active(running): 一次或多次持续处理的运行 active(exited): 成功完成一次性的配置 active(waiting): 运行中,等待一个事件 inactive: 不运行 enabled: 开机启动 disabled: 开机不启动 static:开机不启动,但可被另一个启用的服务激活
杀掉进程:
systemctl kill 进程名
chkconfig 命令的对应关系:
设定某服务开机自启: chkconfig name on ==> systemctl enable name.service 设定某服务开机禁止启动: chkconfig name off ==> systemctl disable name.service 查看所有服务的开机自启状态: chkconfig --list ==> systemctl list-unit-files --type service
用来列出该服务在哪些运行级别下启用和 禁用
chkconfig sshd –list ==> ls /etc/systemd/system/*.wants/sshd.service [root@localhost ~]# ls /etc/systemd/system/*.wants/sshd.service /etc/systemd/system/multi-user.target.wants/sshd.service
查看服务是否开机自启:
systemctl is-enabled name.service [root@localhost ~]# systemctl is-enabled sshd.service enabled
其它命令:
查看服务的依赖关系:systemctl list-dependencies name.service
运行级别
target units:
unit 配置文件:.targetls /usr/lib/systemd/system/*.target systemctl list-unit-files --type target --all //查看级别
运行级别:
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.targetsystemctl isolate multi-user.target //切换到3级别
注: 只有/lib/systemd/system/*.target 文件中AllowIsolate=yes 才能切换( 修改文件需执行systemctl daemon-reload 才能生效)
获取默认运行级别
/etc/inittab ==> systemctl get-default [root@localhost ~]# systemctl get-default multi-user.target
修改默认级别
/etc/inittab ==> systemctl set-default name.target systemctl set-default multi-user.target ls –l /etc/systemd/system/default.target
其它命令
#切换至紧急救援模式: systemctl rescue #切换至emergency 模式: systemctl emergency
其它常用命令:
传统命令init ,poweroff ,halt ,reboot 都成为 systemctl 的软链接关机:systemctl halt 、systemctl poweroff 重启:systemctl reboot 挂起:systemctl suspend 休眠: :systemctl hibernate 休眠并挂起:systemctl hybrid-sleep
service unit 文件格式
/etc/systemd/system :系统管理员和用户使用 /usr/lib/systemd/system :发行版打包者使用
实例:
[root@localhost ~]# cat /usr/lib/systemd/system/httpd.service [Unit] Description=The Apache HTTP Server After=network.target remote-fs.target nss-lookup.target Documentation=man:httpd(8) Documentation=man:apachectl(8) [Service] Type=notify EnvironmentFile=/etc/sysconfig/httpd ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND ExecReload=/usr/sbin/httpd $OPTIONS -k graceful ExecStop=/bin/kill -WINCH ${MAINPID} # We want systemd to give httpd some time to finish gracefully, but still want # it to kill httpd after TimeoutStopSec if something went wrong during the # graceful stop. Normally, Systemd sends SIGTERM signal right after the # ExecStop, which would kill httpd. We are sending useless SIGCONT here to give # httpd time to finish. KillSignal=SIGCONT PrivateTmp=true [Install] WantedBy=multi-user.target
service unit file 文件通常由三部分组成:
[Unit] :定义与Unit 类型无关的通用选项;用于提供unit 的描述信息、unit 行为及依赖关系等
[Service] :与特定类型相关的专用选项;此处为Service 类型 [Install] :定义由“systemctl enable” 以及”systemctl disable“
Unit 段的常用选项:
Description :描述信息 After :定义unit 的启动次序,表示当前unit 应该晚于哪些unit 启动,其功能与Before 相反 Requires :依赖到的其它units ,强依赖,被依赖的units 无法激活时,当前unit 即无法激活 Wants :依赖到的其它units, , 弱依赖 Conflicts :定义units
Service 段的常用选项:
Type :定义影响ExecStart 及相关参数的功能的unit 进程启动类型 simple :默认值,这个daemon 主要由ExecStart 接的指令串来启动,启动后常驻于内存中 forking :由ExecStart 启动的程序透过spawns 延伸出其他子程序来作为此daemon 的主要服务。原生父程序在启动结束后就会终止 oneshot :与simple 类似,不过这个程序在工作完毕后就结束了,不会常驻在内存中 dbus :与simple 类似,但这个daemon 必须要在取得一个D-Bus的 的名称后,才会继续运作. 因此通常也要同时设定BusNname= 才行 notify :在启动完成后会发送一个通知消息。还需要配合NotifyAccess 让 来让 Systemd 接收消息 idle :与simple 类似,要执行这个daemon 必须要所有的工作都顺利执行完毕后才会执行。这类的daemon 通常是开机到最后才执行即可的服务
EnvironmentFile :环境配置文件
ExecStart :指明启动unit 要运行命令或脚本的绝对路径 ExecStartPre: : ExecStart 前运行 ExecStartPost: : ExecStart 后运行 ExecStop :指明停止unit 要运行的命令或脚本 Restart :当设定Restart=1 时,则当次daemon 服务意外终止后,会再次自动启动此服务
Install 段的常用选项:
Alias :别名,可使用systemctl command Alias.service RequiredBy :被哪些units 所依赖,强依赖 WantedBy :被哪些units 所依赖,弱依赖 Also :安装本服务的时候还要安装别的相关服务 注意:对于新创建的unit 文件,或者修改了的unit 文件,要通知systemd 重载此配置文件, 而后可以选择重启 systemctl daemon-reload
服务Unit 文件示例
(1)创建一个脚本,用于被创建的服务调用
[root@localhost system]# cat /testdir/bak.sh #!/bin/bash # 备份/etc/目录 tar -Jcvf /testdir/etc-`date +%F`.tar.xz /etc/ &> dev/null
(2)给bak.sh脚本添加执行权限
[root@localhost ~]# chmod u+x /testdir/bak.sh
(3)创建bak.service服务
[root@localhost ~]# vim /etc/systemd/system/bak.service [Unit] Description=backup my etc Requires=atd.service [Service] Type=simple ExecStart=/bin/bash -c "echo /testdir/bak.sh|at now" [Install] WantedBy=multi-user.target systemctl daemon-reload systemctl start bak
(4)启用服务
[root@localhost system]# systemctl daemon-reload [root@localhost system]# systemctl start bak
(5)验证
[root@localhost system]# ll /testdir/ total 8132 -rwxr--r-- 1 root root 91 Sep 21 19:14 bak.sh -rw-r--r-- 1 root root 4546560 Sep 21 19:15 etc-2016-09-21.tar.xz