Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 服務啟動停止管理 (chkconfig 與 rc.d 目錄結構)

編輯:Linux 教學

說明 Linux 七大運行級別對應的服務啟動/停止機制,chkconfig 指令操作與 /etc/rc.d/rc[0-6].d/ 目錄下 K*/S* 腳本的執行邏輯。

查看服務在各級別狀態


chkconfig --list

輸出範例:sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

修改服務在特定級別啟用/停用


chkconfig sshd --level 3 off   # 關閉級別 3 的 sshd
chkconfig sshd --level 3 on    # 開啟級別 3 的 sshd

手動操作 rc.d 目錄 (底層機制)


ls /etc/rc.d/
# init.d  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local  rc.sysinit

以 rc3.d 為例:


ls /etc/rc.d/rc3.d/
K02avahi-dnsconfd  K89pand  S25bluetooth  S25netfs ...
  • K 開頭 = 該級別需**停止**的服務,數字為停止順序 (越小越早)。
  • S 開頭 = 該級別需**啟動**的服務,數字為啟動順序。
  • 執行順序:**先全數執行 K* (按數字升序),再執行 S* (按數字升序)**。
  • S 腳本會覆蓋 K 腳本 (同一服務若同時存在 K 和 S,最終以 S 為準)。

腳本實體與連結關係

  • 實體腳本放在 /etc/rc.d/init.d/ (或 /etc/init.d/)。
  • rcN.d/ 下皆為符號連結:
    ln -s ../init.d/sshd /etc/rc.d/rc3.d/S55sshd
  • chkconfig 實質是自動管理這些符號連結的建立/刪除/改名。

自訂開機腳本

將需開機執行的命令寫入 /etc/rc.d/rc.local (或 /etc/rc.local),系統進入多用戶模式前會執行。

systemd 時代備註

CentOS 7+ 採用 systemd,服務管理改用 systemctl enable/disable/start/stop,運行級別對應為 target (如 multi-user.target = 3, graphical.target = 5)。chkconfig 仍保留相容但建議改用 systemd 原生指令。

Copyright © Windows教程網 All Rights Reserved