chkconfig 是 SysVinit 時代管理服務啟動等級的標準工具(systemd 系統請用 systemctl)。本文整理語法、參數、運行等級對照、服務腳本規範與實戰範例。
| 等級 | 名稱 | 說明 |
|---|---|---|
| 0 | halt | 關機 |
| 1 | single | 單用戶模式 |
| 2 | multi-user | 無網路多用戶命令列 |
| 3 | multi-user+net | 有網路多用戶命令列 |
| 4 | unused | 保留 |
| 5 | graphical | 圖形介面多用戶 |
| 6 | reboot | 重啟 |
chkconfig --list [服務名] # 顯示所有/指定服務各等級狀態
chkconfig --add 服務名 # 加入管理(建立 rc.d 連結)
chkconfig --del 服務名 # 移除管理(刪除 rc.d 連結)
chkconfig --level 等級 服務名 on/off/reset # 設定指定等級啟動/關閉/重置
chkconfig 服務名 on/off # 設定預設等級 (2345) 啟動/關閉
需包含 chkconfig 註解:
# chkconfig: 2345 20 80
# description: 服務描述,可用 \ 換行
# 更多描述...
2345:預設啟動等級20:啟動優先序(S20),數字越小越早啟動80:停止優先序(K80),數字越小越早停止- 代替(如 chkconfig: - 20 80)chkconfig --list # 列出所有服務
chkconfig --add httpd # 加入 httpd
chkconfig --del httpd # 移除 httpd
chkconfig --level 2345 httpd on # 設定 2345 等級開啟
chkconfig --list mysqld # 查看 mysqld 狀態
chkconfig --level 35 mysqld on # 設定 3、5 等級開啟
chkconfig mysqld on # 設定 2345 等級開啟
/etc/init.d/,加執行權限 chmod +xchkconfig --add 服務名 註冊chkconfig --level 35 服務名 on 設定啟動等級| chkconfig | systemctl |
|---|---|
chkconfig --list | systemctl list-unit-files --type=service |
chkconfig service on | systemctl enable service |
chkconfig service off | systemctl disable service |
chkconfig --level 35 service on | systemctl enable service (systemd 無等級概念,以 target 替代) |
雖然 systemd 已取代 SysVinit,但理解 chkconfig 有助於維護舊系統、閱讀舊腳本與跨平台移植。