The process of resetting the root on a RedHat-based system is simple. Follow the below steps to recover the root password on your RHEL system.
Step 1: Reboot/Power on the server
We must boot the server in recovery mode and access the grub boot loader menu. To access the menu, reboot the system and you will be presented with a screen that lists the system kernels.
There is a timer on this screen and it will automatically load the kernel, to stop the timer press the up/down arrow keys.
Select the latest kernel and press "E" to edit the parameters before booting.
After pressing "E", you will see the below screen:
Step 2: Edit the boot loader parameters
Find the line with the word "vmlinuz", and navigate to the end of the line (If your keyboard has the "end" key, that is the shortcut for jumping to the end of the line).
Add rd.break
at the end of the line and press ctrl+x
to boot the system.
Once the system is booted, Linux will mount the root filesystem under /sysroot
with read-only permissions.
We need to re-mount the filesystem with read+write permissions with the following command:
mount -o remount,rw /sysroot
Step 3: Change the root directory to /sysroot
The chroot
command (stands for change root) can be used to change the system's root directory to a temporary filesystem, in this case, /sysroot
chroot /sysroot
Now we have /sysroot
as the root filesystem.
Step 4: Change the root password
Just type passwd
and it will prompt you to enter a new password.
Enter a new password and re-type it.
You will see a message that the password has been changed successfully.
Step 5: allow SElinux to rewrite the security context
The last step is to allow SELinux to rewrite the security context for all the files on the next reboot. To do this, we must create an empty hidden file named .autorelabel
under the root filesystem. NOTE: Any file that starts with a dot (.) is a hidden file in Linux.
touch /.autorelabel
Make sure you get the ".autorelabel" spelling correct, otherwise, the password won't work upon the next reboot.
Step 6: Exit out of the chroot shell
Type the exit
command to exit the chroot shell. Type the exit
command again to exit the recovery mode.
You should be able to log in with your new password now.
Conclusion
Resetting the root password of an RHEL virtual machine (VM) or physical server is a frequently encountered challenge. Fortunately, resetting an RHEL 9 machine's root password is straightforward. In this tutorial, we have covered how you can quickly recover the root password of an RHEL 9 system.