遺忘 root 密碼是系統管理常見挑戰。透過在 GRUB 開機選單修改核心參數進入單用戶模式(或 emergency mode),即可在無需原密碼的情況下重置 root 密碼。本文整理主流發行版(RHEL/CentOS/Fedora、Debian/Ubuntu、openSUSE、Arch Linux、FreeBSD、Solaris)的具體操作步驟與注意事項。
linux/linuxefi 行尾加入 rd.break(RHEL系)或 init=/bin/bash(Debian/Ubuntu/Arch),核心將啟動最小環境並掛載根檔案系統(通常為唯讀)。mount -o remount,rw /sysroot 或 mount -o remount,rw /。chroot /sysroot 或直接操作。passwd root 設定新密碼。/.autorelabel(SELinux 系統)觸發下次開機重新標記安全上下文,否則登入失敗。reboot -f 或 exec /sbin/init。linux 或 linuxefi 開頭的行,行尾加入 rd.break。mount -o remount,rw /sysroot
chroot /sysroot
passwd root
touch /.autorelabel
exit
exit
linux 行,行尾加入 init=/bin/bash(或 systemd.unit=emergency.target)。mount -o remount,rw /
passwd root
# 或 passwd 您的用戶名
sync
reboot -f
同 RHEL 系,使用 rd.break 或 init=/bin/bash,後續步驟相同。
linux 行尾加 init=/bin/bash。mount -o remount,rw /
passwd root
sync
reboot -f
mount -u -w /
fsck -y
passwd root
reboot
/a 時選 y。passwd root
reboot
RHEL 系系統必須執行 touch /.autorelabel(在 chroot 內),否則重開機後 SELinux 會因檔案安全上下文錯誤導致登入失敗、passwd 修改無效。若忘記,可再次進入單用戶模式補上。
chroot 修改 /etc/shadow。mount /dev/sdXn /mnt(含 /boot, /boot/efi 等獨立分區)。chroot /mntpasswd rootexit; umount -R /mnt; reboot此法不受 GRUB/SELinux/Secure Boot 限制,為最通用方案。
重置 root 密碼核心在於「取得根檔案系統寫入權限」與「正確處理 SELinux 標籤」。熟悉 GRUB 編輯參數(rd.break、init=/bin/bash)與 Live USB chroot 兩大流派,即可應對絕大多數 Linux 發行版的密碼遺忘情境。