How to Recover root Password on RHEL/CentOS 7

Have you forgot your root password for RHEL 7 and CentOS 7 systems? Want to reset a forgotten root password? If so, don’t worry, we are here to help you.

You can recover a forgotten root password from either single user mode or rescue mode.

Please note that this procedure must be performed from a console session and can’t be accomplished over any SSH remote session, so make sure that you have console access in order to reset the password on RHEL 7 Linux and CentOS 7 Linux.

Navigate to the following articles to reset a forgotten root password on other versions of RHEL and CentOS.

In this tutorial, we’ll show you how to recover the root password on Red Hat Linux 7 and CentOS Linux 7 using single user mode.

Method-1: Resetting root password by booting into Single User Mode

Step-1: Reboot your system and interrupt the 'GRUB 2' boot screen by hitting 'e' key.

Step-2: The kernel boot parameters appear. Find the word 'linux16' and add ‘rd.break’ at the end of the line, then press 'Ctrl+x' to boot the system with the changed parameters.

Step-3: By default, the root filesystem will be mounted in ‘Read only (RO)’ mode in the ‘/sysroot’ directory. You can verify this by running the below command.

# mount | grep root

Step-4: This will not allow any changes to be made to your system until the root file system is mounted in ‘Read and write (RW)’ mode in the ‘/ sysroot’ directory. To do so, use the following command.

# mount -o remount,rw /sysroot

Step-5: Your file system is currently mounted as a temporary partition, which is why ‘switch_root:/#’ is displayed on the shell prompt.

Run the following command to switch to the ‘chroot jail’ environment.

# chroot /sysroot

Step-6: You can now reset the root password using the passwd command as shown below:

# echo "CentOS7$#123" | passwd --stdin root

Step-7: By default CentOS 7/RHEL 7 Linux uses SELinux in enforcing mode so, you must perform the SELinux relabeling process for all the files by creating the following hidden file, which will be done automatically at the next boot.

It allow us to fix the context of the /etc/shadow file.

# touch /.autorelabel

Step-8: Issue 'exit' command twice to exit from the chroot environment and reboot the system.

Step-9: Now you can login to your system with your new root password.

Final Thoughts

In this guide, we’ve shown you how to reset or recover a forgotten root password on Red Hat Linux 7 and CentOS Linux 7 using single user mode with ‘rd.break’ parameters.

If you have any questions or feedback, feel free to comment below.

About Prakash Subramanian

Prakash Subramanian is a Linux lover and has 3.5+ years of experience in linux server administration with major Linux distribution such as (RHEL, CentOS, Ubuntu). He is currently working as a Senior L2 Linux Server administrator.

View all posts by Prakash Subramanian

Leave a Reply

Your email address will not be published. Required fields are marked *