Reset lost root password CentOS 7 and RED HAT 7

In this how to we will look into resetting lost root password on CentOS 7. This will also work on RED HAT 7. We will need to boot into special shell in order to do this.

1. Reboot server or virtual machine and when GRUB menu appears press any key to interrupt boot process.
pic 1
2. Highlight the default kernel entry and press e to enter the edit mode.

3. Scroll down to below string and ad init=/sysroot/bin/sh at the end of the line

22

4. Press Ctrl+x when completed and it will boot us to special shell. System will mount root file system read-only on the /sysroot directory. Now we will need to make /sysroot as read-only directory.

chroot /sysroot

5. Now lets remount the root file system in read/write mode with the mount command

mount -o remount,rw /

6. Now lets run passwd command to reset root passwd

passwd

7. Lets now create hidden file called .autolabel at the root of the directory. This will instruct system to perform relabling for SELinux during next boot.

touch /.autorelabel

8. Now exit and reboot

exit
reboot

At this point we should be able to login with our new password.