If the existing root password is no longer memorize or is forgotten, you can recover the forgotten root password on boot by switching into single user mode.
This guide describes how to recover or reset a forgotten root password on RHEL 8 and CentOS 8 Linux using single user mode.
Linux single user mode, also referred to as maintenance mode, which allows you to recover/repair a system problem by enabling only certain services and minimal functionality.
This same procedure works on Rocky Linux 8 and AlmaLinux 8.
Refer the below guides to recover a forgotten root password on RHEL 6/7 and CentOS 6/7.
Recovering the root password
Follow the below steps to recover forgotten root password.
Step-1: Reboot the system to break the boot sequence
reboot
Step-2: On the 'GRUB 2'
boot screen, press the 'e'
key to interrupt the boot process.
Step-3: This will show you the kernel boot parameters. Go to the line that starts with 'linux'
and press 'Ctrl+e'
to jump to the end of the line. Now, add 'rd.break'
and press 'Ctrl+x'
to start the system with the changed parameters.
Step-4: This will take you to a shell, and you need remount the file system as writable, because by default the file system is mounted as read-only in the ‘/sysroot’ directory.
mount -o remount,rw /sysroot
Step-5: Switch to the ‘chroot’ environment:
chroot /sysroot
Step-6: To reset or change the root password, run:
echo "Welcome123" | passwd --stdin root
Step-7: Enable the SELinux relabeling process on the next system boot:
touch /.autorelabel
Step-8: Exit the chroot environment by executing the following command:
exit
Step-9: Exit the switch_root prompt by running the below command:
exit
Make a note: You may have to wait until the SELinux relabeling process is complete and it may take a long time to run on a large disk. The system will automatically restart when the relabeling process is complete.
When login prompt appears, log in as a root user to verify that the root password has been successfully changed.
Final Thoughts
In this guide, we’ve shown you how to recover a forgotten root password on Red Hat Linux 8, CentOS Linux 8, Rocky Linux 8 and AlmaLinux 8 using single user mode.
If you have any questions or feedback, feel free to comment below.