centos 7 启动流程总结
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类型 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 目录
管理系统服务:
CentOS 7:service unit 注意:能兼容早期的服务脚本
命令:systemctl COMMAND name.service
启动命令:service name start ==>systemctl start name[.service]
停止:service name stop ==>systemctl stop name.service
重启:service name restarrt ==> systemctl restart name.service
条件式重启:已启动才重启,否则不做操作:service name condrestart ==> 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-activite name.service
查看所有已激活的服务:
systemctl list-units --type|-t service
查看所有服务:
systemctl list-units --type service --all|-a
chkconfig命令的对应关系:
设定某服务开机自启动:
chkconfig name on ==>systemctl enable name.service
设定某服务禁止启动:
chkconfig name off ==>systemctl disable name.service
查看所有服务的开机自启状态:
chkconfig --list ==> systemctl list-unit-file --type service
用来列出该服务在那些运行级别下启用和禁止
chkconfig sshd -list ==> ls /etc/systemd/system/*.wants/sshd.servicce
查看服务是否开机自启:
systemctl is-enabled name.service
其他命令:
查看服务的依赖关系:
systemctl list-dependencies name.service
杀掉进程:
systemctl kill unitname
systemctl list-unit-files --type service --all 显示状态
显示所有单元的状态:
systemctl 或 systemctl list-units
只显示服务单元的状态
systemctl --type=service
查看服务单元的启用和禁用状态:
systemctl list-unit-files --type=service
列出失败的服务:
systemctl --failed --type=service
service unit 文件格式
/etc/systemd/system :系统管理元和用户使用
/usr/lib/systemd/system :发行版打包者使用
sevice 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:定义unit间的冲突关系
Service段的常用选项:
Type:定义影响ExecStart及相关数据的功能的unit进程启动类型
simple:默认值,这个daemon主要由ExecStart指令串来启动,启动后常驻于内存中
forking:由ExecStart启动的程序透过spawns延伸出其他程序来作为此daemon的主要服务,原生父程序在启动结束后就会终止
oneshot:与simple类似,不过这个程序在工作完毕后就结束了,不会常驻在内存中。
dbus:与simple类似,但这个daemon必须要在取得一个D-Bus 的名称后才会继续运作,因此通常也要同时设定BusName= 才行
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文件,要通知system重载此配置文件,而后可以选择重启
systemctl daemon-reload
运行级别:
unit配置文件:.target
ls /usr/lib/systemd/system/*.target
systemctl list-unit-files --type target --all
切换运行级别:init N ==> systemctl isolate name.target
例如:systemctl isolate multi-usr.target
注意:只有/lib/systemd/system/*.target文件中AllowIsolate=yes 才能切换修改后需执行systemctl daemon-reload 才能生效。
查看target:
runlevel ; who -r
systemctl list-units --type target
获取默认运行级别:
/etc/inittab ==> systemctl get-default
修改默认级别:
/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
centos 7 排错:
实验一:删除/boot/grub2/grub.cfg文件
恢复方法:(1)进入救援模式:chroot /mnt/sysimage然后使用命令grub2-mkconfig -o /boot/grub2/grub.cfg或者grub2-mkconfig > /boot/grub2/grub.cfg 就可以恢复此文件.
恢复方法(2):不进入救援模式手动编辑:
insmod
set root=(hd0,1)
linux16 /vmlinuz-3.10.0-693.el7.x86_64 ro root=/dev/sda* selinux=0
initrd16 /initramfs-3.10.0-693.el7.x86_64.img
这样也能恢复此文件
实验二:删除/boot/grub2文件:
恢复:进入救援模式,chroot /mnt/sysimage 然后恢复grub2使用命令:grub2-install /dev/sda 然后恢复grub.cfg:grub2-mkconfig -o /boot/grub2/grub.cfg即可恢复。如果删除里/boot 则在此基础上再加一步安重装内核就可以恢复了。