︿
Top

前言

最近工作關係開始玩 CentOS 了,本次實作環境中採用的是 CentOS 7.4 (1709) Kernel 3.10.0-693.el7.x86_64) 映像檔,也就是新版 CentOS 7.4 最小化安裝版本 (Minimal Install),那麼開始來玩玩吧。💪



實作環境




Systemd 是什麼?

簡單來說,從 CentOS 7 版本開始在管理系統的部分,已經從過往傳統的 Runlevel (/etc/rc.d/init.d) 改為新一代的 systemd  (/etc/systemd/system)。因此,倘若查看舊有 Runlevel 組態設定檔 (/etc/inittab) 內容會發現是空的 (詳細資訊請參考 RHEL 7 System Administrator Guide - Chapter 9. Managing Services with systemd)。

圖、systemd 系統運作架構示意圖
圖片來源: systemd - Wikipedia



CentOS 7 開機程序

談到 CentOSSystemd 啟動模式等級,便要先了解一下整個 CentOS 開機過程。透過下列的開機流程說明,便會了解到在 Systemd 啟動模式,為何能夠掌控系統後半段開機階段的相關服務啟動及關閉。下列開機流程是以安裝於 x86 硬體上的 CentOS 系統進行說明 (詳細資訊請參考 Overview of systemd for RHEL 7 - Red Hat Customer Portal):

  • 從 BIOS 所選的媒體裝置 (例如,本機硬碟) 載入 Boot Loader (RHEL 7 / CentOS 7 採用 GRUB2)。
  • 啟動 Kernel Initial RAM Disk
  • Systemd 執行程序初始化系統並啟動所有系統服務 (讀取 default.target 內容)。
  • Multi-User Mode (/lib/systemd/system/multi-user.target) 裡面有一行 Requires=basic.target 內容,表示系統將會載入  Basic.traget  (載入 RHEL7 System)。
  • Basic.traget (/usr/lib/systemd/system/basic.target) 裡面有一行 Requires=sysinit.target 內容,表示系統將會載入  Sysinit.traget (載入 System Initialization Services)。
  • Sysinit.target (/usr/lib/systemd/system/sysinit.target) 裡面有一行 Wants=local-fs.target swap.target 內容,表示將會載入 local-fs.target swap.target (執行 Mounting File Systems 及 Enabling Swap Devices)。此外,還會處理 enable logging、set kernel options、start the udevd daemon to detect hardware、allow file system decryption、iSCSI、multipath、LVM monitoring、RAID services。
  • local-fs.target (/usr/lib/systemd/system/local-fs.target) 裡面有一行 After=local-fs-pre.target 內容,表示等 local-fs-pre.target 完成後才載入。




Systemd 啟動模式等級

本文實作環境採用 CentOS 7.4 Minimal Install,預設情況下便會採用「Multi-User Mode」(類似舊有的 Runlevel 3 運作環境)。你可以透過查看「/etc/systemd/system/default.target」內容,或者執行「systemctl get-default」指令即可查詢,目前 CentOS 主機的啟動模式等級。
# ls -l /etc/systemd/system/default.target
lrwxrwxrwx. 1 root root 37 Oct 24 08:28 /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target
# systemctl get-default
multi-user.target




