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

Linux root 密碼遺忘?三種單用戶模式重置密碼實戰教學

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

好久沒碰 Linux,公司伺服器故障需登入卻忘記 root 密碼。經過一番鑽研,整理出三種透過單用戶模式重置密碼的方法,供遇到相同困境的管理員參考。

方法一:GRUB 編輯啟動參數法(適用於 GRUB 0.x/1.x)

  1. 開機出現 GRUB 選單時,用方向鍵選中 Linux 啟動項,按 e 編輯。
  2. 再次選中 kernel 行(如 kernel /boot/vmlinuz-2.4.18-14 ro root=LABEL=/),按 e。
  3. 在行尾加入 single,變為:
    kernel /boot/vmlinuz-2.4.18-14 single ro root=LABEL=/ single
  4. 按 Enter 返回,再按 b 啟動,直接進入單用戶命令列。
  5. 執行 vi /etc/shadow,刪除第一行 root: 到下一個 : 之間的加密密碼字串,存檔(變為 root::...)。
  6. 重啟 reboot,root 密碼即為空,可直接登入並重設新密碼。

方法二:安裝光盤進入 Rescue 模式掛載根分區

  1. 以安裝光盤啟動,選擇 linux rescue 模式。
  2. 掛載原根分區(假設為 /dev/hdaX):
    cd /mnt && mkdir hd && mount -t auto /dev/hdaX hd && cd hd && chroot ./
  3. 執行 passwd root 設定新密碼,退出重啟即可。

方法三:將硬碟掛載至另一 Linux 系統操作

  1. 同樣進入單用戶或 Rescue 模式掛載根分區。
  2. 執行 passwd root 修改密碼。
  3. 若無光盤,可將硬碟拆下掛載到其他 Linux 主機,以 chroot 方式修改 /etc/shadow 或執行 passwd。

提醒:方法二與三核心步驟相同,差異在於取得 root 存取的介質。實際操作前請確認分區裝置名(fdisk -l 查看),避免掛載錯誤導致資料損壞。

Copyright © Windows教程網 All Rights Reserved