How to Reset / Recover Lost Root Password [RedHat Linux 7]

Have you lost your root password. No worries. In today’s article we will see how to reset a forgotten root password in RedHat Linux version 7.

Harsha Nanayakkara
3 min readAug 21, 2022

There are actually 02 ways to accomplish this task.

  1. Boot the system using a Live CD, mount the root file system from there and edit the /etc/shadow file.
  2. Reset the root password without any external media.

We are going to look at the 2nd method. This method could be used in other RedHat based distributions such as CentOS and Fedora. Okay let’s start.

Step 01: Reboot the system. As soon as you see the boot loader with the selection screen, quickly tap any key to interrupt the countdown. Use the UP or DOWN arrows to select the kernel you want to boot into.

Step 02: Then, press e to edit the selected entry. Move the cursor using DOWN arrow until you find a line starts with linux16.

Step 03: Append rd.break to the end of the line.

Step 04: Press Ctrl+x to boot with the changes. At this point, the system will enter into emergency mode. A root shell is presented with the root file system for the actual system mounted read-only on /sysroot .

Step 05: Remount the root partition as read-write mode. Note: I have omitted the top part of the screen to focus more on the command.

Step 06: Switch to chroot . This will change you into the sysroot (/) directory, and make that your path for executing commands.

Step 07: Now we can simply change the password for the root user using the passwd command. You may ignore the BAD PASSWORDmessage here because this is only for demo purposes. Please make sure to enter a more complex password.

Step 08: We have successfully reset the root password by now. However, before you reboot we need to inform SELinux about this change using the following command, so that it will know about the password reset and loads the change in the next reboot.

# touch /.autorelabel

Note: as this will relabel the whole system it might take a while depending on the size of the filesystem and speed of the machine.

Step 09: Enter exit twice. The first will exit the chroot and the second will exit the initramfs debug shell. The system will perform a full SELinux relabel and reboot.

Step 10: That’s it. Log in with the newly changed root password and chill.

In conclusion, I sincerely hope this article will help you at some point when you encounter a similar issue.

Thank you for reading and stay safe!

--

--

Harsha Nanayakkara

An enthusiastic autodidact who is passionate to gain and freely share knowledge. I would really appreciate your feedback and support!