遺忘 Root 密碼是常見運維場景。本文演示通過 GRUB 進入單用戶模式(Single User Mode),無需舊密碼即可重置 Root 密碼的完整步驟,適用於 GRUB Legacy (0.97) 與 GRUB 2。
kernel /vmlinuz... 開頭的行,再按 e single(前面必須有空格)kernel /vmlinuz-2.6.32 ro root=LABEL=/ rhgb quiet singlesh-4.1#),無需密碼linux 或 linux16 / linuxefi 開頭的行 rd.break(推薦)或 single / init=/bin/bashlinux /vmlinuz-3.10.0 ro root=/dev/mapper/centos-root rd.break
# 進入 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 提示符下
linux single
# 或
linux init=/bin/bash
/etc/shadow 密碼哈希已更新/etc/passwd Root shell 為 /bin/bashgrub2-setpassword)lastb、/var/log/secure 監控異常登錄GRUB Legacy 用 single,GRUB 2 推薦 rd.break + chroot /sysroot + autorelabel。操作前請確認有物理/控制台訪問權限,並遵守授權範圍。