systemctl — 管理 systemd 系統服務

管理 systemd 服務的啟動、停止、狀態查看與開機自動啟動設定。

語法

systemctl [指令] [unit...]

常用選項

選項 說明 範例
start 服務 立即啟動服務 systemctl start nginx
stop 服務 立即停止服務 systemctl stop nginx
restart 服務 重新啟動服務(stop + start) systemctl restart nginx
reload 服務 重新讀取設定(不重啟程序) systemctl reload nginx
status 服務 顯示服務的當前狀態與最近日誌 systemctl status nginx
enable 服務 設定開機時自動啟動 systemctl enable nginx
disable 服務 取消開機自動啟動 systemctl disable nginx
is-active 服務 檢查服務是否正在執行(回傳碼) systemctl is-active nginx
is-enabled 服務 檢查服務是否設定為開機啟動 systemctl is-enabled nginx
list-units 列出所有已載入的 unit systemctl list-units
list-unit-files 列出所有已安裝的 unit 及其啟用狀態 systemctl list-unit-files
daemon-reload 重新載入 unit 設定檔(修改後必須執行) systemctl daemon-reload

使用範例

範例 1:查看服務狀態

查看 nginx 的執行狀態、PID、記憶體使用,以及最近的 log 輸出。

$ systemctl status nginx
● nginx.service - A high performance web server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2026-05-20 08:00:01 CST; 2h 30min ago
  Process: 1234 ExecStart=/usr/sbin/nginx
 Main PID: 1234 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1234 nginx: master process
           └─1235 nginx: worker process
範例 2:啟動服務

立即啟動 nginx 服務(若服務已在執行,會有提示但不報錯)。

$ sudo systemctl start nginx
範例 3:設定開機自動啟動

使用 enable 讓服務在系統開機時自動啟動。常與 start 一起使用。

$ sudo systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
--now 選項同時執行 enable 和 start,一個指令搞定。
範例 4:重新載入設定(不中斷服務)

修改 nginx.conf 後,用 reload 讓 nginx 讀取新設定,不需要中斷現有連線。

$ sudo systemctl reload nginx
範例 5:重新載入 unit 設定

自行建立或修改 /etc/systemd/system/ 下的 unit 檔後,必須先 daemon-reload 才能生效。

$ sudo systemctl daemon-reload && sudo systemctl restart myapp
範例 6:列出所有執行中的服務

搭配 grep 過濾只顯示正在執行(running)的服務。

$ systemctl list-units --type=service --state=running
UNIT                   LOAD   ACTIVE SUB     DESCRIPTION
nginx.service          loaded active running A high performance web server
ssh.service            loaded active running OpenBSD Secure Shell server

常見錯誤與排錯

Failed to start nginx.service: Unit not found.
原因
服務名稱拼錯,或尚未安裝該服務。
解法
確認服務名稱:systemctl list-unit-files | grep nginx,或安裝對應套件。
Failed to enable unit: Created symlink exists already.
原因
服務已設定為開機啟動,重複執行 enable 時出現此訊息。
解法
這不是錯誤,可放心忽略。若想確認:systemctl is-enabled servicename。

延伸閱讀