systemd:
POTS–>boot Sequence(BIOS)–>Bootloader(MBR)–>kernel(ramdisk)–>rootfs(swithroot)–>/sbin/init
init:
CentOS 5 :SysV init
CentOS 6:UPstart
CentOS 7:systemd
systemd的新特性:
系統引導時實現服務并行啟動;
按需激活進程;
系統狀態快照;
基于依賴關系定義服務控制邏輯;
同時采用socket式與D-bus總線式激活服務
核心概念:unit
unit由其相關配置文件進行標識,識別和配置;文件中主要包含了系統服務,監聽的socket,保存的快照以及其他的與init相關的信息;
這些配置文件主要保存在/usr/lib/systemd/system,/run/systemd/system,/etc/systemd/system
配置文件:
/usr/lib/systemd/system:每個服務最主要的啟動腳本設置,類似于之前的/etc/init.d/
/run/systemd/system:系統執行過程中所產生的服務腳本,比上面目錄優先運行
/etc/systemd/system:管理員建立的執行腳本,類似與/etc/rc.d/rcN.d/Sxx類的功能,比上面目錄優先運行
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 腳本;
/etc/init.d/
不兼容:
systemctl的命令是固定不變的;
非由systemd啟動的服務,systemctl無法與之通信
管理系統服務:
CentOS 7:service類型的unit文件;
systemctl命令:
systemctl – Control the systemd system and service manager
格式為:systemctl [OPTIONS…] COMMAND [NAME…]
命令對比:
啟動:service NAME start ===》systemctl start NAME.Sservice
停止:service NAME stop ===》systemctl stop NAME.Sservice
重啟:service NAME restart===》systemctl restart NAME.Sservice
狀態:service NAME status===》systemctl status NAME.Sservice
條件時重啟:service NAME condrestart ===>systemctl try-restart NAME.service
centos 7. 如果服務此前是啟動的,就重啟,如果沒有啟動就不管了;
centos 6. 如果服務此前是啟動的,就重啟,如果沒有啟動就將它啟動
重載或重啟服務:systemctl reload-or-restart NAME.service
centos 7 如果沒服務支持重載就重載此服務,如果不支持,就重啟此服務
重載或條件式重啟服務:systemctl reload-or-try-restart NAME.service
centos 7 如果沒服務支持重載就重載此服務,如果不支持,就嘗試重啟此服務
查看某服務當前激活與否的狀態:systemctl is-active NAME.service
查看所有已激活的服務:chkconfig –list ====>systemctl list-units –type service(簡寫為-t)
查看所有的服務:chkconfig –list ====>systemctl list-units –type service –all (簡寫為-t,-a)
設置服務開機自啟:chkoncfig NAME on ====>systemctl enable NAME.service
禁止服務開機自啟:chkoncfig NAME off ====>systemctl disable NAME.service
查看某服務是否能開機自啟:chkconfig –list NAME ===>systemctl is-enabled NAME.service
禁止某服務設定為開機自啟:systenctl mask NAME.service
取消禁止某服務設定為開機自啟:systenctl unmask NAME.service
查看服務的依賴關系:systemctl list-dependencies NAME.service
查看所有服務的開機自啟狀態:systemctl list-unit-files –type service
殺掉進程:
systemctl kill 進程名
列出失敗的服務
服務狀態:
systemctl list-units –type service –all 顯示狀態
loaded:Unit配置文件已處理
active(running):一次或多次持續處理的運行
active(exited):成功完成一次性的配置
active(running):運行中,等待一個事件
inactive:不允許
enbled:開機啟動
disabled:開機不啟動
static:開機不啟動,但可被另一個啟用的服務激活
管理target units:
unit配置文件:.target
ls /usr/lib/systemd/system/*.target
systemctl list-unit-files –type target –all
查看所有級別:systemcl list-units –type target –all
運行級別:
0====> runlever0.target,poweroff.target
1====> runlever1.target ,recuse.target
2====> runlever2.target, multi-user.target
3====> runlever3.target, multi-user.target
4====> runlever4.target, multi-user.target
5====> runlever4.target , graphical.target
6====> runlever4.target,reboot.target
級別切換: init N ====>systemctl isolate NAME.target
例如:切換到圖形界面 :systemctl isolate graphical.target
查看級別:runlevel who -r ===>systemcl list-units –type target
查看所有級別:systemcl list-units –type target –all
注: 只有/lib/systemd/system/*.target 文件中AllowIsolate=yes 才能切換( 修改文件需執行systemctl daemon-reload 才能生效)
獲取默認運行級別:systemctl get-default
修改默認運行級別:systemctl set-default NAME.target
運行原理其實就是創建和刪除軟鏈接文件
切換值緊急救援模式:systemctl rescue(級別1,會執行/etc/rc.d/init)
切換值emergency模式:systemctl emergency(最簡潔的模式,)
其他常用命令:
關機:systemctl halt, systemctl poweroff
重啟:systemctl reboot
掛載:systemctl suspend
快照:systemctl hibernate
快照并掛起:systemctl hybrid-sleep
service unit file:
相關布爾值,1 、yes 、on 、true 都是開啟,0 、no 、off、false
文件通常有三部分組成:
[Unit] :定義與unit類型無關的通用選項;用于提高unit的描述信息,unit行為及依賴關系等;
[Service] :定義與特定類型相關的專業選項;此處為service
[Install] :定義有“systemctl enable”以及“systemctl disable ”命令在實現服務啟動和禁用時用到的一些選項
unit段的常用選項:
Description:描述信息,意義性描述
After:定義unit的啟動次序;表示當前unit應該晚于那些unit啟動;其功能與before相反
Wants:依賴到的其他units;(弱依賴),被依賴的units無法激活時,當前unit可以激活
Requies:依賴到的其他units;(強依賴),被依賴的units無法激活時,當前unit即無法激活
Conflicts:定義units間的沖突關系;
service段的常用選項:
type:用于定義影響ExecStart及相關參數的功能的unit進程啟動類型
類型:
simple:由ExecStart所指明的程序啟動起來的服務就是主進程(默認值,這個daemon 主要由ExecStart 接的指令串來啟動
,啟動后常駐于內存中)
forking:由ExecStart所指明的程序啟動起來的服務生成的一個子進程將成為主進程,啟動完成后,父進程會退出
oneshot:與simple 類似,不過這個程序在工作完畢后就結束了,不會常駐在內存中
dbus:與simple 類似,但這個daemon 必須要在取得一個D-Bus的名稱后,才會繼續運作. 因此通常也要同時設定BusNname= 才行
notify:類似于simple,在啟動完成后會發送一個通知消息。還需要配合
idle:與simple 類似,要執行這個daemon 必須要所有的工作都順利執行完畢后才會執行。這類的daemon 通常是開機到最后才執行即可的服務
EnvironmentFile:定義環境配置文件(此文件會在Exer之前讀取,并為Exec提供變量)
ExecStart:指明啟動unit要運行命令或腳本;ExecStartPre(預運行),ExecStartPost(后運行)
ExecStop:指明停止unit要運行的命令或腳本
Restart:當設定Restart=1 時,則當次daemon 服務意外終止后,會再次自動啟動此服務
Install段的常用選項
Alias:
RequiredBy:被哪些units所依賴(強依賴)
WantdBy:被哪些units所依賴(若依賴)
Also:安裝本服務的時候還要安裝別的相關服務
注意:對于新創建的unit文件或修改了的unit文件,要通知systemd重載此配置文件
#systemctl daemon-reload
CentOS 7引導順序
1.UEFi 或BIOS 初始化,運行POST開機自檢
2.選擇啟動設備
3.引導裝載程序,cetnos 7 是gurb2
4.加載裝載程序的配置文件:/etc/grub.d/ /etc/default/grub /boot/grub2/grub.cfg
5.加載initramfs驅動模塊
6.加載內核模塊
7.內核初始化,centos 7使用systemd代替了init
8.執行initrd.target所有單元,包括掛載/etc/fatab
9.從initramfs根文件系統切換到磁盤根目錄
10systemd執行默認target配置,配置文件/etc/systemd/default.target /etc/systemd/system/
11.systemd執行sysinit.target初始化系統以及basic.target準操作系統
12.systemd啟動multi-user.target下的本機與服務器服務
13.systemd執行multi-user.target下/etc/rc.d/rc.local
14.systemd執行multi-user.target下的getty.target及登入服務
15.systemd執行graphical需要的服務
設置內核參數
設置內核參數,只影響當次啟動
啟動時,在linux16 行后添加
systemd.unit=desired.target
systemd.unit=emergency.target
systemd.unit=recure.target
recure.target比emergency.targe支持更多的功能,例如日志等
啟動排錯
文件系統損壞
先嘗試自動修復,失敗則進入emergency shell,提示用戶修復
在 在/etc/fstab 不存在對應的設備和UUID等一段時間,如不可用,進入emergency shell
在/etc/fstab 不存在對應掛載點systemd嘗試創建掛載點,否則提示進入emergency shell.
在/etc/fstab 不正確的掛載選項,提示進入emergency shell
破解root口令
1.啟動時任意鍵暫停啟動
2.按e鍵進入編輯模式
3.將光標移動linux16 開始的行,添加內核參數rd.break
4.按ctrl+x啟動
5.mount –o remount,rw /sysroot 因為是只讀掛載,所要重新掛載,好切換到root環境
6.chroot /sysroot切換到root環境
7.修改密碼
8.touch /.autorelabel(必須有此步驟)
9.exit退出,重啟,登入成功
修復grub2
GRUB “the Grand Unified Bootloader” 引導提示時可以使用命令行界面,可從文件系統引導
主要配置文件:/boot/grub2/grub.cfg
修復配置文件:
grub2-mkconfig> /boot/grub2/grub.cfg
修復grub(和centos 6的修復一樣,就是命令換成了grub2-install,這里就不詳細說了,參照前次博客)
grub2-install /dev/sda BIOS環境
grub2-install UEFI環境
原創文章,作者:qiuwei,如若轉載,請注明出處:http://www.www58058.com/48257