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 關機/重啟指令詳解:shutdown、halt、reboot、init 差異與最佳實踐

編輯:Linux 教學

Linux 提供多種關機重啟指令,理解其底層機制與參數差異,能確保資料安全、服務優雅停止,避免強制斷電導致檔案系統損壞。

1. shutdown —— 標準安全關機指令(推薦)

透過通知 init 切換 runlevel,讓服務依序停止、檔案系統同步寫入。

shutdown -h now        # 立即關機
shutdown -r now        # 立即重啟
shutdown -h +10 "系統將在10分鐘後關機"  # 延遲關機並廣播訊息
shutdown -c            # 取消已排程的關機

核心參數

參數作用
-h關機後切斷電源
-r重新開機
-k僅發送警告訊息,不真正關機
-t 秒數切換 runlevel 前等待秒數
-f重啟時跳過 fsck
-F重啟時強制 fsck
-n不經 init 直接關機(不建議)

2. halt —— 簡單關機(實際呼叫 shutdown -h)

halt            # 正常關機
halt -p         # 關機並切斷電源(預設)
halt -f         # 強制關機,不經 shutdown(危險)

適用於單用戶模式或緊急情況,但不如 shutdown 完善通知機制。

3. reboot —— 重啟系統

reboot          # 正常重啟
reboot -f       # 強制重啟(不建議)

行為類似 halt 但切換至 runlevel 6。

4. init / telinit —— 直接切換 runlevel

init 0          # 關機
init 6          # 重啟
init 1          # 單用戶維護模式
telinit 3       # 切換至多用戶文字模式

直接操作 init 進程,**不發送廣播訊息、無等待時間**,適用於腳本自動化或緊急切換。

Runlevel 對照表

Runlevel名稱用途
0halt關機
1single單用戶維護
2multi-user多用戶無 NFS
3multi-user完整多用戶(伺服器常用)
4unused保留
5graphical圖形化多用戶
6reboot重啟

最佳實踐建議

  • 日常維護:**優先使用 shutdown -h +1 "message"**,給用戶緩衝時間。
  • 腳本自動化:使用 systemctl poweroff / reboot(systemd 系統)或 init 0。
  • 避免直接斷電或 halt -f/reboot -f,除非系統已無回應。
  • 關機前手動執行 sync 確保緩衝區寫入磁碟。
Copyright © Windows教程網 All Rights Reserved