管理systemd(CentOS7)

啟動(dòng)流程:POST --> Boot Sequence --> Bootloader --> kernel +initramfs(initrd) --> rootfs --> /sbin/init
init: CentOS 5: SysV init
CentOS 6: Upstart
CentOS 7: Systemd

1.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)程

(1)Systemd新特性:

系統(tǒng)引導(dǎo)時(shí)實(shí)現(xiàn)服務(wù)并行啟動(dòng)
按需啟動(dòng)守護(hù)進(jìn)程
自動(dòng)化的服務(wù)依賴(lài)關(guān)系管理
同時(shí)采用socket式與D-Bus總線式激活服務(wù)
系統(tǒng)狀態(tài)快照

(2)核心概念:unit

unit表示不同類(lèi)型的systemd對(duì)象,通過(guò)配置文件進(jìn)行標(biāo)識(shí)和配置;文件中主要包含了系統(tǒng)服務(wù)、監(jiān)聽(tīng)socket、保存的系統(tǒng)快照以及其它與init相關(guān)的信息

(3)配置文件:

/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)行

3.Unit類(lèi)型

(1)Systemctl –t help 查看unit類(lèi)型

? Service unit: 文件擴(kuò)展名為.service, 用于定義系統(tǒng)服務(wù)
? Target unit: 文件擴(kuò)展名為.target,用于模擬實(shí)現(xiàn)運(yùn)行級(jí)別
? Device unit: .device, 用于定義內(nèi)核識(shí)別的設(shè)備
? Mount unit: .mount, 定義文件系統(tǒng)掛載點(diǎn)
? Socket unit: .socket, 用于標(biāo)識(shí)進(jìn)程間通信用的socket文件,也可在系統(tǒng)啟動(dòng)時(shí),延遲啟動(dòng)服務(wù),實(shí)現(xiàn)按需啟動(dòng)
? Snapshot unit: .snapshot, 管理系統(tǒng)快照
? Swap unit: .swap, 用于標(biāo)識(shí)swap設(shè)備
? Automount unit: .automount,文件系統(tǒng)的自動(dòng)掛載點(diǎn)
? Path unit: .path,用于定義文件系統(tǒng)中的一個(gè)文件或目錄使用,常用于當(dāng)文件系統(tǒng)變化時(shí),延遲激活服務(wù),如:spool 目錄

(2)特性

關(guān)鍵特性:
基于socket的激活機(jī)制:socket與服務(wù)程序分離
基于d-bus的激活機(jī)制:
基于device的激活機(jī)制:
基于path的激活機(jī)制:
系統(tǒng)快照:保存各unit的當(dāng)前狀態(tài)信息于持久存儲(chǔ)設(shè)備中
向后兼容sysv init腳本

? 不兼容:

systemctl命令固定不變,不可擴(kuò)展
非由systemd啟動(dòng)的服務(wù),systemctl無(wú)法與之通信和控制

4.管理服務(wù)

? 管理系統(tǒng)服務(wù):
CentOS 7: service unit
注意:能兼容早期的服務(wù)腳本

? 命令:systemctl COMMAND name.service
? 啟動(dòng):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

? 條件式重啟:已啟動(dòng)才重啟,否則不做操作
service name condrestart ==> systemctl tryrestart
name.service
? 重載或重啟服務(wù):先加載,再啟動(dòng)
systemctl reload-or-restart name.service
? 重載或條件式重啟服務(wù):
systemctl reload-or-try-restart name.service
? 禁止自動(dòng)和手動(dòng)啟動(dòng):
systemctl mask name.service
? 取消禁止:
systemctl unmask name.service

5.服務(wù)查看

(1)查看某服務(wù)當(dāng)前激活與否的狀態(tài):
systemctl is-active name.service
(2)查看所有已經(jīng)激活的服務(wù):
systemctl list-units --type|-t service
(3)查看所有服務(wù):
systemctl list-units --type service --all|-a

6.chkconfig命令的對(duì)應(yīng)關(guān)系:

