Root 密碼遺忘是 Linux 運維必遇場景。現代發行版 (CentOS 7/8, RHEL 7/8, Ubuntu 18.04+, Debian 9+) 均使用 GRUB2,重置流程統一為:中斷 GRUB → 編輯內核參數進入急救模式 → 重掛載根分區讀寫 → 修改密碼 → 重啟。本文提供通用 GRUB2 流程、舊版 GRUB Legacy 流程、SELinux 重標記、關鍵故障排查。
Shift (或反複按 Esc)。Esc 或 F12 進入 Firmware 菜單選擇 GRUB。e 進入編輯模式。linux / linuxefi / linux16 開頭的行 (內核參數行)。rd.break (RHEL/CentOS/Fedora) 或 init=/bin/bash (Debian/Ubuntu/通用)。Ctrl+x 或 F10 啟動。
# --- 方案 A: rd.break (RHEL 系) ---
# 系統掛載真實根分區到 /sysroot,且為只讀
mount -o remount,rw /sysroot
chroot /sysroot
passwd root
# 輸入新密碼兩次
touch /.autorelabel # 關鍵!SELinux 重標記,防止下次啟動卡住/拒絕登錄
exit
mount -o remount,ro /sysroot
reboot
# --- 方案 B: init=/bin/bash (Debian/Ubuntu/通用) ---
# 直接進入根 Shell,根分區通常只讀
mount -o remount,rw /
passwd root
# 若啟用 SELinux (Ubuntu 默認不啟用,CentOS 默認啟用)
touch /.autorelabel
sync
reboot -f # 或 exec /sbin/init
# 1. 開機按任意鍵進入 GRUB 菜單
# 2. 選中內核,按 e 編輯
# 3. 選中 kernel 行,按 e
# 4. 行尾加 single 或 1 (運行級別 1)
# 5. 按 Enter,再按 b 啟動
# 6. 進入單用戶模式 (無需密碼)
# 7. passwd root
# 8. reboot
/etc/shadow 後,文件安全上下文可能不匹配策略,導致登錄失敗、啟動卡在 "SELinux policy"。touch /.autorelabel 會在下次啟動觸發全盤重標記 (耗時數分鐘),務必執行。restorecon -Rv /etc/shadow 或 autorelabel。rd.break:真實根在 /sysroot,需 chroot /sysroot。init=/bin/bash:真實根在 /,直接操作。lsblk -f、findmnt 確認掛載情況。rd.break 通常已自動激活 VG/LV,掛載至 /sysroot。rd.luks.uuid=UUID,或進入急救模式後手動 cryptsetup open /dev/sda2 root、vgchange -ay、mount /dev/mapper/vg-root /sysroot。passwd -l root / /etc/shadow 以 ! 開頭)
passwd -u root # 解鎖
passwd root # 重設密碼
# 1. 掛載系統根分區
mount /dev/sda2 /mnt
# 若有單獨 /boot: mount /dev/sda1 /mnt/boot
# 若 LVM: vgchange -ay; mount /dev/mapper/vg-root /mnt
# 2. chroot 進入
chroot /mnt
# 3. 修改密碼、修復 GRUB、檢查 fstab 等
passwd root
# 4. 退出重啟
exit
reboot
PermitRootLogin no),普通用戶 sudo 提權。PasswordAuthentication no)。grub2-setpassword) 防止惡意進入單用戶模式。忘記 root 密碼不恐慌。GRUB2 + rd.break/init=/bin/bash + chroot + passwd + .autorelabel 是現代 Linux 通用解法。熟練掌握 GRUB 編輯、掛載點識別、SELinux 重標記、LVM/LUKS 處理,能應對 99% 密碼重置場景。

Linux man 手冊頁完整使用指南:章節、導航與搜尋技巧
CentOS 7 搭建 Nginx 反向代理 + proxy_cache CDN 節點完整實戰教學
SUSE Linux 建立 Samba 共享資料夾:smb.conf 設定、服務啟動與開機自動掛載
CentOS wget 進度條多行顯示亂碼修復:locale 翻譯錯誤導致換行、msgunfmt/msgfmt 修正 .mo 檔案
CentOS 7 搭建 Pacemaker/Corosync 高可用叢集 (HA) 完整實戰:雙節點 DRBD + VIP + Nginx
Linux expr 指令完全攻略:字串操作、整數運算與模式匹配
Linux 檔案管理核心指令實戰:touch 建立/更新時間、mkdir 建立目錄、cp/mv/rm 複製移動刪除、ln 連結