接著,我們可以透過查看「/etc/systemd/system/multi-user.target.wants」內容,或「systemctl list-units --type service |grep running」指令了解 Multi-User Mode 的運作模式預設會啟用哪些系統服務。
# ls -l /etc/systemd/system/multi-user.target.wants
total 0
lrwxrwxrwx. 1 root root 38 Oct 24 08:11 auditd.service -> /usr/lib/systemd/system/auditd.service
lrwxrwxrwx. 1 root root 39 Oct 24 08:11 chronyd.service -> /usr/lib/systemd/system/chronyd.service
lrwxrwxrwx. 1 root root 37 Oct 24 08:11 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx. 1 root root 41 Oct 24 08:11 firewalld.service -> /usr/lib/systemd/system/firewalld.service
lrwxrwxrwx. 1 root root 47 Oct 24 00:30 hv_fcopy_daemon.service -> /usr/lib/systemd/system/hv_fcopy_daemon.service
lrwxrwxrwx. 1 root root 45 Oct 24 00:30 hv_kvp_daemon.service -> /usr/lib/systemd/system/hv_kvp_daemon.service
lrwxrwxrwx. 1 root root 45 Oct 24 00:30 hv_vss_daemon.service -> /usr/lib/systemd/system/hv_vss_daemon.service
lrwxrwxrwx. 1 root root 42 Oct 24 08:11 irqbalance.service -> /usr/lib/systemd/system/irqbalance.service
lrwxrwxrwx. 1 root root 37 Oct 24 08:11 kdump.service -> /usr/lib/systemd/system/kdump.service
lrwxrwxrwx. 1 root root 39 Oct 24 08:11 postfix.service -> /usr/lib/systemd/system/postfix.service
lrwxrwxrwx. 1 root root 40 Oct 24 08:11 remote-fs.target -> /usr/lib/systemd/system/remote-fs.target
lrwxrwxrwx. 1 root root 39 Oct 24 08:11 rsyslog.service -> /usr/lib/systemd/system/rsyslog.service
lrwxrwxrwx. 1 root root 36 Oct 24 08:11 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx. 1 root root 37 Oct 24 08:11 tuned.service -> /usr/lib/systemd/system/tuned.service
# systemctl list-units --type service |grep running
auditd.service loaded active running Security Auditing Service
chronyd.service loaded active running NTP client/server
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
firewalld.service loaded active running firewalld - dynamic firewall daemon
getty@tty1.service loaded active running Getty on tty1
hv_fcopy_daemon.service loaded active running Hyper-V FCOPY daemon
hv_kvp_daemon.service loaded active running Hyper-V KVP daemon
hv_vss_daemon.service loaded active running Hyper-V VSS daemon
irqbalance.service loaded active running irqbalance daemon
polkit.service loaded active running Authorization Manager
postfix.service loaded active running Postfix Mail Transport Agent
rsyslog.service loaded active running System Logging Service
sshd.service loaded active running OpenSSH server daemon
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-udevd.service loaded active running udev Kernel Device Manager
tuned.service loaded active running Dynamic System Tuning Daemon


倘若,希望了解支援哪些運作層級類型,請執行「systemctl list-units --type=target」指令即可查詢。
# 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
network-online.target loaded active active Network is Online
network-pre.target loaded active active Network (Pre)
network.target loaded active active Network
paths.target loaded active active Paths
remote-fs.target loaded active active Remote File Systems
slices.target loaded active active Slices
sockets.target loaded active active Sockets
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.

16 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.




Systemctl 系統服務管理常用參數

在傳統的 Runlevel 運作環境中,我們常常會使用「service / chkconfig」指令來管理系統服務。現在,新一代的 Systemd 運作環境中一律使用「systemctl」指令來管理系統服務即可。下列為搭配 systemctl 指令管理系統服務的常用參數:

  • status: 查詢指定的系統服務運作狀態,例如,systemctl status sshd。
  • stop: 停止指定的系統服務,例如,systemctl stop sshd。
  • start: 啟動指定的系統服務,例如,systemctl start sshd。
  • enable: 設定指定的系統服務開機時自動啟動,例如,systemctl enable sshd。
  • disable: 設定指定的系統服務開機時自動啟動,例如,systemctl disable sshd。
  • list-dependencies: 查詢指定的系統服務相依性資訊,例如,systemctl list-dependencies sshd。
  • list-units: 查詢系統服務類型資訊,例如,systemctl list-units --type service 或 systemctl list-units --type mount。
  • list-unit-files: 列出所有系統服務運作狀態,例如,systemctl list-unit-files。

# systemctl status sshd
sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-10-24 14:35:21 CST; 1h 22min ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 3863 (sshd)
   CGroup: /system.slice/sshd.service
           └─3863 /usr/sbin/sshd -D

Oct 24 14:22:18 centos74.weithenn.org sshd[3985]: pam_unix(sshd:auth): authentication failure; logname= uid=0 eu...root
Oct 24 14:22:18 centos74.weithenn.org sshd[3985]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met b...oot"
Oct 24 14:22:20 centos74.weithenn.org sshd[3985]: Failed password for root from 10.10.75.16 port 49279 ssh2
Oct 24 14:22:30 centos74.weithenn.org sshd[3985]: Connection closed by 10.10.75.16 port 49279 [preauth]
Oct 24 14:24:02 centos74.weithenn.org sshd[4008]: pam_unix(sshd:auth): authentication failure; logname= uid=0 eu...root
Oct 24 14:24:02 centos74.weithenn.org sshd[4008]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met b...oot"
Oct 24 14:24:04 centos74.weithenn.org sshd[4008]: Failed password for root from 10.10.75.16 port 49281 ssh2
Oct 24 14:27:20 centos74.weithenn.org sshd[4044]: pam_unix(sshd:auth): authentication failure; logname= uid=0 eu...root
Oct 24 14:27:20 centos74.weithenn.org sshd[4044]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met b...oot"
Oct 24 14:27:22 centos74.weithenn.org sshd[4044]: Failed password for root from 10.10.75.16 port 49282 ssh2
Hint: Some lines were ellipsized, use -l to show in full.




CentOS 7.4 基礎設定系列文章:

文章標籤: ,