浅谈systemd
淺談systemd
systemd的基本概念
Systemd的管理服務
Systemd的管理運行級別
systemd的基本概念
一、system的簡要介紹
定義
??系統啟動和服務器守護進程管理器,負責在系統啟動或運行時,激活系統資源,服務器進程和其它進程
新特性
系統引導時實現服務并行啟動
按需啟動守護進程
自動化的服務依賴關系管理
同時采用socket式與D-Bus總線式激活服務
系統狀態快照
systemd的核心概念:unit
unit表示不同類型的systemd對象,通過配置文件進行標識和配置;文件中主要包含了系統服務、監聽socket、保存的系統快照以及其它與init相關的信息
systmed的配置文件
/usr/lib/systemd/system:每個服務最主要的啟動腳本設置,類似于之前的/etc/init.d/
/run/systemd/system:系統執行過程中所產生的服務腳本,比上面目錄優先運行
/etc/systemd/system:管理員建立的執行腳本,類似于/etc/rcN.d/Sxx的功能,比上面目錄優先運行
二、Systemd的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 目錄
三、Systemd的新特性
關鍵特性
基于socket的激活機制:socket與服務程序分離
基于d-bus的激活機制:
基于device的激活機制:
基于path的激活機制:
系統快照:保存各unit的當前狀態信息于持久存儲設備中向后兼容sysv init腳本
不兼容
systemctl命令固定不變,不可擴展
非由systemd啟動的服務,systemctl無法與之通信和控制
Systemd的管理服務
??語法: systemctl COMMAND name.service
一、管理服務
啟動:systemctl start name.service,相當于Centos6的service name.service start
停止:sysytemctl stop name.service,相當于Centos6的service name.service stop
重啟:systemctl restart name.service,相當于Centos6的service name.service restart
狀態:systemctl status name.service,相當于Centos6的service name.service status
條件式重啟:已啟動才重啟,否則不做操作
sysytmectl try-restart name.service,相當于Centos6的service name condrestart
重載或重啟服務:先加載,再啟動
systmenctl reload-or-restart name.service
重載或條件式重啟服務:
sysytemctl reload-or-try-restart name.service
禁止自啟動和手動啟動
systemctl mask name.service
取消禁止自啟動和手動啟動
systmectl unmask name.service
[root@Centos8 ~]#systemctl mask httpd
Created symlink /etc/systemd/system/httpd.service → /dev/null.
[root@Centos8 ~]#systemctl unmask httpd
Removed /etc/systemd/system/httpd.service.
#結論:mask和umask命令實際為建立/刪除指向/dev/null的軟鏈接
實驗:修改unit
修改 /usr/lib/systemd/system/httpd.service文件,將Description 改為The Apache HTTP,此時查看systemctl status httpd狀態,發現服務正常運行,但是描述沒有改變
? ? Warning: The unit file, source configuration file or drop-ins of httpd.service changed on disk. Run 'systemctl daemon-reload' to reload units.
? ? #已經給出提示了
? ? ● httpd.service - The Apache HTTP Server
? ? Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
? ? Active: active (running) since Sun 2019-11-03 17:14:36 CST; 11min ago
? ??
? ? #據狀態信息的提示,執行`sysytemctl daemon-reload`,再查看狀態發現名稱改變了
? ? [root@Centos8 /usr/lib/systemd/system]#systemctl daemon-reload
? ? [root@Centos8 /usr/lib/systemd/system]#systemctl status httpd
? ? ● httpd.service - The Apache HTTP
當服務正在運行的時候,對服務文件的unit進行改動,必須要重載才能更新
二、查看服務
查看某服務當前激活與否的狀態:
sytemctl is-active name.service
查看所有已經激活的服務:
systemctl list-units -type service
systemctl list-units -t service
查看所有服務:
systemctl list-units -t service -a or -all
三、設置服務開機狀態
設定某服務開機自啟動
systemctl enable name.service,相當于Centos6的chkconfig name on
設定某服務開機禁止自啟動
systemctl disable name.service,相當于Centos6的chkconfig name off
查看所有服務的開機自啟狀態
systemctl list-unit-files,相當于Centos6的 chkconfig --list
用來列出該服務在哪些運行級別下啟用和禁用:
ls /etc/systemd/system/*wants/sshd.service,相當于Centos6的 chkconfig sshd --list
實驗:設置服務自啟和禁止自啟
[root@Centos8 ~]#systemctl list-unit-files | grep httpd
httpd.service ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? disabled
#查看httpd服務原來狀態是禁止自啟
[root@Centos8 ~]#systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
#讓httpd服務開機自啟后,提示創建了軟鏈接
[root@Centos8 ~]rm -f /etc/systemd/system/multi-user.target.wants/httpd.service
#刪除這個軟鏈接文件
[root@Centos8 ~]#systemctl list-unit-files | grep httpd
httpd.service ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?disabled?
#查看httpd狀態,發現仍然是禁止自啟動
[root@Centos8 ~]#ln -s /usr/lib/systemd/system/httpd.service /etc/systemd/system/multi-user.target.wants/httpd.service
#創建原來的軟鏈接文件
[root@Centos8 ~]#systemctl list-unit-files | grep httpd
httpd.service ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?enabled
#此時查看httpd狀態,已經是開機自啟動
[root@Centos8 ~]#systemctl disable httpd
Removed /etc/systemd/system/multi-user.target.wants/httpd.service
#設置httpd開機禁止自啟,提示刪除了軟鏈接文件
[root@Centos8 ~]#systemctl is-enabled httpd
disabled
#查看狀態,已經變為禁止開機自啟動
總結:將某個服務設為自啟或者禁止自啟,實際上就是在相應的文件夾建立軟鏈接文件的過程,systemd通過查詢軟鏈接文件的存在與否判斷啟用/禁止自啟的狀態。
四、其他命令
查看服務是否開機自啟:
systemctl is-enabled name.service
systemctl list-unit-files | grep name.service
查看服務的依賴關系:
`systemctl list-dependencies httpd.service
[root@Centos8 ~]#systemctl list-dependencies httpd.service
httpd.service
● ├─-.mount
● ├─httpd-init.service
● ├─system.slice
● └─sysinit.target
● ? ├─dev-hugepages.mount
● ? ├─dev-mqueue.mount
● ? ├─dracut-shutdown.service
● ? ├─import-state.service
● ? ├─iscsi.service
● ? ├─kmod-static-nodes.service
● ? ├─ldconfig.service
● ? ├─loadmodules.service
● ? ├─lvm2-lvmpolld.socket
● ? ├─lvm2-monitor.service
● ? ├─multipathd.service
● ? ├─nis-domainname.service
● ? ├─plymouth-read-write.service
● ? ├─plymouth-start.service
● ? ├─proc-sys-fs-binfmt_misc.
.
殺掉進程:
systemctl kii unitname
五、服務狀態
顯示狀態:systemctl list-unit-files -t service -a
loaded:unit配置文件已處理
active(running):一次或多次持續處理的運行
active(exited):成功完成一次性的配置
active(waiting):運行中,等待一個事件
inactive:不運行
enabled:開機啟動
disabled:開機不啟動
static:開機不啟動,但可被另一個啟用的服務激活
Systemd的管理運行級別
一、target的概念
target:systemd的運行級別管理unit
路徑:ls /etc/lib/systemd/system/*.target
顯示狀態:systemctl list-unit-files
systemd的運行級別與傳統運行級別的對應關系
runlevel0.target, poweroff.target ==> 0
runlevel1.target, rescue.target ==> 1
runlevel2.target, multi-user.target ==> 2
runlevel3.target, multi-user.target ==> 3
runlevel4.target, multi-user.target ==> 4
runlevel5.target, graphical.target ==> 5
runlevel6.target, reboot.target ==> 6
查看target的依賴關系:systemctl list-dependencies name.target
二、級別切換
級別切換:systemctl isolate name.target
注:只有/lib/systemd/system/*.target文件中AllowIsolate=yes 才能切換(修改文件需執行systemctl daemon-reload才能生效)
顯示狀態:
systemctl list-unit-files -t target -a
systemctl list-unit-files | grep target
查看target:who -r runlevel
獲取默認運行級別:systemctl get-default
修改默認運行級別:systemctl set-default name.target
三、Systemd其他命令
切換緊急救援模式: systemctl rescue
切換至emergency模式:systemctl emergency
其他常用命令
傳統命令init,poweroff,halt,reboot都成為systemctl的軟鏈接
關機:systemctl halt、systemctl poweroff
重啟:systemctl reboot
掛起:systemctl suspend
休眠:systemctl hibernate
休眠并掛起:systemctl hybrid-sleep
[root@Centos8 ~]#ls /sbin/poweroff -l
lrwxrwxrwx. 1 root root 16 May 23 22:48 /sbin/poweroff -> ../bin/systemctl
[root@Centos8 ~]#ls /sbin/reboot -l
lrwxrwxrwx. 1 root root 16 May 23 22:48 /sbin/reboot -> ../bin/systemctl
[root@Centos8 ~]#ls /sbin/shutdown -l
lrwxrwxrwx. 1 root root 16 May 23 22:48 /sbin/shutdown -> ../bin/systemctl
?
總結
- 上一篇: 复古文字效果
- 下一篇: 私有网段IP地址的划分