systemd 是 Linux 下的一款系統和服務管理器,兼容 SysV ,Upstart和 LSB 的啓動腳本。在CentOS 7中成爲默認的服務管理器。html
#CentOS 5: SysV init
#CentOS 6: Upstart
#CentOS 7: Systemdsocket
systemd 的特性有:
spa
systemd核心概念:unit
hibernate
systemd引入了一個新的概念,叫作unit,全部可用的單元文件存放在 /usr/lib/systemd/system/
和 /etc/systemd/system/
目錄。rest
unit類型:code
系統服務(.service
):.service, 用於定義系統服務;htm
掛載點(.mount
):.mount,定義文件系統掛載點;blog
sockets(.sockets
) :.socket, 用於標識進程間通訊用的socket文件;進程
系統設備(.device
):.device, 用於定義內核識別的設備;ci
交換分區(.swap
): .swap, 用於標識swap設備;
文件路徑(.path
):.path,用於定義文件系統中的一個文件或目錄;
啓動目標(.target
):.target,用於模擬實現「運行級別」;
系統快照(.swap
):.swap, 用於標識swap設備;
自動掛載點(.automount
):.automount,文件系統的自動掛載點;
systemd的主要命令是systemctl
。該命令可用於查看系統狀態和管理系統及服務。
命令格式:
systemctl [OPTIONS...] COMMAND [NAME...]
顯示系統狀態:
systemctl status
查看激活的單元:
systemctl | systemctl list-units
查看失敗的單元
systemctl --failed
服務管理:
啓動服務: systemctl start name.service #.service能夠不加 關閉服務: systemctl stop name.service 查看服務狀態: systemctl status name.service 重啓服務: systemctl restart name.service 條件式重啓: systemctl try-restart name.service 重載或重啓服務: systemctl reload-or-restart name.service 重載或條件式重啓服務: systemctl reload-or-try-restart name.service 查看全部已激活的服務: systemctl list-units --type service 查看全部服務: systemctl list-units --type service --all 查看某服務當前激活與否狀態: systemctl is-active name.service 設置服務開機啓動: systemctl enable name.service 禁止服務開機啓動: systemctl disable name.service 查看服務是否開機啓動: systemctl is-enabled name.service 查看全部服務是否開機啓動: systemctl list-unit-files --type service 禁止設爲開機啓動: systemctl mask name.service 取消禁止設爲開啓啓動: systemctl unmask name.service 查看服務啓動依賴關係: systemctl list-dependencies name.service
在RHEL/CentOS 5/6 中,更改系統默認的啓動運行級別的方式是編輯/etc/inittab
,systemd 是向前兼容的,那麼systemd 的運行級別爲:
runlevel0.target, poweroff.target runlevel1.target, rescue.target runlevel2.target, multi-user.target runlevel3.target, multi-user.target runlevel4.target, multi-user.target runlevel5.target, graphical.target runlevel6.target, reboot.target
運行級別切換:
systemctl isolate name.target 查看當前默認運行級別: systemctl get-default 修改默認級別: systemctl set-default name.target
電源管理:
關機: systemctl halt | systemctl poweroff 重啓: systemctl reboot 休眠: systemctl hibernate 混合休眠模式:(同時休眠硬盤並待機) systemctl hybrid-sleep