啟動流程
POST --> Boot Sequence --> Bootloader --> kernel + initramfs(initrd) --> rootfs --> /sbin/init
init: CentOS 5: SysV init
CentOS 6: Upstart
CentOS 7: Systemd
- Systemd
系統(tǒng)啟動和服務(wù)器守護(hù)進(jìn)程管理器,負(fù)責(zé)在系統(tǒng)
啟動或運行時,激活系統(tǒng)資源,服務(wù)器進(jìn)程和其它進(jìn)程
- Systemd新特性
系統(tǒng)引導(dǎo)時實現(xiàn)服務(wù)并行啟動
按需啟動守護(hù)進(jìn)程
自動化的服務(wù)依賴關(guān)系管理
同時采用socket式與D-Bus總線式激活服務(wù)
系統(tǒng)狀態(tài)快照
- 核心概念:unit
unit表示不同類型的systemd對象,通過配置文件進(jìn)行標(biāo)識和配置;文件中主要包含了系統(tǒng)服務(wù)、監(jiān)聽socket 、保存的
系統(tǒng)快照以及其它與init 相關(guān)的信息
- 配置文件:
/usr/lib/systemd/system: 每個 服務(wù)最主要的啟動腳本設(shè)置,類似于之前的/etc/init.d/
/run/systemd/system :系統(tǒng)執(zhí)行過程中所產(chǎn)生的服務(wù)腳本,比上面目錄優(yōu)先運行
/etc/systemd/system :管理員建立的執(zhí)行腳本,類似于/etc/rc.d/rcN.d/Sxx類的功能,比上面目錄優(yōu)先運行
Unit類型
Systemctl –t help 查看unit 類型
Service unit:文件擴(kuò)展名為.service,用于定義系統(tǒng)服務(wù)
Target unit:文件擴(kuò)展名為.target,用于模擬實現(xiàn)運行級別
Device unit:.device,用于定義內(nèi)核識別的設(shè)備
Mount unit: .mount,定義文件系統(tǒng)掛載點
Socket unit:.socket,用于標(biāo)識進(jìn)程間通信用的socket文件,也可在系統(tǒng)啟動時,延遲啟動服務(wù),實現(xiàn)按需啟動
Snapshot unit: .snapshot,管理系統(tǒng)快照
Swap unit:.swap,用于標(biāo)識swap設(shè)備
Automount unit: .automount,文件系統(tǒng)的自動掛載點
Path unit:.path,用于定義文件系統(tǒng)中的一個文件或目錄使用,常用于當(dāng)文件系統(tǒng)變化時,延遲激活服務(wù),如:spool目錄
system特性
關(guān)鍵特性:
基于socket的激活機(jī)制socket與服務(wù)程序分離
基于d-bus的激活機(jī)制:
基于device的激活機(jī)制:
基于path的激活機(jī)制:
系統(tǒng)快照:保存各unit 的當(dāng)前狀態(tài)信息于持久存儲設(shè)備中
向后兼容sysv init腳本
不兼容:
systemctl命令固定不變,不可擴(kuò)展
非由systemd 啟動的服務(wù),systemctl
system管理服務(wù)
管理系統(tǒng)服務(wù):
CentOS 7: service unit
注意:能兼容早期的服務(wù)腳本
命令: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
狀態(tài):service name status ==> systemctl status name.service
條件式重啟:已啟動才重啟,否則不做操作
service name condrestart ==> systemctl try-restart name.service
重載或重啟服務(wù):先加載,再啟動
systemctl reload-or-restart name.service
重載或條件式重啟服務(wù):
systemctl reload-or-try-restart name.service
禁止自動和手動啟動:
systemctl mask name.service
取消禁止:
systemctl unmask name.service
查看system服務(wù)
查看某服務(wù)當(dāng)前激活與否的狀態(tài):
systemctl is-active name.service
查看所有已經(jīng)激活的服務(wù):
systemctl list-units --type|-t service
查看所有服務(wù):
systemctl list-units --type service –all|-a
chkconfig命令的對應(yīng)關(guān)系:
設(shè)定某服務(wù)開機(jī)自啟:
chkconfig name on ==> systemctl enable name.service
設(shè)定某服務(wù)開機(jī)禁止啟動:
chkconfig name off ==> systemctl disable name.service
查看所有服務(wù)的開機(jī)自啟狀態(tài):
chkconfig --list ==> systemctl list-unit-files --type service
用來列出該服務(wù)在哪些運行級別下啟用和禁用
chkconfig sshd –list ==> ls /etc/systemd/system/*.wants/sshd.service
查看服務(wù)是否開機(jī)自啟:
systemctl is-enabled name.service
其它命令:
查看服務(wù)的依賴關(guān)系:
systemctl list-dependencies name.service
殺掉進(jìn)程:
systemctl kill 進(jìn)程名
服務(wù)狀態(tài)
systemctl list-unit-files --type service --all 顯示狀態(tài)
loaded:Unit 配置文件已處理
active(running):一次或多次持續(xù)處理的運行
active(exited):成功完成一次性的配置
active(waiting):運行中,等待一個事件
inactive: 不運行
enabled:開機(jī)啟動
disabled:開機(jī)不啟動
static:開機(jī)不啟動,但可被另一個啟用的服務(wù)激活
systemctl命令示例
顯示所有單元狀態(tài)
systemctl 或 或 systemctl list-units
只顯示服務(wù)單元的狀態(tài)
systemctl --type=service
顯示sshd 服務(wù)單元
systemctl status sshd.service –l
驗證sshd 服務(wù)當(dāng)前是否活動
systemctl is-active sshd
啟動,停止和重啟sshd 服務(wù)
systemctl start sshd.service
systemctl stop sshd.service
systemctl restart sshd.service
重新加載配置
systemctl reload sshd.service
列出活動狀態(tài)的所有服務(wù)單元
systemctl list-units --type=service
列出所有服務(wù)單元
systemctl list-units --type=service --all
查看服務(wù)單元的啟用和禁用狀態(tài)
systemctl list-unit-files --type=service
列出失敗的服務(wù)
systemctl --failed --type=service
列出依賴的單元
systemctl list-dependencies sshd
驗證sshd服務(wù)是否開機(jī)啟動
systemctl is-enabled sshd
禁用network,使之不能自動啟動, 但手動可以
systemctl disable network
啟用network
systemctl enable network
禁用network,使之不能手動或自動啟動
systemctl mask network
啟用network
systemctl unmask network
服務(wù)的運行級別
target units:
unit 配置文件:.target
ls /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.target
systemctl isolate multi-user.target
注:只有/lib/systemd/system/*.target文件中AllowIsolate=yes才能切換(修改文件需執(zhí)行systemctl daemon-reload 才能生效)
查看target:
runlevel ; who -r
systemctl list-units --type target
獲取默認(rèn)運行級別:
/etc/inittab ==> systemctl get-default
修改默認(rèn)級別:
/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
其它常用命令:
傳統(tǒng)命令init,poweroff,halt,reboot都成為systemctl的軟鏈接
關(guān)機(jī):systemctl halt、systemctl poweroff
重啟:systemctl reboot
掛起:systemctl suspend
休眠:systemctl hibernate
休眠并掛起:systemctl hybrid-sleep
Centos 7引導(dǎo)順序
UEFi或BIOS初始化,運行POST開機(jī)自檢
選擇啟動設(shè)備
引導(dǎo)裝載程序, centos7 是grub2
加載裝載程序的配置文件:/etc/grub.d/
/etc/default/grub /boot/grub2/grub.cfg
加載initramfs 驅(qū)動模塊
加載內(nèi)核選項
內(nèi)核初始化,centos7 使用systemd 代替init
執(zhí)行initrd.target所有單元,包括掛載/etc/fstab
從initramfs根文件系統(tǒng)切換到磁盤根目錄
systemd執(zhí)行默認(rèn)target配置,配置文件 /etc/systemd/system/default.target
systemd 執(zhí)行sysinit.target 初始化系統(tǒng)及basic.target準(zhǔn)備操作系統(tǒng)
systemd啟動multi-user.target下的本機(jī)與服務(wù)器服務(wù)
systemd執(zhí)行multi-user.target下的/etc/rc.d/rc.local
Systemd執(zhí)行multi-user.target 下的getty.target 及登錄服務(wù)
systemd執(zhí)行g(shù)raphical
service unit 文件格式
- /etc/systemd/system:系統(tǒng)管理員和用戶使用
/usr/lib/systemd/system:發(fā)行版打包者使用
- 以“#”開頭的行后面的內(nèi)容會被認(rèn)為是注釋
- 相關(guān)布爾值,1 、yes 、on 、true都是開啟,0、no、off、false都是關(guān)閉
- 時間單位默認(rèn)是秒,所以要用毫秒(ms)分鐘(m)等須顯式說明
- service unit file 文件通常由三部分組成:
[Unit]:定義與Unit 類型無關(guān)的通用選項;用于提供unit的描述信息、unit 行為及依賴關(guān)系等
[Service]:與特定類型相關(guān)的專用選項;此處為Service 類型
[Install] :定義由“systemctl enable” 以及"systemctl disable“命令在實現(xiàn)服務(wù)啟用或禁止時用到的一些選項
Unit段的常用選項:
Description:描述信息
After:定義unit 的啟動次序,表示當(dāng)前unit 應(yīng)該晚于哪些unit啟動,其功能與Before 相反
Requires:依賴到的其它units ,強(qiáng)依賴,被依賴的units 無法激活時,當(dāng)前unit也無法激活
Wants:依賴到的其它units,弱依賴
Conflicts :定義units
Service段的常用選項:
Type:定義影響ExecStart 及相關(guān)參數(shù)的功能的unit 進(jìn)程啟動類型
simple:默認(rèn)值,這個daemon 主要由ExecStart接的指令串來啟動,啟動后常駐于內(nèi)存中
forking:由ExecStart 啟動的程序透過spawns 延伸出其他子程序來作為此daemon的主要服務(wù)。原生父程序在啟動結(jié)束后就會終止
oneshot:與simple 類似,不過這個程序在工作完畢后就結(jié)束了,不會常駐在內(nèi)存中
dbus:與simple類似,但這個daemon必須要在取得一個D-Bus的名稱后,才會繼續(xù)運作.因此通常也要同時設(shè)定BusNname=才行
notify:在啟動完成后會發(fā)送一個通知消息。還需要配合NotifyAccess來讓Systemd接收消息
idle:與simple類似,要執(zhí)行這個daemon必須要所有的工作都順利執(zhí)行完畢后才會執(zhí)行。這類的daemon通常是開機(jī)到最后才執(zhí)行即可的服務(wù)
EnvironmentFile:環(huán)境配置文件
ExecStart:指明啟動unit 要運行命令或腳本的絕對路徑
ExecStartPre:ExecStart前運行
ExecStartPost:ExecStart后運行
ExecStop:指明停止unit要運行的命令或腳本
Restart:當(dāng)設(shè)定Restart=1時,則當(dāng)次daemon服務(wù)意外終止后,會再次自動啟動此服務(wù)
Install段的常用選項:
Alias:別名,可使用systemctl command Alias.service
RequiredBy:被哪些units所依賴,強(qiáng)依賴
WantedBy:被哪些units所依賴,弱依賴
Also:安裝本服務(wù)的時候還要安裝別的相關(guān)服務(wù)
注意:對于新創(chuàng)建的unit文件,或者修改了的unit文件,要通知systemd重載此配置文件,而后可以選擇重啟
systemctl daemon-reload
服務(wù)Unit文件示例:
vim /etc/systemd/system/bak.service
[Unit]
Description=backup /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
設(shè)置內(nèi)核參數(shù)
- 設(shè)置內(nèi)核參數(shù),只影響當(dāng)次啟動
- 啟動時,在linux16行后添加systemd.unit=desired.target
- systemd.unit=emergency.target
- systemd.unit=recure.target
- recure.target 比emergency 支持更多的功能,例如日志等
啟動排錯
- 文件系統(tǒng)損壞
先嘗試自動修復(fù),失敗則進(jìn)入emergency shell,提示用戶修復(fù)
- 在/etc/fstab不存在對應(yīng)的設(shè)備和UUID
等一段時間,如不可用,進(jìn)入emergency shell
- 在/etc/fstab不存在對應(yīng)掛載點
systemd嘗試創(chuàng)建掛載點,否則提示進(jìn)入emergency shell.
- 在/etc/fstab不正確的掛載選項
提示進(jìn)入emergency shell
破解Centos 7的root密碼
方式一
啟動時任意鍵暫停啟動
按e鍵進(jìn)入編輯模式
將光標(biāo)移動linux16 開始的行,添加內(nèi)核參數(shù)rd.break
按ctrl-x 啟動
mount –o remount,rw /sysroot
chroot /sysroot
passwd root
touch /.autorelabel
exit
reboot
方式二
啟動時任意鍵暫停啟動
按e鍵進(jìn)入編輯模式
將光標(biāo)移動linux16 開始的行,改為rw init=/sysroot/bin/sh
按ctrl-x 啟動
chroot /sysroot
passwd root
touch /.autorelabel
exit
reboot
修復(fù)GRUB2
- GRUB “the Grand Unified Bootloader”
引導(dǎo)提示時可以使用命令行界面
可從文件系統(tǒng)引導(dǎo)
- 主要配置文件 /boot/grub2/grub.cfg
- 修復(fù)配置文件
grub2-mkconfig > /boot/grub2/grub.cfg
grub2-install /dev/sda BIOS 環(huán)境
grub2-install UEFI 環(huán)境
vim /etc/default/grub
GRUB_DEFAULT=0
練習(xí)
- 為編譯安裝的httpd 服務(wù),實現(xiàn)service unit文件
- 破解centos7 口令
- 修改默認(rèn)的啟動內(nèi)核為新編譯內(nèi)核
- 啟動時臨時禁用SELinux
- 啟動時進(jìn)入emergency 模式
- 卸載編譯安裝的新內(nèi)核