Reset CentOS 8/RHEL 8 forgot root password

CentOS 8 is widely used by the users to set up various server environments, thus a strong password is needed. However, somehow, if you forget the root password then the only way to get access to it is by resetting.

In this tutorial, we will show you the steps to reset the CentOS 8 Linux root password that has been forgotten due to any reason. So let’s start the steps to retrieve or change the password?

What we learn here:

  • how to change the root password, if lost?
  • Recovery of the lost root password
  • root password no longer works, how to reset it?

Steps to reset CentOS 8 root user password

This tutorial is also applicable to previous versions of this Linux operating system that are CentOS 7 and CentOS 6. Moreover, we are not using SELinux autorelabel which sometimes causes problems.

1. Go to the boot menu

The first step is to start the CentOS 8 and when the boot menu appears, press the “e” key on your keyboard. This will open the Grub 2 boot editing interface.  To get an idea see the below-given screenshot.

Drop to CetnOS boot Switch root shell

2. Drop CentOS 8 boot to Shell

In the Grub boot configuration, use the arrow key and  replace the line rhg quiet with  rd.break enforcing=0.

You can see how we have done, in the same way, add the mentioned string.

recover password RedHat 8

3. Start the Emergency mode scripting Shell

After the addition of the syntax given in the previous step, boot the system by pressing Ctrl+X. This will boot CentOS 8 in the shell rather initiating the system init daemon.

4. Mount Filesystem in “read and write” mode

To reset the root password of this Linux, the first thing we need is the read and write access to the sysroot filesystem. That is by default available only in read-only mode.  Use the below command:

mount -o remount,rw / sysroot

Note: There is a space between the mount and -o and there is also a space between rw and /, which cannot be omitted), and press Enter key on the keyboard to execute it.

5. Chroot /Sysroot

Chroot is the command that will change the current root directory running process. So, that we can access the file of the Sysroot in a confined environment.

 chroot /sysroot

6. Reset password

Finally, everything is set and the command we need to reset the available root user password.

passwd

7. Enter New Password

Now, enter the new password at the blinking position of the cursor (the password is not displayed when you enter it, just like there is no response). But remember that the password you entered is correct.  Yes, the password should be at least 8 digits, otherwise, it will give a warning that the password is weak. Once you are done press Enter on the keyboard after completion.

8. Return back to Switch root

As we have recovered the lost password problem, thus not get back to Switch root and make the system to its normal state.

Simply type:

exit

9. Make sysroot again read-only

As we have manually change the mode of the default file system, thus make it as it was. Here is the command:

mount -o remount,ro /sysroot

Reset root password of CentOS 8 safely

10. Exit Session and reboot

To safely reboot the system and leave everything intact exit the current Switch root session and let the system reboot.

exit

11. Login root with a new password

Next, on login screen enter username root and password that you have set and click “Login” when finished.

12. Clean up and set SELinux enforcing mode

Just to convey a proper way of doing the things as official website RHEL recommends, clean the /etc/shadow that stores system password in an encrypted format to remove any residue entries.

restorecon /etc/shadow

To enable polices of SELinux, run below command to enforcing it.

setenforce 1

Verdict: RedHat 8 password change

So, this was the safest way we can use to recover our system that root user password we forgot on RHEL or CentOS.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.