Systemd
概述:
CentOS 6和之前版本采用SysVinit的系統啟動進程管理體系,一般用戶都可通過在/etc/inittab文件的配置,來個性化自己的系統啟動序列。但也經常會由于特殊環境的硬件等關系問題,造成其串行的啟動進程控制流,因為可能任務的阻塞而影響啟動過程。
CentOS 7開始使用SystemD,所以我們必須要了解SystemD.本章將從CentOS 7 的啟動流程、Unit、服務管理,啟動排錯,破解口令以及修復grub2 等方面來介紹Systemd的相關內容。
1.Systemd介紹:
1)啟動流程
POST –> Boot Sequence –> Bootloader –> kernel + initramfs(initrd) –> rootfs(根切換)–> /sbin/init
init:
init:CentOS 5: SysVinit;
CentOS 6: Upstart;
CentOS 7: Systemd;
2)Systemd:
系統啟動和服務器守護進程管理器,負責在系統啟動或運行時,激活系統資源,服務器進程和其它進程;
3)Systemd新特性
系統引導時實現服務并行啟動;
按需啟動守護進程;
自動化的服務依賴關系管理;
同時采用socket式與D-Bus總線式激活服務;
系統狀態快照。
2.核心概念:Unit
unit表示不同類型的systemd對象,通過配置文件進行標識和配置;
文件中主要包含了系統服務、監聽socket、保存的系統快照以及其它與init相關的信息;
3.配置文件
/usr/lib/systemd/system:每個服務最主要的啟動腳本設置,類似于之前的/etc/init.d/
/run/systemd/system:系統執行過程中所產生的服務腳本,比上面目錄優先運行
/etc/systemd/system:管理員建立的執行腳本,類似于/etc/rc.d/rcN.d/Sxx類的功能,比上面目錄優先運行
[root@centos7 ~]# cd /usr/lib/systemd/system [root@centos7 system]# ls abrt-ccpp.service gdm.service ntpdate.service sys-fs-fuse-connections.mount abrtd.service geoclue.service oddjobd.service sysinit.target abrt-oops.service getty@.service paths.target sysinit.target.wants abrt-pstoreoops.service getty.target plymouth-halt.service sys-kernel-config.mount abrt-vmcore.service graphical.target plymouth-kexec.service sys-kernel-debug.mount abrt-xorg.service graphical.target.wants plymouth-poweroff.service syslog.socket accounts-daemon.service gssproxy.service plymouth-quit.service syslog.target.wants alsa-restore.service halt-local.service plymouth-quit-wait.service sysstat.service alsa-state.service halt.target plymouth-read-write.service systemd-ask-password-console.path alsa-store.service halt.target.wants plymouth-reboot.service systemd-ask-password-console.service anaconda-direct.service hibernate.target plymouth-start.service systemd-ask-password-plymouth.path anaconda-nm-config.service htcacheclean.service plymouth-switch-root.service systemd-ask-password-plymouth.service anaconda-noshell.service httpd.service polkit.service systemd-ask-password-wall.path anaconda.service hybrid-sleep.target postfix.service systemd-ask-password-wall.service anaconda-shell@.service initial-setup-graphical.service poweroff.target systemd-backlight@.service anaconda-sshd.service initial-setup-text.service poweroff.target.wants s
4.Unit 類型
Systemctl –t help 查看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,用于定義文件系統中的一個文件或目錄使用,常用于當文件系統變化時,延遲激活服務,如:spool 目錄
文件如下:
[root@centos7 ~]# systemctl -t help Available unit types: service socket busname target snapshot device mount automount swap timer path slice scope
5.特性
1)關鍵特性:
基于socket的激活機制:socket與服務程序分離
基于d-bus的激活機制:
基于device的激活機制:
基于path的激活機制:
系統快照:保存各unit的當前狀態信息于持久存儲設備中
向后兼容sysvinit腳本
2)不兼容
systemctl命令固定不變,不可擴展
非由systemd啟動的服務,systemctl無法與之通信和控制
6.管理服務
1)管理系統服務
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
示例:
[root@CentOS6 ~]# service httpd status # CentOS 6 顯示的狀態信息 httpd is stopped [root@centos7 ~]# systemctl status httpd.service # CentOS 7 顯示的狀態信息 ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: inactive (dead) Docs: man:httpd(8) man:apachectl(8)
2)服務查看
查看某服務當前激活與否的狀態
systemctl is-active name.service
查看所有已經激活的服務:
systemctl list-units –type|-t service
查看所有服務(已激活及未激活):
systemctl list-units –type|-t service –all|-a
示例:
[root@centos7 ~]# systemctl is-active httpd.service # 查看某服務當前激活與否的狀態 active [root@centos7 ~]# systemctl stop httpd.service [root@centos7 ~]# systemctl is-active httpd.service unknown [root@centos7 ~]# systemctl list-units -t service # 查看所有已激活的服務 UNIT LOAD ACTIVE SUB DESCRIPTION abrt-ccpp.service loaded active exited Install ABRT coredump hook abrt-oops.service loaded active running ABRT kernel log watcher abrt-xorg.service loaded active running ABRT Xorg log watcher abrtd.service loaded active running ABRT Automated Bug Reporting Tool alsa-state.service loaded active running Manage Sound Card State (restore and store) atd.service loaded active running Job spooling tools auditd.service loaded active running Security Auditing Service autofs.service loaded active running Automounts filesystems on demand blk-availability.service loaded active exited Availability of block devices chronyd.service loaded active running NTP client/server crond.service loaded active running Command Scheduler cups.service loaded active running CUPS Printing Service dbus.service loaded active running D-Bus System Message Bus getty@tty1.service loaded active running Getty on tty1 [root@centos7 ~]# systemctl list-units -t service --all # 查看所有服務狀態 UNIT LOAD ACTIVE SUB DESCRIPTION abrt-ccpp.service loaded active exited Install ABRT coredump hook abrt-oops.service loaded active running ABRT kernel log watcher abrt-vmcore.service loaded inactive dead Harvest vmcores for ABRT abrt-xorg.service loaded active running ABRT Xorg log watcher abrtd.service loaded active running ABRT Automated Bug Reporting Tool accounts-daemon.service loaded inactive dead Accounts Service alsa-restore.service loaded inactive dead Restore Sound Card State
3)服務狀態:
systemctl list-units –type service –all顯示狀態
loaded:Unit配置文件已處理
active(running):一次或多次持續處理的運行
active(exited):成功完成一次性的配置
active(waiting):運行中,等待一個事件
inactive:不運行
enabled:開機啟動
disabled:開機不啟動
static:開機不啟動,但可被另一個啟用的服務激活
命令的對應關系
設定某服務開機自啟:
chkconfig name on ==> systemctl enable name.service
設定某服務開機禁止啟動:
chkconfig name off ==> systemctl disable name.service
查看所有服務的開機自啟狀態:
chkconfig –list ==> systemctl list-unit-files –type service
用來列出該服務在哪些運行級別下啟用和禁用
chkconfig sshd –list==> ls /etc/systemd/system/*.wants/sshd.service
查看服務能否開機自啟:
chkconfig –list name ==> systemctl is-enabled name.service
其他命令
查看服務的依賴關系:
systemctll ist-dependencies name.service
殺掉進程:
systemctl kill 進程名
示例:
[root@centos7 ~]# systemctl is-enabled httpd disabled [root@centos7 ~]# systemctl is-enabled sshd # 查看某服務能否開機自啟 enabled
systemctl示例:
·顯示所有單元狀態
systemctl 或systemctl list-units
·只顯示服務單元的狀態
systemctl --type=service
·顯示sshd服務單元
systemctl status sshd.service–l
·驗證sshd服務當前是否活動
systemctlis-active sshd
·啟動,停止和重啟sshd服務
systemctl start sshd.service
systemctl stop sshd.service
systemctl restart sshd.service
·重新加載配置
systemctl reload sshd.service
·列出活動狀態的所有服務單元
systemctl list-units --type=service
·列出所有服務單元
systemctl list-units --type=service --all
·查看服務單元的啟用和禁用狀態。
systemctl list-unit-files --type=service
·列出失敗的服務
systemctl--failed --type=service
·列出依賴的單元
systemctl list-dependencies sshd
·驗證sshd服務是否開機啟動
systemctl is-enabled sshd
·禁用network,使之不能自動啟動,但手動可以
systemctl disable network
·啟用network
systemctl enable network
·禁用network,使之不能手動或自動啟動
systemctl mask network
·啟用network
systemctl umask network
運行級別(管理target unit)
★ 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 # 切換到級別3
注:只有/lib/systemd/system/*.target文件中AllowIsolate=yes 才能切換(修改 文件需執行systemctl daemon-reload才能生效)
★ 查看級別:
runlevelwho -r ==> systemctl list-units –type target
★ 獲取默認運行級別:
/etc/inittab ==> systemctl get-default
★ 修改默認級別:
/etc/inittab==> systemctl set-default name.target
systemctl set-default multi-user.target //修改為3級別
ls –l /etc/systemd/system/default.target
示例:
[root@centos7 ~]# ls /usr/lib/systemd/system/*.target # 顯示的所有級別 /usr/lib/systemd/system/anaconda.target /usr/lib/systemd/system/local-fs-pre.target /usr/lib/systemd/system/runlevel2.target /usr/lib/systemd/system/basic.target /usr/lib/systemd/system/local-fs.target /usr/lib/systemd/system/runlevel3.target /usr/lib/systemd/system/bluetooth.target /usr/lib/systemd/system/machines.target /usr/lib/systemd/system/runlevel4.target /usr/lib/systemd/system/cryptsetup-pre.target /usr/lib/systemd/system/multi-user.target /usr/lib/systemd/system/runlevel5.target /usr/lib/systemd/system/cryptsetup.target /usr/lib/systemd/system/network-online.target /usr/lib/systemd/system/runlevel6.target /usr/lib/systemd/system/ctrl-alt-del.target /usr/lib/systemd/system/network-pre.target /usr/lib/systemd/system/shutdown.target /usr/lib/systemd/system/default.target /usr/lib/systemd/system/network.target /usr/lib/systemd/system/sigpwr.target /usr/lib/systemd/system/emergency.target /usr/lib/systemd/system/nfs-client.target /usr/lib/systemd/system/sleep.target /usr/lib/systemd/system/final.target /usr/lib/systemd/system/nss-lookup.target /usr/lib/systemd/system/slices.target /usr/lib/systemd/system/getty.target /usr/lib/systemd/system/nss-user-lookup.target /usr/lib/systemd/system/smartcard.target /usr/lib/systemd/system/graphical.target /usr/lib/systemd/system/paths.target /usr/lib/systemd/system/sockets.target /usr/lib/systemd/system/halt.target /usr/lib/systemd/system/poweroff.target /usr/lib/systemd/system/sound.target /usr/lib/systemd/system/hibernate.target /usr/lib/systemd/system/printer.target /usr/lib/systemd/system/spice-vdagentd.target /usr/lib/systemd/system/hybrid-sleep.target /usr/lib/systemd/system/reboot.target /usr/lib/systemd/system/suspend.target /usr/lib/systemd/system/initrd-fs.target /usr/lib/systemd/system/remote-fs-pre.target /usr/lib/systemd/system/swap.target /usr/lib/systemd/system/initrd-root-fs.target /usr/lib/systemd/system/remote-fs.target /usr/lib/systemd/system/sysinit.target /usr/lib/systemd/system/initrd-switch-root.target /usr/lib/systemd/system/rescue.target /usr/lib/systemd/system/system-update.target /usr/lib/systemd/system/initrd.target /usr/lib/systemd/system/rpcbind.target /usr/lib/systemd/system/timers.target /usr/lib/systemd/system/iprutils.target /usr/lib/systemd/system/runlevel0.target /usr/lib/systemd/system/time-sync.target /usr/lib/systemd/system/kexec.target /usr/lib/systemd/system/runlevel1.target /usr/lib/systemd/system/umount.target
[root@centos7 ~]# who -r run-level 3 2016-09-23 07:10 [root@centos7 ~]# runlevel # 具有舊版本兼容性,也可以使用centos6的命令來查看 N 3 [root@centos7 ~]# systemctl list-units --type target # 查看運行級別 UNIT LOAD ACTIVE SUB DESCRIPTION basic.target loaded active active Basic System cryptsetup.target loaded active active Encrypted Volumes getty.target loaded active active Login Prompts local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems multi-user.target loaded active active Multi-User System # 當前級別為3 network-online.target loaded active active Network is Online network.target loaded active active Network nfs-client.target loaded active active NFS client services paths.target loaded active active Paths remote-fs-pre.target loaded active active Remote File Systems (Pre) remote-fs.target loaded active active Remote File Systems slices.target loaded active active Slices sockets.target loaded active active Sockets sound.target loaded active active Sound Card swap.target loaded active active Swap sysinit.target loaded active active System Initialization timers.target loaded active active Timers LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type. 18 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'.
[root@centos7 ~]# systemctl get-default # 獲取默認運行級別 multi-user.target [root@centos7 ~]# systemctl isolate graphical.target # 切換到5級別 PolicyKit daemon disconnected from the bus. We are no longer a registered authentication agent. PolicyKit daemon reconnected to bus. Attempting to re-register as an authentication agent. We are now a registered authentication agent.
其他命令
★ 切換至緊急救援模式:
·systemctl rescue
★ 切換至emergency(緊急)模式:
·systemctlemergency
★ 其它常用命令:
傳統命令init,poweroff,halt,reboot都成為systemctl的軟鏈接
·關機:systemctl halt 或者 systemctlpoweroff
·重啟:systemctl reboot
·掛起:systemctl suspend
·快照(休眠):systemctl hibernate
·快照并掛起:systemctl hybrid-sleep
service unit 文件格式
★ /etc/systemd/system:系統管理員和用戶使用
/usr/lib/systemd/system:發行版打包者使用
★ 以“#” 開頭的行后面的內容會被認為是注釋;
★ 相關布爾值,1、yes、on、true 都是開啟,0、no、off、false 都是關閉;
★ 時間單位默認是秒,所以要用毫秒(ms)分鐘(m)等請顯式說明;
★ service unit file文件通常由三部分組成:
·[Unit]:
定義與Unit類型無關的通用選項;用于提供unit的描述信息、unit行為及依賴 關系等;
·[Service]:
與特定類型相關的專用選項;此處為Service類型;
·[Install]:
定義由“systemctl enable”以及"systemctl disable“命令在實現服務啟用 或禁用時用到的一些選項。
文件格式如下:
[root@centos7 system]# pwd /usr/lib/systemd/system [root@centos7 system]# cat httpd.service [Unit] Description=The Apache HTTP Server # 描述信息 After=network.target remote-fs.target nss-lookup.target # 定義啟動順序 Documentation=man:httpd(8) Documentation=man:apachectl(8) [Service] Type=notify EnvironmentFile=/etc/sysconfig/httpd ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND ExecReload=/usr/sbin/httpd $OPTIONS -k graceful ExecStop=/bin/kill -WINCH ${MAINPID} # We want systemd to give httpd some time to finish gracefully, but still want # it to kill httpd after TimeoutStopSec if something went wrong during the # graceful stop. Normally, Systemd sends SIGTERM signal right after the # ExecStop, which would kill httpd. We are sending useless SIGCONT here to give # httpd time to finish. KillSignal=SIGCONT PrivateTmp=true [Install] WantedBy=multi-user.target
Unit段的常用選項意義:
Description:描述信息,意義性描述;
After:定義unit的啟動次序,表示當前unit應該晚于哪些unit啟動,其功能與Before相反
Requires:依賴到的其它units,強依賴,被依賴的units無法激活時,當前unit即無法激活
Wants:依賴到的其它units,弱依賴;
Conflicts:定義units間的沖突關系
Service段的常用選項意義:
★ Type:定義影響ExecStart及相關參數的功能的unit進程啟動類型
類型:
simple:默認值,這個daemon主要由ExecStart接的指令串來啟動,啟動后常駐于內存中
forking:由ExecStart啟動的程序透過spawns延伸出其他子程序來作為此daemon的主要服務。原生父程序在啟動結束后就會終止
oneshot:與simple類似,不過這個程序在工作完畢后就結束了,不會常駐在內存中
dbus:與simple類似,但這個daemon必須要在取得一個D-Bus的名稱后,才會繼續運作.因此通常也要同時設定BusNname= 才行
notify:在啟動完成后會發送一個通知消息。還需要配合NotifyAccess 來讓Systemd 接收消息
idle:與simple類似,要執行這個daemon必須要所有的工作都順利執行完畢后才會執行。這類的daemon通常是開機到最后才執行即可的服務
★ EnvironmentFile:環境配置文件;
★ ExecStart:指明啟動unit要運行命令或腳本的絕對路徑;
★ ExecStartPre:ExecStart前運行;
★ ExecStartPost:ExecStart后運行;
★ ExecStop:指明停止unit要運行的命令或腳本;
★ Restart:當設定Restart=1 時,則當次daemon服務意外終止后,會再次自動啟動此服務
Install段的常用選項意義:
Alias:別名,可使用systemctl command Alias.service
RequiredBy:被哪些units所依賴,強依賴
WantedBy:被哪些units所依賴,弱依賴
Also:安裝本服務的時候還要安裝別的相關服務
注意:
對于新創建的unit文件,或者修改了的unit文件,要通知systemd重載此配置文件,而后 可以選擇重啟
# systemctl daemon-reload
服務Unit 文件示例
(1)創建一個腳本,用于被創建的服務調用
[root@localhost system]# cat /testdir/bak.sh
#!/bin/bash
# 備份/etc/目錄
tar -Jcvf /testdir/etc-`date +%F`.tar.xz /etc/ &> dev/null
(2)給bak.sh腳本添加執行權限
[root@localhost ~]# chmod u+x /testdir/bak.sh
(3)創建bak.service服務
[root@localhost ~]# 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
(4)啟用服務
[root@localhost system]# systemctl daemon-reload
[root@localhost system]# systemctl start bak
(5)驗證
[root@localhost system]# ll /testdir/
total 8132
-rwxr--r-- 1 root root 91 Sep 21 19:14 bak.sh
-rw-r--r-- 1 root root 4546560 Sep 21 19:15 etc-2016-09-21.tar.xz
CentOS 7 引導順序
UEFi或BIOS初始化,運行POST開機自檢
選擇啟動設備
引導裝載程序, centos7是grub2
加載裝載程序的配置文件:/etc/grub.d/ /etc/default/grub /boot/grub2/grub.cfg
加載initramfs驅動模塊
加載內核選項
內核初始化,centos7使用systemd代替init
執行initrd.target所有單元,包括掛載/etc/fstab
從initramfs根文件系統切換到磁盤根目錄
systemd執行默認target配置,配置文件/etc/systemd/default.target /etc/systemd/system/
systemd執行sysinit.target初始化系統及basic.target準備操作系統
systemd啟動multi-user.target下的本機與服務器服務
systemd執行multi-user.target下的/etc/rc.d/rc.local
Systemd執行multi-user.target下的getty.target及登入服務
systemd執行graphical需要的服務
設置內核參數:
設置內核參數,只影響當次啟動
啟動時,在linux16行后添加systemd.unit=desired.target
systemd.unit=emergency.target
systemd.unit=recure.target
recure.target 比emergency 支持更多的功能,例如日志等
啟動排錯:
★ 文件系統損壞
先嘗試自動修復,失敗則進入emergency shell,提示用戶修復
★ 在/etc/fstab不存在對應的設備和UUID
等一段時間,如不可用,進入emergency shell
★ 在/etc/fstab不存在對應掛載點
systemd嘗試創建掛載點,否則提示進入emergency shell.
★ 在/etc/fstab不正確的掛載選項
提示進入emergency shell
破解root口令:
啟動時任意鍵暫停啟動
按e鍵進入編輯模式
將光標移動linux16開始的行,添加內核參數rd.break
按ctrl-x啟動
mount –o remount,rw /sysroot # 因為是只讀掛載,所以要重新掛載成寫的
chroot /sysroot # 切換成真正文件系統的根
passwd root
touch /.autorelabel # 要重新打標簽,觸發selinux策略
修復GRUB2
★ GRUB“the Grand Unified Bootloader”
引導提示時可以使用命令行界面
可從文件系統引導
★ 主要配置文件
/boot/grub2/grub.cfg
★ 修復配置文件
grub2-mkconfig > /boot/grub2/grub.cfg
★ 修復grub
grub2-install /dev/sda BIOS環境
grub2-install UEFI環境
原創文章,作者:zhumengxiaotao,如若轉載,請注明出處:http://www.www58058.com/48990