萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 電腦基礎知識 >  >> 電腦入門

Linux 查看與管理服務指令大全:chkconfig 與 systemctl 實戰

Linux 下查看所有服務狀態、啟停控制、設定開機自啟,核心指令是 chkconfig (SysVinit) 與 systemctl (systemd)。現代主流發行版 (CentOS 7+, Ubuntu 16.04+, Debian 8+, RHEL 7+) **預設使用 systemd**,請優先掌握 systemctl。

systemd 系統

查看服務狀態

# 列出所有已載入服務單元 (含啟用/停用/靜態)
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 # 查詢是否已啟用

SysVinit 系統 (CentOS 6 等舊版)

chkconfig 指令

# 查看所有服務在各執行等級的狀態
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 指令

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。
copyright © 萬盛學電腦網 all rights reserved