Centos7 Systemd概念
centos7 中init程序使用的是Systemd,它是系統啓動和服務器守護進程管理器,負責系統啓動或運行時,激活系統資源,服務器進程和其他進程。
systemd關鍵特性
基於socket的激活機制:socket與服務程序分離
基於bus的激活機制
基於device的激活機制
基於path的激活機制
系統快照:保存各unit的當前狀態信息於持久存儲設備中
向後兼容sysv init腳本
Systemd核心單元
核心概念:unit unit表示不同類型的systemd對象,通過配置文件進行標識和配置;文件中 主要包含了系統服務、監聽socket、保存的系統快照以及其它與init相關的信息
配置文件:
/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 用於定義文件系統中的一個文件或者目錄
system解決服務依賴性
例如,rpcbind服務和nfs服務,在centos6上需要先啓動rpcbind才能使用nfs
在centos7上直接啓動nfs時,systemd會自動啓動依賴服務rpcbind
管理系統服務:
centos 7 : service unit
注意能兼容早期的服務腳本
命令:system COMMAND name.service
啓動:service name start ===> systemctl start name.service
停止:service name stop ===> systemctl stop name.servcie
重啓:service name restart ===> systemctl restart name.service
狀態:service name status ===> systemctl status name.service
重載或重啓服務:systemctl reload-or-restart name.service
重載或條件重啓:systemctl reload-or-try-restart name.service
條件式重啓:service name condrestart ===> systemctl try-restart name.service
查看某服務當前激活與否的狀態: systemctl is-active name.service
查看所有已激活服務:systemctl list-units --type service
查看所有服務:systemctl list-units --type service --all
chkconfig命令的對應關係:
設定某服務開機自啓:chkconfig name on ===> systemctl enable name.service
禁止:chkconfig name off ===> systemctl disable name.service
查看所有服務的開機自啓狀態:
chkconfig --list ===> systemctl list-unit-files --type service
其他命令:
查看服務依賴關係:systemctl list-dependencies name.service
禁止服務開機自啓(假設已經enable):systemctl mask name.service
去標記:systemctl umask name.service
target units:
unit配置文件:.target
運行級別:
0 ==> runlevel0.target,powerof.target
1 ==> runlevel1.target,rescue.target
2 ==> runlevel2.target,multiuser.target
3 ==> runlevel3.target,multiuser.target
4 ==> runlevel4.target,multiuser.target
5 ==> runlevel5.target,graphical.target
6 ==> runlevel6.target,reboot.target
切換級別:
init N ==> systemctl isolate name.target 前提是name.target已經激活
獲取默認運行級別:
/etc/inittab ==> systemctl get-default
修改默認級別:
/etc/inittab ==> systemctl set-default name.target
切換至緊急模式
systemctl rescue
切換值emergency模式
systemctl emergency
其他常用命令:
關機:systemctl halt,systemctl poweroff
重啓:systemctl reboot
掛起:systemctl suspend
快照:systemctl hiberate
快照並掛起:systemctl hybrid-sleep
systemd代替xinetd守護進程
centos7安裝telnet時不安裝xinetd
systemctl start telnet.socket
ss -ntl
ss -ntlpe 可以看到是systemd在監聽23端口,實際上telnet服務還沒啓動