Linux 忘记密码解决方法

通过 GRUB 进入单用户/rescue 模式重设 root 密码,以及云主机上的替代做法。

Intermediate
Key Points本地机器靠 GRUB 改内核参数进 rescue 或 rd.break 重置密码;云主机直接重置磁盘或走控制台。

RHEL / CentOS 系:rd.break

  1. 开机在 GRUB 菜单按 e 编辑启动项
  2. 在 linux 行末尾加 rd.break,按 Ctrl+X 启动
  3. 系统进入 switch_root 前的紧急 shell,根分区挂在 /sysroot(只读)
  4. 重新挂载为可写并改密码
Bash
mount -o remount,rw /sysroot
chroot /sysroot
passwd root
touch /.autorelabel        # SELinux 开启时必须,否则改完登不进去
exit
reboot

Debian / Ubuntu 系:recovery mode

Bash
# GRUB 菜单选 "Advanced options" → "recovery mode" → "root  Drop to root shell prompt"
mount -o remount,rw /
passwd root
exit
reboot

云主机怎么办

Provider featureHow
Reset passwordConsole → 实例 → 重置密码(需重启)
VNC console网页终端,可按上面步骤操作
Detach disk挂到另一台机器上改 /etc/shadow
Anyone with physical access can reset the root password by design. Real protection is disk encryption (LUKS) plus a firmware password — not hoping the password cannot be changed.

预防

  • 给自己建普通账号 + sudo,别只用 root
  • 把密码存进密码管理器
  • 云主机优先用 SSH 密钥,密码只作后备

Friends & Partners