适用系统

  • CentOS 6 / 7 / 8 / Stream
  • Red Hat Enterprise Linux (RHEL) 6 / 7 / 8 / 9
  • Oracle Linux
  • Rocky Linux
  • AlmaLinux

方法一:rd.break 紧急模式(推荐)

适用版本:CentOS/RHEL 7 及以上

步骤 1:进入 GRUB 编辑模式

  1. 启动时在 GRUB 菜单按 e

  2. 找到以 linux16(BIOS)或 linuxefi(UEFI)开头的行:

1
linux16 /vmlinuz-3.10.0-1160.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=en_US.UTF-8
  1. End 键移动到行末,添加:
1
rd.break
  1. Ctrl+X 启动

步骤 2:修改密码

进入 switch_root:/# 后执行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 挂载为读写
mount -o remount,rw /sysroot

# 切换根目录
chroot /sysroot

# 修改密码
passwd root

# SELinux 重标记(必须!)
touch /.autorelabel

# 退出并重启
exit
reboot

重要说明

  • SELinux:RHEL 系默认启用 SELinux,不执行 touch /.autorelabel 会导致登录失败
  • 首次启动慢:重标记过程根据磁盘大小需要 2-10 分钟

方法二:init=/bin/bash

适用版本:所有版本

操作步骤

  1. GRUB 编辑模式下,在 linux16 行末添加:
1
init=/bin/bash
  1. Ctrl+X 启动

  2. 进入 bash 后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 重新挂载根分区
mount -o remount,rw /

# 如果使用 LVM,可能需要激活
lvm vgchange -ay

# 修改密码
passwd root

# SELinux 重标记
touch /.autorelabel

# 重启(init=/bin/bash 模式下 reboot 可能不工作)
exec /sbin/init
# 或强制重启
echo b > /proc/sysrq-trigger

方法三:single 单用户模式

适用版本:CentOS/RHEL 6

CentOS 6 使用 GRUB Legacy,操作不同:

  1. 启动时按任意键进入 GRUB 菜单

  2. 选择内核,按 a 键编辑启动参数

  3. 在行末添加:

1
single

1
1
  1. Enter 启动

  2. 直接进入 root shell,执行:

1
2
passwd root
reboot

CentOS 6 注意事项

  • CentOS 6 默认不启用 SELinux enforcing,通常不需要 .autorelabel
  • 如果启用了 SELinux,仍需执行重标记

方法四:rescue 救援模式

从安装光盘启动

  1. 使用 CentOS/RHEL 安装 ISO 启动

  2. 选择 TroubleshootingRescue a CentOS system

  3. 选择 1) Continue 让系统自动挂载到 /mnt/sysimage

  4. 执行:

1
2
3
4
5
chroot /mnt/sysimage
passwd root
touch /.autorelabel
exit
reboot

方法五:直接修改文件系统

通过外部挂载修改 shadow

shadow 文件位置

1
/etc/shadow

文件格式

1
用户名:加密密码:最后修改日期:最小间隔:最大间隔:警告天数:不活动天数:过期日期:保留字段

清空 root 密码

1
2
3
4
5
# 修改前
root:$6$rounds=4096$salt$hash...:18000:0:99999:7:::

# 修改后(第二字段留空)
root::18000:0:99999:7:::

设置已知密码

使用 openssl 生成密码哈希:

1
2
# 生成密码 "password123" 的哈希(SHA-512)
openssl passwd -6 -salt xyz password123

输出类似:

1
$6$xyz$hash...

将此哈希替换到 shadow 文件第二字段。

添加后门用户

/etc/passwd 添加:

1
forensic:x:0:0:Forensic Admin:/root:/bin/bash

/etc/shadow 添加:

1
forensic::18000:0:99999:7:::

LVM 分区挂载

CentOS/RHEL 默认使用 LVM,挂载步骤:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 扫描 LVM 物理卷
pvscan

# 扫描卷组
vgscan

# 激活所有卷组
vgchange -ay

# 查看逻辑卷
lvscan

# 挂载根分区(通常是 centos-root 或 rhel-root)
mount /dev/mapper/centos-root /mnt

# 挂载 boot 分区
mount /dev/sda1 /mnt/boot

SELinux 相关问题

问题:修改密码后仍无法登录

原因

SELinux 安全上下文未更新

解决方案 1:重标记

1
2
touch /.autorelabel
reboot

解决方案 2:临时禁用 SELinux

在 GRUB 启动参数添加:

1
selinux=0

1
enforcing=0

解决方案 3:手动修复上下文

1
2
3
chroot /sysroot
restorecon -v /etc/shadow
restorecon -v /etc/passwd

GRUB 密码保护绕过

如果 GRUB 设置了密码保护:

方法 1:使用 Live CD

  1. 从 Live CD 启动
  2. 挂载系统分区
  3. 编辑 /etc/grub.d/40_custom/boot/grub2/grub.cfg
  4. 删除 password_pbkdf2 相关行
  5. 重新生成 GRUB 配置(可选)

方法 2:直接修改 shadow

无需进入系统,直接通过文件系统修改


常见问题

Q: 看不到 linux16 行?

A: UEFI 模式下是 linuxefi,操作相同。

Q: rd.break 后提示只读文件系统?

A: 确保执行了 mount -o remount,rw /sysroot

Q: passwd 命令报错 “Authentication token manipulation error”?

A: 检查 /etc/shadow 文件权限,应为 000 或 640:

1
chmod 640 /etc/shadow

Q: reboot 命令无响应?

A: 使用强制重启:

1
echo b > /proc/sysrq-trigger

适用系统:CentOS 6/7/8/Stream, RHEL 6/7/8/9, Rocky Linux, AlmaLinux, Oracle Linux