Recovering your root password in a Red Hat OS base Linux

You may be wondering, why is Anderson talking about Linux now? Well, there are several reasons, the most compelling ones are:

  • Linux is a dominant player and first-class citizen in Microsoft Azure.
  • Microsoft certifications will require proficiency in Linux having Linux Foundation certifications.
  • Knowledge is always good and makes you humble.
  • Last but not least, Linux was one of my first certifications (many, many cycles ago in my previous life, I used to work with Red Hat) and although I don’t use that much nowadays, I do like to use Linux in some scenarios.

I was working on my environment, and I realized that I had no clue which password I had entered during my CentOS installation process and now that I have time to use the VM, I can’t use it. Let’s reset that password. There are a couple of ways to perform this task, and in this blog post, we are covering the easiest and more orthodox approach, which is using the installation media.

The first step is to boot the server/vm with the CentOS (Fedora/Red Hat or any other Red Hat OS base Linux), and then select Troubleshooting.

red hat linux

In the second image, select Rescue a CentOS system and hit enter.

red hat linux

In the next step, the Rescue mode will inform you that will try to find your current Linux installation and mount under /mnt/sysimage. Type 1 and hit enter a couple of times until you get the command prompt (shell).

If everything goes well, the message is that the system was mounted, as depicted in the image below.

Now we need to perform five commands to reset the root password. The first command will change the root directory for the path that we mounted our Linux installation, which means that our root will start from that location (the actual Linux installation), the second command will be used to change the root password, the third command is to instruct SELinux to avoid relabeling the disk. After that, the system will be restarted and the new password can be used.

Chroot /mnt/sysimage
Rm -f /.autorelabel

red hat linux
That’s all there is to it!

Featured image: Red Hat

About The Author

2 thoughts on “Recovering your root password in a Red Hat OS base Linux”

  1. An alternative method, which falls in line with what you need to know for the RHCSA exam :
    Hit “e” when presented with kernel versions
    Append “rd.break” to the Linux16 line
    Press Ctrl+X
    Once at the command line, execute “mount -o remount, rw /sysimage”
    Execute “chroot /sysimage”
    Execute “passwd ”
    Execute “load_policy -i”
    Execute “restorecon /etc/shadow” to restore the SELinux settings for the given file
    Execute “reboot”

  2. I have not seen this method using Rescue mode before. I will have to give it a shot. I have always seen touch /.autorelabel rather than rm -r /.autorelabel.

    It’s unfortunate that the first letter of the commands were helpfully capitalized.

    I have also not seen the technique of loading the policy, and then restoring the setting on /etc/shadow.

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top