POST --> Boot Sequence --> Bootloader --> kernel + initramfs(initrd) --> rootfs --> /sbin/init
init:
CentOS 5: SysV init
CentOS 6: Upstart
CentOS 7: Systemd
Systemd新特性:
系統引導時實現服務並行啓動;
按需激活進程;
系統狀態快照;
基於依賴關係定義服務控制邏輯;
核心概念:unit
配置文件進行標識和配置;文件中主要包含了系統服務、監聽socket、保存的系統快照以及其它與init相關的信息;
保存至:
/usr/lib/systemd/system
/run/systemd/system
/etc/systemd/system
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,用於定義文件系統中的一個文件或目錄;
關鍵特性:
基於socket的激活機制:socket與服務程序分離;
基於bus的激活機制:
基於device的激活機制:
基於path的激活機制:
系統快照:保存各unit的當前狀態信息於持久存儲設備中;
向後兼容sysv init腳本;
不兼容:
systemctl命令固定不變
非由systemd啓動的服務,systemctl無法與之通信
管理系統服務:
CentOS 7: 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
條件式重啓: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-active name.service
查看所有已經激活的服務:
systemctl list-units --type service
查看所有服務:
systemctl list-units --type service --all
chkconfig命令的對應關係:
設定某服務開機自啓:chkconfig name on ==> systemctl enable name.service
禁止:chkconfig name off ==> systemctl disable name.service
查看所有服務的開機自啓狀態:
chkconfig --list ==> systemctl list-unit-files --type service
查看服務是否開機自啓:systemctl is-enabled name.service
其它命令:
查看服務的依賴關係:systemctl list-dependencies name.service
target units:
unit配置文件:.target
運行級別:
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
級別切換:
init N ==> systemctl isolate name.target
查看級別:
runlevel ==> systemctl list-units --type target
獲取默認運行級別:
/etc/inittab ==> systemctl get-default
修改默認級別:
/etc/inittab ==> systemctl set-default name.target
切換至緊急救援模式:
systemctl rescue
切換至emergency模式:
systemctl emergency
其它常用命令:
關機:systemctl halt、systemctl poweroff
重啓:systemctl reboot
掛起:systemctl suspend
快照:systemctl hibernate
快照並掛起:systemctl hybrid-sleep