Linux 下查看所有服務狀態、啟停控制、設定開機自啟,核心指令是 chkconfig (SysVinit) 與 systemctl (systemd)。現代主流發行版 (CentOS 7+, Ubuntu 16.04+, Debian 8+, RHEL 7+) **預設使用 systemd**,請優先掌握 systemctl。
# 列出所有已載入服務單元 (含啟用/停用/靜態)
systemctl list-unit-files --type=service
# 查看特定服務詳細狀態
systemctl status httpd
# 僅列出運行中服務
systemctl list-units --type=service --state=running
systemctl start httpd # 啟動
systemctl stop httpd # 停止
systemctl restart httpd # 重啟
systemctl reload httpd # 重新載入設定 (不中斷服務)
systemctl enable httpd # 設定開機自啟
systemctl disable httpd # 取消開機自啟
systemctl is-enabled httpd # 查詢是否已啟用
# 查看所有服務在各執行等級的狀態
chkconfig --list
# 查看特定服務
chkconfig --list httpd
# 輸出範例: httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# 設定執行等級 3,4,5 啟用
chkconfig --level 345 httpd on
# 設定關閉
chkconfig --level 345 httpd off
service httpd start
service httpd stop
service httpd restart
service httpd status
| 功能 | systemd (新) | SysVinit (舊) |
|---|---|---|
| 服務管理單位 | .service 單元檔 | /etc/init.d/ 腳本 |
| 平行啟動 | 支援 (快) | 串列 (慢) |
| 相依性處理 | 自動解析 | 腳本內手動處理 |
| 日誌整合 | journald | 分散式文字日誌 |
systemctl mask httpd:完全遮蔽服務,防止被其他服務意外啟動(解除用 unmask)。systemctl edit httpd:建立覆寫片段修改服務參數,不動原單元檔。systemd-analyze blame。