Systemd 是 CentOS 7 取代 SysV init 與 Upstart 的新一代初始化系統。本文系統整理其核心概念、unit 類型與常用命令。
開機流程:POST → Boot Sequence → Bootloader → kernel + initramfs → rootfs → /sbin/init。CentOS 5 用 SysV init,CentOS 6 用 Upstart,CentOS 7 則是 Systemd。
開機時服務並行啟動、按需激活進程、系統狀態快照、基於相依關係定義服務控制邏輯。
以設定檔標識與配置,內容包含系統服務、監聽 socket、系統快照等資訊,保存於 /usr/lib/systemd/system、/run/systemd/system、/etc/systemd/system。
Unit 類型:Service(.service)、Target(.target,模擬運行級別)、Device(.device)、Mount(.mount)、Socket(.socket)、Snapshot(.snapshot)、Swap(.swap)、Automount(.automount)、Path(.path)。
關鍵特性:基於 socket、bus、device、path 的激活機制,系統快照,向後相容 sysv init 腳本。不相容部分:systemctl 命令固定不變、非 systemd 啟動的服務無法以 systemctl 通訊。
systemctl start name.service # 啟動,等同 service name start systemctl stop name.service # 停止 systemctl restart name.service # 重啟 systemctl status name.service # 狀態 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 # 取消禁止 systemctl is-active name.service # 是否激活 systemctl list-units --type service # 已激活服務 systemctl list-units --type service --all # 所有服務
對應 chkconfig:enable/disable 設定開機自啟,list-unit-files --type service 查看自啟狀態,is-enabled 查詢單一服務。查相依用 systemctl list-dependencies。
0 → poweroff.target;1 → rescue.target;2/3/4 → multi-user.target;5 → graphical.target;6 → reboot.target。切換級別用 systemctl isolate name.target;查看用 list-units --type target;get-default 取得預設級別;set-default 修改;rescue/emergency 進入救援與緊急模式。
關機 halt/poweroff、重啟 reboot、掛起 suspend、快照 hibernate、快照並掛起 hybrid-sleep。
本文出自「梁小明的博客」博客,請務必保留此出處 https://7038006.blog.51cto.com/7028006/1842197