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 忘記 Root 密碼?GRUB 單用戶模式重置全流程

編輯:Linux 教學

遺忘 Root 密碼是常見運維場景。本文演示通過 GRUB 進入單用戶模式(Single User Mode),無需舊密碼即可重置 Root 密碼的完整步驟,適用於 GRUB Legacy (0.97) 與 GRUB 2。

GRUB Legacy (CentOS 6、舊版系統)

步驟 1:進入 GRUB 菜單編輯模式

  1. 開機出現 GRUB 計數器時,按任意鍵停止倒計時
  2. 選中啟動內核行,按 e 進入編輯

步驟 2:編輯內核參數

  1. 選中以 kernel /vmlinuz... 開頭的行,再按 e
  2. 在行末添加 single(前面必須有空格)
    kernel /vmlinuz-2.6.32 ro root=LABEL=/ rhgb quiet single
  3. 按 Enter 確認

步驟 3:啟動單用戶模式

  1. 回到主菜單,按 b 啟動
  2. 系統啟動後直接進入 Root Shell(提示符 sh-4.1#),無需密碼

GRUB 2 (CentOS 7/8、Ubuntu 16.04+、Debian 8+)

步驟 1:進入 GRUB 編輯

  1. 開機 GRUB 菜單,選中啟動項,按 e
  2. 找到以 linux 或 linux16 / linuxefi 開頭的行

步驟 2:添加內核參數

  1. 在行末添加 rd.break(推薦)或 single / init=/bin/bash
    linux /vmlinuz-3.10.0 ro root=/dev/mapper/centos-root rd.break
  2. Ctrl+x 或 F10 啟動

步驟 3:掛載 sysroot 並重置密碼


# 進入 emergency shell 後
mount -o remount,rw /sysroot          # 重新掛載為讀寫
chroot /sysroot                       # 切換根目錄
passwd root                           # 修改密碼
touch /.autorelabel                   # 觸發 SELinux 重標記(關鍵!)
exit                                  # 退出 chroot
exit                                  # 退出 emergency shell,系統繼續啟動
  • rd.break 在 initramfs 階段中斷,進入 /sysroot 環境
  • touch /.autorelabel 確保 SELinux 文件上下文正確,否則重啟後可能無法登錄

LILO 引導器(極少見)


# LILO 提示符下
linux single
# 或
linux init=/bin/bash

驗證與收尾

  1. 系統完全啟動後,使用新密碼登錄
  2. 檢查 /etc/shadow 密碼哈希已更新
  3. 建議檢查 /etc/passwd Root shell 為 /bin/bash
  4. 安全加固建議

    • GRUB 設置密碼:防止未授權編輯啟動參數(grub2-setpassword)
    • BIOS/UEFI 設置密碼:防止從外部介質啟動繞過
    • 磁盤加密 (LUKS):物理訪問也無法讀取數據
    • 記錄審計:lastb、/var/log/secure 監控異常登錄

    總結

    GRUB Legacy 用 single,GRUB 2 推薦 rd.break + chroot /sysroot + autorelabel。操作前請確認有物理/控制台訪問權限,並遵守授權範圍。

Copyright © Windows教程網 All Rights Reserved