1、系统启动的流程回顾:
post(加电自检,主要用来检查支持系统启动的硬件环境是否满足)---> bootseqence(根据bios中设定启动顺序,去加载相应的启动项设备)---> bootloader(根据启动项设备的mbr去加载bootloader)---> kernel(加载内核至内存中)---> rootfs(根切换)---> init(/sbin/init)
不同系统的init程序不一样:
centos5:sysV init //V表示版本号,第五版
centos6:upstart
centos7:systemd
2、systemd新特性介绍:
系统引导时实现服务并行启动
按需激活进程:意思是只有需要此进程运行时,才激活进程
支持系统状态快照
基于依赖关系定义的服务控制逻辑。
3、systemd核心概念:unit
unit由其相关的配置文件进行标识识别和配置,文件中主要包含了:系统服务、监听的socket、保存的快照以及其他的unit相关的信息,这些配置文件主要保存在/usr/lib/systemd/system,以及/etc/systemd/system/,以及/run/systemd/system/目录下。
unit常见的类型:
unit类型 | 文件扩展名 | 用途 | 备注 |
service unit | .service | 定义系统服务 | |
taget unit | .target | 用于模拟实现init的运行级别 | |
device unit | .device | 用于定义内核识别的设备 | |
mount unit | .mount | 用于定义文件系统挂载点 | |
scoket unit | .scoket | 用于标识进程间通信用到的socket文件 | |
snapshot unit | .snapshot | 用于管理系统快照 | |
swap unit | .swap | 用于标识swap设备 | |
automount unit | .automount | 文件系统自动挂载点设置 | |
path unit | .path | 用于定义文件系统中的一个文件或目录。 |
4、systemd的关键特性:
基于socket的激活机制,socket与程序分离
基于bus的激活机制
基于device的激活机制
机制path的激活机制
系统快照:保存各unit的当前状态信息于持久存储设备中
向后兼容sysV init脚本:存放在/etc/init.d/目录下的脚本
5、systemd不兼容的功能特性:(与老版本不兼容的一些的特性)
systemctl的命令时固定不变的
非由systemd启动的服务,systemctl无法与之通信,即systemctl无法控制此服务。6
6、centos7管理系统服务
centos7:service类型的unit文件
systemctl命令:#man systemctl
systemctl [OPTIONS...] COMMAND [NAME...]
centos7 | centos6 | 备注 | |
启动服务 | systemctl start NAME[.service] | service NAME start | |
停止服务 | sytemctl stop NAME[.service] | service NAME stop | |
重启服务 | systemctl restart NAME[.service] | service NAME restart | |
显示状态 | systemctl status NAME[.service] | service NAME status | |
条件式重启 | systemctl try-restart NAME[.service] | service NAME condrestart | |
重载或重启服务 | systemctl reload-or-restart NAME[.service] //如果支持重载就重载服务,不支持就重启服务 | 不支持 | 重新读取配置文件,而不用重启服务 |
重启或条件式重启服务 | systemctl reload-or-tty-restart NAME[.service] | 不支持 | |
查看某服务当前激活与否状态 | systemctl is-active NAME[.service] 如: ~]# systemctl is-active crond active | ||
查看所有已激活的服务 | ~]# systemctl list-units --type service | ||
查看所有服务(含已激活和未激活的) | # systemctl list-units --type service --all | chkconfig --list | |
设置服务随系统引导自动开启(开机自启服务) | systemctl enable NAME[.service] | chkconfig NAME ON | |
禁止指定服务开机自启动 | systemctl disable NAME[.service] | chkconfig NAME off | |
查看某服务能否开机自启动 | systemctl is-enabled NAME[.service] 如: # systemctl is-enabled crond enabled | chkconfig --list NAME | |
禁止某服务设置为开机自启 | systemctl mask NAME.[service] 如: [root@localhost ~]# systemctl mask crond | ||
取消禁止某服务设置为开机自启 | systemctl unmask NAME.[service] | ||
查看服务的依赖关系 | systemctl list-dependencies NAME.[service] 如: ~]# systemctl list-dependencies crond crond.service | ||
关机 | systemctl halt systemctl poweroff | ||
重启 | systemctl reboot | ||
挂起 | systemctl suspend | ||
快照 | systemctl hibernate | ||
快照并挂起 | systemctl hybrid-sleep |
6.1、target unit:管理运行级别的,
运行级别:
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
[root@localhost system]# cat /etc/inittab # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target # # systemd uses 'targets' instead of runlevels. By default, there are two main targets: # # multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5 # # To view current default target, run: # systemctl get-default # # To set a default target, run: # systemctl set-default TARGET.target # [root@localhost system]# |
6.2、运行级别runlevel#.target之间切换
centos6:init #
centos7:systemctl isolate NAME.target
6.3、查看当前运行级别:
centos5/6:#runlevel
或 #who -r
centos7:~]# systemctl list-units -t target
6.4、查看所有运行级别:含未激活的
~]# systemctl list-units -t target --all
6.5、获取默认运行级别:
[root@localhost ~]# systemctl get-default
graphical.target
[root@localhost ~]#
6.6、修改默认运行级别:
sytemctl set-default NAME.target
6.7、切换至紧急救援模式:
#systemctl rescue
6.8、切换至emergency模式:
#systemctl emergency
7、service unit
7.1service unit文件位置:/usr/lib/systemd/system,
[root@localhost ~]# ll /usr/lib/systemd/system/default.target
lrwxrwxrwx. 1 root root 16 Aug 29 20:05 /usr/lib/systemd/system/default.target -> graphical.target
[root@localhost ~]#
注意:默认/etc/systemd/system/目录下的文件多为开机自动启动的默认服务,一般是链接文件,链接到/usr/lib/systemd/sytem目录下的相应服务。
7.2、service unit file格式规范:以/usr/lib/systemd/system/httpd.service 为例介绍:
文件通常由三部分组成:[unit] 、[service] 、[install]
[unit]:
定义与unit类型无关的通用选项,用于提供unit的描述信息,unit行为及依赖关系
如:
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[unit]常用选项:
Description:描述信息,服务的意义性描述
After:定义unit的启动次序,表示当前的unit应该晚于哪些unit启动,其功能与Before相反
Wants:依赖到的其他unit,弱依赖关系,被依赖的unit无法激活时,对当前的unit没有影响
Requies:依赖到的其他unit,强依赖关系,被依赖的unit无法激活时,则当前的unit无法激活
Conflicts:定义unit间的冲突关系
[service]:
定义与特定类型相关的专用选项,此处为service类型
如:
[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
[service]常用选项:
Type:用于定义影响Execstart及相关参数的功能的unit进程启动类型。
unit进程启动的类型:simple、forking、oneshot、dbus、notify、idel
simple:有Execstart启动的进程是主进程
forking:有Execstart启动的进程中的一个子进程为主进程。
notify:类似simple
EnvironmentFile:环境配置文件,一般放在命令之前
ExecStart:指明启动unit要运行的命令或脚本,ExecStartpre,ExecStartposr
ExecStop:指明停止unit要运行的命令或脚本
[install]:
定义有“systemctl enable”以及“systemctl disable”命令在实现服务启用或禁用时用到的一些选项。
如:
[Install]
WantedBy=multi-user.target
[install]常用选项:
Alias:当前unit所依赖
RequiredBy:被哪些units所依赖
WantedBy:被哪些units所依赖
注意:对于新创建的unit文件或被修改的unit文件,要通知systemctl重载此配置文件。(#systemctl daemon-reload )
8、练习:参考上述,为当前系统的httpd服务提供一个unit文件。