Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux 忘記 root 密碼?單用戶模式清除密碼 3 步驟搞定

編輯:Linux 系統常見問題解答

適用場景

實體機/虛擬機主控台可存取,但 root 密碼遺失。需以單用戶模式進入系統修改 /etc/shadow。

操作步驟

1. 進入 GRUB 選單,編輯啟動參數

  • 開機時快速按 Esc 或 Shift 進入 GRUB。
  • 選擇預設核心,按 e 編輯。
  • 找到以 linux 或 kernel 開頭的行,行尾加入 single 或 init=/bin/bash。
  • 按 Ctrl+X 或 b 啟動。

2. 重新掛載根分區為讀寫、清除密碼

# 若用 single 進入,通常已掛載唯讀,需重掛載
mount -o remount,rw /

# 編輯 shadow,清除 root 密碼欄位(第 2 欄)
vi /etc/shadow
# 找到 root 行:
# root:$6$salt$hash:18000:0:99999:7:::
# 將第 2 欄($6$...)刪除,變成:
# root::18000:0:99999:7:::
# 存檔離開

3. 重啟並設定新密碼

reboot
# 進入系統後無需密碼直接登入 root
passwd root
# 輸入新密碼兩次

注意事項

  • 部分發行版(如 Ubuntu)預設鎖定 root 帳號,單用戶模式可能直接給 root shell 無需密碼。
  • 若掛載失敗,嘗試在 GRUB 參數加 rw init=/bin/bash 直接進入可寫入 shell。
  • 生產環境建議配合 **GRUB 密碼保護** 防止未授權單用戶模式存取。

結語

單用戶模式 + /etc/shadow 清除密碼是 Linux 找回 root 權限的經典方法,操作需謹慎,建議事先演練。

Copyright © Windows教程網 All Rights Reserved