操作步骤
将 Linux 机器重新启动到启动菜单,如下所示
在 GRUB 菜单中编辑主引导加载程序,你可以看到我只安装了一个内核,按“e”进入编辑模式
将进入以下菜单:
setparams 'Red Hat Enterprise Linux Server (3.10.0-693.el7.x86_64) 7.4 (Maipo)' load_video set gfxpayload=keep insmod gzio insmod part_msdos insmod xfs set root='hd0,msdos1' if [ x$feature_platform_search_hint = xy ]; then search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' af0be92e-0721-46b8-affd-60f9baff3652 else search --no-floppy --fs-uuid --set=root af0be92e-0721-46b8-affd-60f9baff3652 fi linux16 /vmlinuz-3.10.0-693.el7.x86_64 root=/dev/mapper/rhel-root ro rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet net.ifnames=0 systemd.unit=emergency.target initrd16 /initramfs-3.10.0-693.el7.x86_64.img
按键说明:
按 Ctrl-x 利用刚修改的参数启动系统
按Ctrl-c 进入命令提示符
按Escape 忽略修改,并返回菜单
按 Tab列出可能的补全内容
注意类似下面的内核行:
linux16 /vmlinuz-3.10.0-693.el7.x86_64 root=/dev/mapper/rhel-root ro rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet net.ifnames=0
删除“rhgb quiet”并在最后添加“rd.break”,如下所示
linux16 /vmlinuz-3.10.0-693.el7.x86_64 root=/dev/mapper/rhel-root ro rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap net.ifnames=0 rd.break
rd.break 将在安装文件系统之前停止引导程序,其中所有支持模块都已加载并具有有效的shell。
按“Ctrl + x”以新的设置启动服务器。
我们将得到一个 shell,不需要输入 root 密码。
接下来,我们必须重新挂载具有读写权限的文件系统,因为当前我们已使用根文件系统是以只读模式登录的
switch_root:/# mount -o remount,rw /sysroot
接下来我们需要将 sysroot 的内容从 chroot 移动到普通的 root 环境
switch_root:/# chroot /sysroot
现在我们已准备好设置密码。
我们可以使用以下命令设置密码。
将“mynewpassword”替换为新密码,后跟用户名,例如:“root”
sh-4.2# echo mynewpassword | passwd --stdin root
接下来我们需要告诉 selinux 它需要重新标记,否则我们的更改将在重启后消失,为此使用以下命令创建一个新的自动标记文件
sh-4.2# touch /.autorelabel
完成后,按“Ctrl + D”两次退出窗口,然后重新启动。
当服务器重启后,就可以尝试使用新密码登录
在 Red Hat Enterprise Linux/Cent OS 6 中,更改 root 密码非常简单(知道步骤很容易,不知道就很难)
对于 RHEL/CentOS 7,这些步骤与旧 RHEL 版本中的步骤完全不同,而且有点棘手。
在本文中,将介绍使用 rd.break 重置 root 密码的步骤.