How to recover root user Password in Red Hat Linux
We all know that the root user has unlimited power in a Linux-based Operating System. But what would happen if you forgot the root password of your system? It's a scary thought, isn't it?
Well, it was scary till today, because today I came up with the ultimate article on resetting your root password in RedHat and Centos based systems. So if you forgot your root password then no need to worry. Take a chill pill and follow this article.
Note:- In this article, I am using RedHat Linux 8.4 VM running on Virtual Box 6.1 and my base OS is Windows 10.
First thing first - Take a snapshot of your current VM to prevent any data losses if anything wrong happens ๐ฅ.
To take a snapshot, click on the three dots, that is appearing on the right side of your VM name. and then choose the Take option as shown below:
Step-1 (Shutdown your Linux VM)
First, you need to shut down your virtual machine if it is still running.
Step-2 (Start your Linux VM in emergency mode)
Now you need to start your Redhat Linux system in emergency mode.
don't know how to do it, don't worry, follow below steps :
Click on the Start button
Now press "e" when you are in this window
After pressing e, you will be in the below window
here on line number 4, you can see the second last word is ro, this ro stands for readonly. You need to change this with the following:
rw init=/sysroot/bin/sh
and then press ctrl+x to start.
Now you will be in emergency mode and the following window will appear:
Step-3 (reset root password)
Here you have to execute the following commands:
chroot /sysroot (for changing root dir)
passwd root (for resetting passwd)
touch /.autorelabel (for auto relabeling of SELinux contexts)
exit (exit from sysroot)
reboot (to reboot/restart the machine)
Step-4 (Restart your Linux Machine)
When you press enter to reboot
command, it will restart your VM. While restarting, you may see this window
It is common and no need to panic. This is the process for SELinux auto relabeling and it may take a few minutes based on your hardware.
After Successful restart, you will see your normal login page like this:
Now you should be able to login with your root account with the new password that you created.
Congratulations ๐ฅณ๐. You saved yourself from a big disaster and recovered root access to your system.
Conclusion
We learned how to boot your system into emergency mode
We learned to reset the root password.
We saw auto-relabeling SELinux contexts.
And finally, gained root access to our RHEL System.
Subscribe to my newsletter
Read articles from Adil Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Adil Ansari
Adil Ansari
I'm a tech aficionado, and cloud & DevOps engineer. I love solving problems and collaborating with teams. #TechEnthusiast #CloudOps #AWS #DevOps #TeamPlayer Connect with me to stay updated with my latest articles and insights on AWS, Linux, and DevOps!