How to reset or change lost root password in RHEL / CentOS 7

In Red Hat Enterprise Linux/Cent OS 6 changing root password was quiet easy (well it is easy if you know the steps and it is hard if you don't)

For RHEL / CentOS 7 the steps are completely different and a little trickier than it was in older RHEL versions.

In this article I will show you the steps to reset your root password using rd.break

NOTE: I hope you have console access to your Linux box as without this you cannot reset the password using an ssh client.
Reboot your Linux box to the boot menu as shown below

Edit the primary boot loader in the GRUB menu as for me you can see I only have one kernel installed to I will press "e" to enter into edit mode

which will bring you below menu

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

      Press Ctrl-x to start, Ctrl-c for a command prompt or Escape to 
      discard edits and return to the menu. Pressing Tab lists       
      possible completions.


Look out for the kernel line i.e. below one for me

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

Remove "rhgb quiet" and add "rd.break" in the last as shown below

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 will stop the boot procedure before mounting the filesystem wherein all the supporting modules are loaded and with a valid shell.
Press "Ctrl +x" to boot your box with the new changes

As you see now you have a shell and you were not prompted for root password

Next you must remount your filesystem with read write permission as currently you have logged in with read only mode for root filesystem

switch_root:/# mount -o remount,rw /sysroot

Next we need to move the content of sysroot from chroot to normal root environment

switch_root:/# chroot /sysroot

Now you are ready to set your password. You can use below command to set the password. Replace "mynewpassword" with your new password followed by the username i.e. "root"

sh-4.2# echo mynewpassword | passwd --stdin root

Next we need to tell selinux that it needs to do relabelling or else our changes will be gone after reboot, for this create a new autolabel file using below command

sh-4.2# touch /.autorelabel

You are all done now. Next press "Ctrl +D" twice to exit the window followed by a reboot

Next once your node is UP, try to login with your new password

I hope the article was useful.