萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux GRUB 開機密碼遺忘救援:單用戶模式與救援模式雙重破解實戰

遺忘 GRUB 開機密碼會阻礙進入單用戶模式重置 root 密碼,形成「雙重鎖死」。本文提供兩種通用破解路徑,適用於 GRUB Legacy (0.9x) 與 GRUB 2,涵蓋 BIOS 與 UEFI 環境。

方法一:Linux 救援模式(通用、推薦)

  1. 啟動安裝媒介:插入對應版本的安裝 ISO(光碟/USB),選擇 Rescue Mode 或 Troubleshooting → Rescue a Linux system。
  2. 掛載系統根分區:
    救援模式通常自動掛載至 /mnt/sysimage。若未自動掛載:
    mkdir /mnt/sysimage
    mount /dev/sda2 /mnt/sysimage # 根分區
    mount /dev/sda1 /mnt/sysimage/boot # /boot 獨立分區時
  3. 編輯 GRUB 配置移除密碼:
    GRUB Legacy (/mnt/sysimage/boot/grub/grub.conf 或 menu.lst):
    刪除或註釋 password [--md5] <hash> 行。
    GRUB 2 (/mnt/sysimage/boot/grub2/grub.cfg、/mnt/sysimage/etc/grub.d/40_custom):
    GRUB 2 密碼通常定義在 /etc/grub.d/40_custom 或 /boot/grub2/user.cfg。
    編輯 /mnt/sysimage/etc/grub.d/40_custom,刪除 set superusers= 與 password_pbkdf2 區塊。
    若使用 grub2-setpassword 設定,刪除 /mnt/sysimage/boot/grub2/user.cfg。
  4. 重新生成 GRUB 2 設定(如修改 40_custom):
    chroot /mnt/sysimage
    grub2-mkconfig -o /boot/grub2/grub.cfg
    exit
  5. 重啟驗證:移除安裝媒介,重啟系統,GRUB 選單不再提示密碼。

方法二:GRUB 命令列臨時繞過(無需外部媒介,僅限 GRUB Legacy/GRUB 2 互動模式)

GRUB Legacy

  1. 開機選單按 e 進入編輯模式。
  2. 選擇 kernel 行,再按 e,行尾加 single 或 init=/bin/bash。
  3. 按 b 啟動,進入單用戶/root shell。
  4. 掛載 /boot 寫入權限:mount -o remount,rw /boot。
  5. 編輯 /boot/grub/grub.conf 移除 password 行。
  6. reboot。

GRUB 2

  1. 開機選單按 e 編輯啟動項。
  2. 找到 linux / linux16 / linuxefi 行,行尾加 rd.break(進入 initramfs shell)或 init=/bin/bash。
  3. 按 Ctrl+x 啟動。
  4. rd.break 路徑:
    mount -o remount,rw /sysroot
    chroot /sysroot
    編輯 /etc/grub.d/40_custom 移除密碼設定。
    grub2-mkconfig -o /boot/grub2/grub.cfg
    touch /.autorelabel # SELinux 重新標記
    exit; exit 離開 chroot 與 initramfs shell,系統繼續啟動。

事後加固:重新設定強密碼

# GRUB 2 設定 PBKDF2 加密密碼
grub2-setpassword
# 輸入兩次新密碼,自動更新 /boot/grub2/user.cfg
# 重新生成配置
grub2-mkconfig -o /boot/grub2/grub.cfg

同步建議:BIOS/UEFI 設定開機密碼、啟用 Secure Boot、實體機箱加鎖,構建多層物理與邏輯防線,防止再次被繞過。

copyright © 萬盛學電腦網 all rights reserved