(1)設(shè)定某服務(wù)開(kāi)機(jī)自啟:
chkconfig name on ==> systemctl enable name.service
(2)設(shè)定某服務(wù)開(kāi)機(jī)禁止啟動(dòng):
chkconfig name off ==> systemctl disable name.service
(3)查看所有服務(wù)的開(kāi)機(jī)自啟狀態(tài):
chkconfig --list ==>
systemctl list-unit-files --type service
(4)用來(lái)列出該服務(wù)在哪些運(yùn)行級(jí)別下啟用和禁用
chkconfig sshd –list ==>
ls /etc/systemd/system/*.wants/sshd.service
(5)查看服務(wù)是否開(kāi)機(jī)自啟:
systemctl is-enabled name.service
(6)查看服務(wù)的依賴(lài)關(guān)系:
systemctl list-dependencies name.service
(7)殺掉進(jìn)程:
systemctl kill unitname

7.服務(wù)狀態(tài)

(1)systemctl list-unit-files --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ù)激活

(2)顯示所有單元狀態(tài)
systemctl 或 systemctl list-units
(3)只顯示服務(wù)單元的狀態(tài)
systemctl --type=service(--type后面跟服務(wù)類(lèi)型)
(4)顯示sshd服務(wù)單元
systemctl –l status sshd.service (-l顯示更詳細(xì)的信息)
(5)驗(yàn)證sshd服務(wù)當(dāng)前是否活動(dòng)
systemctl is-active sshd
(6)啟動(dòng),停止和重啟sshd服務(wù)
systemctl start sshd.service
systemctl stop sshd.service
systemctl restart sshd.service
systemctl 命令示例
(7)重新加載配置
systemctl reload sshd.service
(8)列出活動(dòng)狀態(tài)的所有服務(wù)單元
systemctl list-units --type=service
(9)列出所有服務(wù)單元
systemctl list-units --type=service --all
(10)查看服務(wù)單元的啟用和禁用狀態(tài)
systemctl list-unit-files --type=service
(11)列出失敗的服務(wù)
systemctl --failed --type=service

Paste_Image.png
eg:
? 列出依賴(lài)的單元
systemctl list-dependencies sshd
? 驗(yàn)證sshd服務(wù)是否開(kāi)機(jī)啟動(dòng)
systemctl is-enabled sshd
? 禁用network,使之不能自動(dòng)啟動(dòng),但手動(dòng)可以
systemctl disable network
? 啟用network
systemctl enable network
? 禁用network,使之不能手動(dòng)或自動(dòng)啟動(dòng)
systemctl mask network
? 啟用network
systemctl unmask network

8.運(yùn)行級(jí)別

(1)target units:

unit配置文件:.target
ls /usr/lib/systemd/system/*.target
systemctl list-unit-files --type target --all

(2)運(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

(3)查看依賴(lài)性:

systemctl list-dependencies graphical.target

(4) 級(jí)別切換:

init N ==> systemctl isolate name.target
systemctl isolate multi-user.target
注:只有/lib/systemd/system/*.target文件中
AllowIsolate=yes 才能切換(修改文件需執(zhí)行systemctl daemonreload才能生效)

(5)查看target:

runlevel ; who -r
systemctl list-units --type target

(6)獲取默認(rèn)運(yùn)行級(jí)別:

/etc/inittab ==> systemctl get-default

(7)修改默認(rèn)級(jí)別:

/etc/inittab ==>
systemctl set-default name.target
systemctl set-default multi-user.target
ls –l /etc/systemd/system/default.target

(8)切換至緊急救援模式:

systemctl rescue

(9) 切換至emergency模式:

systemctl emergency

(10)其它常用命令:

傳統(tǒng)命令init,poweroff,halt,reboot都成為systemctl軟鏈接
關(guān)機(jī):systemctl halt、systemctl poweroff
重啟:systemctl reboot
掛起:systemctl suspend
休眠:systemctl hibernate
休眠并掛起:systemctl hybrid-sleep

9.CentOS7引導(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/system/default.target
? 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ù)

10.service unit文件格式

(1)配置文件

/etc/systemd/system:系統(tǒng)管理員和用戶(hù)使用
/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)等須顯式說(shuō)明

(2)service unit file文件通常由三部分組成:

? [Unit]:定義與Unit類(lèi)型無(wú)關(guān)的通用選項(xiàng);用于提供unit描述信息、unit行為及依賴(lài)關(guān)系等
? [Service]:與特定類(lèi)型相關(guān)的專(zhuān)用選項(xiàng);此處為Service類(lèi)型
? [Install]:定義由“systemctl enable”以及"systemctl
disable“命令在實(shí)現(xiàn)服務(wù)啟用或禁用時(shí)用到的一些選項(xiàng)

Paste_Image.png

Unit段的常用選項(xiàng):

? Description:描述信息
? After:定義unit的啟動(dòng)次序,表示當(dāng)前unit應(yīng)該晚于哪些unit啟動(dòng),其功能與Before相反
? Requires:依賴(lài)到的其它units,強(qiáng)依賴(lài),被依賴(lài)的units無(wú)法激活時(shí),當(dāng)前unit也無(wú)法激活
? Wants:依賴(lài)到的其它units,弱依賴(lài)
? Conflicts:定義units間的沖突關(guān)系

Service段的常用選項(xiàng):

? Type:定義影響ExecStart及相關(guān)參數(shù)的功能的unit進(jìn)程啟動(dòng)類(lèi)型
? simple:默認(rèn)值,這個(gè)daemon主要由ExecStart接的指令串來(lái)啟動(dòng),啟動(dòng)后常駐于內(nèi)存中
? forking:由ExecStart啟動(dòng)的程序透過(guò)spawns延伸出其他子程序來(lái)作為此daemon的主要服務(wù)。原生父程序在啟動(dòng)結(jié)束后就會(huì)終止
? oneshot:與simple類(lèi)似,不過(guò)這個(gè)程序在工作完畢后就結(jié)束了,不會(huì)常駐在內(nèi)存中
? dbus:與simple類(lèi)似,但這個(gè)daemon必須要在取得一個(gè)D-Bus的名稱(chēng)后,才會(huì)繼續(xù)運(yùn)作.因此通常也要同時(shí)設(shè)定BusNname= 才行
? notify:在啟動(dòng)完成后會(huì)發(fā)送一個(gè)通知消息。還需要配合NotifyAccess 來(lái)讓 Systemd 接收消息
? idle:與simple類(lèi)似,要執(zhí)行這個(gè)daemon必須要所有工作都順利執(zhí)行完畢后才會(huì)執(zhí)行。這類(lèi)的daemon通常是開(kāi)機(jī)到最后才執(zhí)行即可的服務(wù)
? EnvironmentFile:環(huán)境配置文件
? ExecStart:指明啟動(dòng)unit要運(yùn)行命令或腳本的絕對(duì)路徑
? ExecStartPre: ExecStart前運(yùn)行
? ExecStartPost: ExecStart后運(yùn)行
? ExecStop:指明停止unit要運(yùn)行的命令或腳本
? Restart:當(dāng)設(shè)定Restart=1 時(shí),則當(dāng)次daemon服務(wù)意外終止后,會(huì)再次自動(dòng)啟動(dòng)此服務(wù)

Install段的常用選項(xiàng):

? Alias:別名,可使用systemctl command Alias.service
? RequiredBy:被哪些units所依賴(lài),強(qiáng)依賴(lài)
? WantedBy:被哪些units所依賴(lài),弱依賴(lài)
? Also:安裝本服務(wù)的時(shí)候還要安裝別的相關(guān)服務(wù)
? 注意:對(duì)于新創(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

11.設(shè)置內(nèi)核參數(shù)

設(shè)置內(nèi)核參數(shù),只影響當(dāng)次啟動(dòng)

? 啟動(dòng)時(shí),在linux16行后添加systemd.unit=desired.target
? systemd.unit=emergency.target
? systemd.unit=rescue.target相當(dāng)于init1
? rescue.target 比emergency 支持更多的功能,例如日志等
? systemctl default 進(jìn)入默認(rèn)target

12.啟動(dòng)排錯(cuò)

(1)文件系統(tǒng)損壞
先嘗試自動(dòng)修復(fù),失敗則進(jìn)入emergency shell,提示用戶(hù)修復(fù)
(2)在/etc/fstab不存在對(duì)應(yīng)的設(shè)備和UUID
等一段時(shí)間,如不可用,進(jìn)入emergency shell
(3)在/etc/fstab不存在對(duì)應(yīng)掛載點(diǎn)
systemd 嘗試創(chuàng)建掛載點(diǎn),否則提示進(jìn)入emergency shell.
(4)在/etc/fstab不正確的掛載選項(xiàng)
提示進(jìn)入emergency shell

13.破解CentOS7的root口令

方法一:

啟動(dòng)時(shí)任意鍵暫停啟動(dòng)
按e鍵進(jìn)入編輯模式
將光標(biāo)移動(dòng)linux16開(kāi)始的行,添加內(nèi)核參數(shù)rd.break
按ctrl-x啟動(dòng)
? mount –o remount,rw /sysroot(改文件權(quán)限)
? chroot /sysroot
? passwd root
? touch /.autorelabel(這是給文件加標(biāo)簽)
? exit
? reboot

方法二

? 啟動(dòng)時(shí)任意鍵暫停啟動(dòng)
? 按e鍵進(jìn)入編輯模式
? 將光標(biāo)移動(dòng)linux16開(kāi)始的行,改為rw init=/sysroot/bin/sh
? 按ctrl-x啟動(dòng)
? chroot /sysroot
? passwd root
? touch /.autorelabel
? exit
? reboot

14.修復(fù)GRUB2

GRUB“the Grand Unified Bootloader”
引導(dǎo)提示時(shí)可以使用命令行界面
可從文件系統(tǒng)引導(dǎo)
? 主要配置文件 /boot/grub2/grub.cfg
? 修復(fù)配置文件
grub2-mkconfig > /boot/grub2/grub.cfg

15.修復(fù)grub

grub2-install /dev/sda BIOS環(huán)境
grub2-install UEFI環(huán)境
? 調(diào)整默認(rèn)啟動(dòng)內(nèi)核
vim /etc/default/grub
GRUB_DEFAULT=0

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容