centos7.x系統(tǒng)管理之Systemd詳解
原文鏈接 http://www.aichengxu.com/linux/11318300.htm
說(shuō)明: 本文根據(jù)實(shí)際在centos7.3上的測(cè)試結(jié)果進(jìn)行了改動(dòng)
本文的內(nèi)容包括:
- CentOS7啟動(dòng)
- systemd介紹
- centos 7 引導(dǎo)順序
- service target 文件介紹
- 啟動(dòng)排錯(cuò)
1. CentOS7啟動(dòng)過(guò)程
post-->BISO-->bootloader(MBR)-->kernel(ramdisk)-->rootfs-->/sbin/init
加電->進(jìn)入BIOS->加載磁盤(pán)主引導(dǎo)記錄->加載內(nèi)核->初始化rootfs->系統(tǒng)初始化
1.1 init程序版本
不同系統(tǒng)上的init程序版本
centos5: Sysv lnit
centos6: Upstart
centos7: systemd 系統(tǒng)守護(hù)進(jìn)程
2. systemd介紹
2.1 systemd 簡(jiǎn)介
Systemd是系統(tǒng)啟動(dòng)和服務(wù)器守護(hù)進(jìn)程管理器,負(fù)責(zé)在系統(tǒng)啟動(dòng)或運(yùn)行時(shí),激活系統(tǒng)資源顾腊,服務(wù)器進(jìn)程和其它進(jìn)程
2.2 systemd的新特性
系統(tǒng)引導(dǎo)時(shí)實(shí)現(xiàn)服務(wù)并行啟動(dòng);實(shí)現(xiàn)快速開(kāi)機(jī)
按需啟動(dòng)守護(hù)進(jìn)程:
能自動(dòng)保存系統(tǒng)狀態(tài)快照:
基于依賴關(guān)系定義服務(wù)控制邏輯:(自動(dòng)化的服務(wù)依賴關(guān)系管理)
同時(shí)采用socket式與D-Bus總線式激活服務(wù)
2.3 systemd核心概念
unit(單元):
unit表示不同類(lèi)型的systemd對(duì)象
unit由其相關(guān)的配置文件進(jìn)行標(biāo)識(shí),識(shí)別和配置.
相關(guān)文件中主要包含了系統(tǒng)服務(wù),監(jiān)聽(tīng)的socket,保存的快照以及其他與init相關(guān)的信息;
這些配置文件主要保存在:
/usr/lib/systemd/system:每個(gè)服務(wù)最主要的啟動(dòng)腳本設(shè)置溶其,類(lèi)似于之前的/etc/init.d/
/run/systemd/system:系統(tǒng)執(zhí)行過(guò)程中所產(chǎn)生的服務(wù)腳本东且,比上面目錄優(yōu)先運(yùn)行
/etc/systemd/system:管理員建立的執(zhí)行腳本排苍,類(lèi)似于/etc/rc.d/rcN.d/Sxx類(lèi)的功能,比上面目錄優(yōu)先運(yùn)行
unit的常見(jiàn)類(lèi)別:
[root@test2 system]# systemctl -t help
Available unit types:
service
socket
busname
target
snapshot
device
mount
automount
swap
timer
path
slice
scope
各類(lèi)unit說(shuō)明:
(1) Service unit: 文件擴(kuò)展名為.service,用于定義系統(tǒng)服務(wù),類(lèi)似服務(wù)腳本
(2) Target unit: 文件擴(kuò)展為.target,用于模擬實(shí)現(xiàn)"運(yùn)行級(jí)別";與/etc/inittab不一樣,只是為了對(duì)應(yīng)老版本的系統(tǒng)
(3) Device unit: .device,用于定義內(nèi)核識(shí)別的設(shè)備;centos6下,/dev目錄下的設(shè)備文件是由udev根據(jù)/sys/目錄下由內(nèi)核探測(cè)輸出的信息而創(chuàng)建的,對(duì)centos7來(lái)說(shuō),/dev目錄下是由systemd和udev聯(lián)合創(chuàng)建的,主要由systemd完成.systemd識(shí)別硬件,主要靠*.device文件.
(4) Mount unit: .mount,用于定義文件系統(tǒng)掛載點(diǎn);kernel 3.0版本后的系統(tǒng),大都是用cgroup(k控制組)來(lái)實(shí)現(xiàn)資源分配.mount后看到大量的cgroup信息,cgroup實(shí)現(xiàn)對(duì)資源分配的一種內(nèi)核中的資源分配的機(jī)制.systemd負(fù)責(zé)對(duì)cgroup的激活,實(shí)現(xiàn)資源分配.
(5) Socket unit: .socket,用于標(biāo)識(shí)進(jìn)程間通信用到的socket文件.任何主機(jī)監(jiān)聽(tīng)在一個(gè)套接字上,任何進(jìn)程監(jiān)聽(tīng)在一個(gè)套接字上,或者任何進(jìn)程打開(kāi)一個(gè)隨機(jī)端口去與別的服務(wù)器的進(jìn)程進(jìn)行通道,都要?jiǎng)?chuàng)建socket文件.現(xiàn)在這些創(chuàng)建等功能是有systemd負(fù)責(zé)管理實(shí)現(xiàn)的
(6) Snapshot unit: .snapshot,管理系統(tǒng)快照
(7) Swap unit: .swap,用于表示swap設(shè)備
(8) Autommount unit: .autommount,文件系統(tǒng)自動(dòng)掛載點(diǎn)設(shè)備
(9) Path unit: .path,用于定于文件系統(tǒng)中的一文件或目錄使用,常用于當(dāng)文件系統(tǒng)發(fā)生變化時(shí),延遲激活服務(wù)痘儡,如: spool 目錄
關(guān)鍵特性:
基于socket的激活機(jī)制,socket與程序分離;
基于D-bus(總線)的激活機(jī)制;如果總線上有對(duì)某一個(gè)服務(wù)的訪問(wèn),那么就基于總線的請(qǐng)求,將某一設(shè)備激活
基于device的激活機(jī)制;當(dāng)某個(gè)設(shè)備插入,能自動(dòng)激活mount unit和deviceunit或autommount unit,能監(jiān)控當(dāng)前系統(tǒng)和內(nèi)核所輸出的硬件信息,一旦發(fā)現(xiàn)某個(gè)硬件出現(xiàn),先創(chuàng)建設(shè)備文件,在自動(dòng)掛載至某掛載點(diǎn);掛載點(diǎn)不存在,還能自動(dòng)創(chuàng)建.
基于Path的激活機(jī)制;系統(tǒng)能夠監(jiān)控某個(gè)目錄或文件的存在,來(lái)激活一個(gè)服務(wù)或進(jìn)程;比如說(shuō),某個(gè)進(jìn)程崩潰時(shí)創(chuàng)建一個(gè)鎖文件,系統(tǒng)會(huì)根據(jù)判斷鎖文件的存在,去啟動(dòng)另一個(gè)進(jìn)程來(lái)判斷,崩潰進(jìn)程的原因;
系統(tǒng)快照:保存各unit的當(dāng)前狀態(tài)信息于持久存儲(chǔ)設(shè)備中;
向后兼容sysv lnit腳本:/etc/init.d/下的服務(wù)腳本
不兼容:
systemctl的命令是固定不變的;
非由systemd啟動(dòng)的服務(wù),systemctl無(wú)法與之通信,控制服務(wù);
2.4 systemctl系統(tǒng)服務(wù)管理命令
centos7:service類(lèi)型的unit文件;
所有命令要靠systemctl控制;
注意:能兼容早期的服務(wù)腳本
systemctl命令:
名稱:Control the systemd system and service manager
格式: systemctl [OPTIONS...] COMMAND NAME[.service]
命令示例
啟動(dòng)服務(wù):service NAME start --> systemctl start NAME.service
停止服務(wù):service NAME stop --> systemctl stop NAME.service
重啟服務(wù):service NAME restart --> systemctl restart NAME.service
查看狀態(tài):service NAME status --> systemctl status NAME.service
條件式重啟服務(wù):service NAME condrestart--> systemctl try-restart-NAME.service
重載或重啟服務(wù):systemctl reload-or-restart NAME.service
重載或條件式重啟:systemctl reload-or-try-restart NAME.service
查看某服務(wù)當(dāng)前激活與否的狀態(tài):systemctl is-active NAME.service
查看所有已激活的服務(wù):systemctl list-units -t service
查看所有服務(wù)(激活和未激活):chkconfig --list-->systemctl list-units -t service -a
查看所有服務(wù)的開(kāi)機(jī)自啟狀態(tài):
chkconfig --list ==> systemctl list-unit-files --type service
設(shè)置服務(wù)開(kāi)機(jī)自啟:chkconfig NAME on --> systemctl enable NAME.service
禁止服務(wù)開(kāi)機(jī)自啟:chkconfig NAME off --> systemctl disable NAME.service
查看某服務(wù)是否能開(kāi)機(jī)自啟:chkconfig --list NAME --> systemctl is-enabled NAME.service
禁止某服務(wù)設(shè)定為開(kāi)機(jī)自啟或手動(dòng)啟動(dòng):systemctl mask NAME.service
取消禁止某服務(wù)設(shè)定為開(kāi)機(jī)自啟或手動(dòng)啟動(dòng):systemctl unmask NAME.service
查看服務(wù)的依賴關(guān)系:systemctl list-dependencies NAME.service
用來(lái)列出該服務(wù)在哪些運(yùn)行級(jí)別下啟用和禁用:chkconfig sshd –list ==>ls /etc/systemd/system/*.wants/sshd.service
殺掉進(jìn)程:systemctl kill 進(jìn)程名
查看服務(wù)狀態(tài):
systemctl list-units --type service --all顯示狀態(tài)
loaded:Unit配置文件已處理
active(running):一次或多次持續(xù)處理的運(yùn)行
active(exited):成功完成一次性的配置
active(waiting):運(yùn)行中刊咳,等待一個(gè)事件
inactive:不運(yùn)行
enabled:開(kāi)機(jī)啟動(dòng)
disabled:開(kāi)機(jī)不啟動(dòng)
static:開(kāi)機(jī)不啟動(dòng)彪见,但可被另一個(gè)啟用的服務(wù)激活
systemctl命令示例:
顯示所有單元狀態(tài)
systemctl 或 systemctl list-units
只顯示服務(wù)單元的狀態(tài)
systemctl --type=service
顯示sshd服務(wù)單元
systemctl status sshd.service –l
驗(yàn)證sshd服務(wù)當(dāng)前是否活動(dòng)
systemctl is-active sshd
啟動(dòng),停止和重啟sshd服務(wù)
systemctl start sshd.service
systemctl stop sshd.service
systemctl restart sshd.service
重新加載配置
systemctl reload sshd.service
列出活動(dòng)狀態(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
驗(yàn)證sshd服務(wù)是否開(kāi)機(jī)啟動(dòng)
systemctl is-enabled sshd
禁用network余指,使之不能自動(dòng)啟動(dòng),但手動(dòng)可以
systemclt disable network
啟用network
systemctl enable network
禁用network,使之不能手動(dòng)或自動(dòng)啟動(dòng)
systemclt mask network
啟用network
systemctl umask network
管理target units:
unit配置文件: .target
ls /usr/lib/systemd/system/*.target
systemctl list-unit-files --type target --all
運(yùn)行級(jí)別:
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 NAME.target
級(jí)別切換:
centos6: init N
centos7: systemctl isolate NAME.target
注: 只有/lib/systemd/system/*.target文件中AllowIsolate=yes 才能切換(修改文件需執(zhí)行systemctl daemonreload才能生效)
查看級(jí)別:
centos6:runlevel 或者 who -r
centos7:systemctl list-units -t target
查看所有級(jí)別:systemctl list-units -t target -a
獲取默認(rèn)運(yùn)行級(jí)別:sytemctl get-default
修改默認(rèn)運(yùn)行級(jí)別:
systemctl set-default NAME.target
ls –l /etc/systemd/system/default.target
切換至緊急救援(級(jí)別1)模式:systemctl rescue
切換至emergency(緊急)模式:各種驅(qū)動(dòng)不會(huì)被加載,各種系統(tǒng)初始化功能不會(huì)被加載.是真正的實(shí)現(xiàn)系統(tǒng)救援的模式:因驅(qū)動(dòng)不兼容導(dǎo)致系統(tǒng)崩潰,適合切換此模式救援.
systemctl emergency
其他常用命令:
關(guān)機(jī):systemctl halt,systemctl poweroff
重啟:systemctl reboot
掛起:systemctl suspend
快照:systemctl hlbernate
快照并掛起:systemctl hybrid-sleep
修改運(yùn)行級(jí)別就是修改/etc/systemd/system/default.target的連接文件
3 centos 7 引導(dǎo)順序
UEFi或BIOS初始化跷坝,運(yùn)行POST開(kāi)機(jī)自檢
選擇啟動(dòng)設(shè)備
引導(dǎo)裝載程序, centos7是grub2
加載裝載程序的配置文件: /etc/grub.d/
/etc/default/grub /boot/grub2/grub.cfg
加載initramfs驅(qū)動(dòng)模塊
加載內(nèi)核選項(xiàng)
內(nèi)核初始化酵镜, centos7使用systemd代替init
執(zhí)行initrd.target所有單元,包括掛載/etc/fstab
從initramfs根文件系統(tǒng)切換到磁盤(pán)根目錄
systemd執(zhí)行默認(rèn)target配置柴钻,配置文件
/etc/systemd/default.target /etc/systemd/system/
systemd執(zhí)行sysinit.target初始化系統(tǒng)及basic.target準(zhǔn)備操
作系統(tǒng)
systemd啟動(dòng)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需要的服務(wù)
4 service target 文件介紹
/etc/systemd/system:系統(tǒng)管理員和用戶使用
/usr/lib/systemd/system:發(fā)行版打包者使用
以 “ #” 開(kāi)頭的行后面的內(nèi)容會(huì)被認(rèn)為是注釋
相關(guān)布爾值淮韭, 1、 yes贴届、 on靠粪、 true 都是開(kāi)啟, 0毫蚓、 no庇配、 off、false 都是關(guān)閉绍些。
時(shí)間單位默認(rèn)是秒,所以要用毫秒( ms)分鐘( m)等請(qǐng)顯式說(shuō)明
文件通常有三部分組成:
[Unit]:定義與Unit類(lèi)型無(wú)關(guān)的通用選項(xiàng);用于提供unit的描述信息,unit行為及依賴關(guān)系
Description:意義性描述信息 "systemctl status NAME"命令 可顯示
After:定義unit的啟動(dòng)次序;表示當(dāng)前unit應(yīng)該晚與那些unit啟動(dòng);
Requles:定義依賴到的其他unit;強(qiáng)依賴關(guān)系,只有依賴的unit啟動(dòng),此unit才能啟動(dòng);任何依賴unit啟動(dòng)不了,當(dāng)前unit就無(wú)法激活
Wants:定義依賴到的其他unit;弱依賴關(guān)系;被依賴的unit對(duì)本身的啟動(dòng)影響不大.
Conflicts:定義unit間的沖突關(guān)系
Documentation=man:httpd(8) 文檔的路徑
Documentation=man:apachectl(8)
[Service]:定義與特定類(lèi)型相關(guān)的專用選項(xiàng);此處為Service類(lèi)型
Type=notify 用來(lái)定義影響execstart及相關(guān)參數(shù)的功能的unit進(jìn)程啟動(dòng)類(lèi)型
類(lèi)型:
simple:默認(rèn)值,表示由execstart啟動(dòng)的進(jìn)程是主進(jìn)程
forking: 表示由execstart指明的程序所啟動(dòng)的進(jìn)程生成的一個(gè)子進(jìn)程為主進(jìn)程,啟動(dòng)完成后,父進(jìn)程會(huì)退出;
oneshot:類(lèi)似于simple,在啟動(dòng)后續(xù)的unit前,父進(jìn)程會(huì)退出;一次性的
dbus:類(lèi)似于simple,后續(xù)的unit僅在主進(jìn)程得到dbus之后,才能啟動(dòng);
notify:類(lèi)似于simple,表示后續(xù)的unit僅在通過(guò)sdbotify函數(shù)發(fā)送通知以后,然后才能運(yùn)行這個(gè)命令
ldle:與simple類(lèi)似耀鸦,要執(zhí)行這個(gè)daemon必須要所有的工作都順利執(zhí)行完畢后才會(huì)執(zhí)行柬批。這類(lèi)的daemon通常是開(kāi)機(jī)到最后才執(zhí)行即可的服務(wù)
EnvironmentFile=/etc/sysconfig/httpd 指定啟動(dòng)unit時(shí)要用到的環(huán)境配置文件,從這個(gè)文件中調(diào)用變量
ExecStart=指明啟動(dòng)unit要t運(yùn)行的命令或腳本
ExecReload:指明重新加載unit要運(yùn)行的命令或腳本
ExecStop: 指明停止unit要運(yùn)行的命令或腳本
Restart:當(dāng)設(shè)定Restart=1 時(shí),則當(dāng)次daemon服務(wù)意外終止后袖订,會(huì)再次自動(dòng)啟動(dòng)此服務(wù)
[Install]:定義由"systemctl enable"以及"systemctl disable"命令在實(shí)現(xiàn)服務(wù)啟動(dòng)或禁用時(shí)用到的一些選項(xiàng)
Alias:當(dāng)前unit的別名
RequiredBy:被那些units所依賴;依賴于當(dāng)前unit的unit列表;強(qiáng)依賴
WantedBy:被那些units所依賴;弱依賴
Also:安裝本服務(wù)的時(shí)候還要安裝別的相關(guān)服務(wù)
注意:對(duì)于新創(chuàng)建的unit文件或修改了的init文件,要通知systemd重載次配置文件.
#systemctl daemon-reload
#systemctl restart NAME.service
例如:
為httpd服務(wù)提供一個(gè)unit文件;
#vim /etc/systemd/system/bak.service
[Unit]
Description=backup my 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
5 啟動(dòng)排錯(cuò)
文件系統(tǒng)損壞
先嘗試自動(dòng)修復(fù)氮帐,失敗則進(jìn)入emergency shell,提示用戶修復(fù)
在/etc/fstab不存在對(duì)應(yīng)的設(shè)備和UUID等一段時(shí)間洛姑,如不可用上沐,進(jìn)入emergency shell
在/etc/fstab不存在對(duì)應(yīng)掛載點(diǎn)
systemd 嘗試創(chuàng)建掛載點(diǎn),否則提示進(jìn)入emergency shell.
在/etc/fstab不正確的掛載選項(xiàng)
提示進(jìn)入emergency shell