How to Reset forgotten root password in RHEL/CentOS 7/8

As a Linux Administrator, You must know the importance of root User.

Without root User, you cannot do any kind of configuration in Linux such as creating a partition, Configuring a service, and so on.

That’s why root is also called as Superuser.

Just think about what will happen if you forget the root user’s password.

Forgot root Password

In this guide, I will teach you How to reset forgotten root password in RHEL/CentOS 7/8

I will teach you two methods by which you can reset the root password.

Let’s get started.

Method #1

First, let’s focus on the method one.

Step #1 Reboot your RHEL/CentOS 7/8 Server

To reset the forgotten root password, we have to boot the server in the single-user mode for which we have to edit the Grub menu.

Hence to do this restart the server.

As soon as you restart the server, you will get the following screen.

Here you will get 5 seconds to interrupt the normal boot process. So quickly press any key to interrupt the boot process.

Interrupt the normal boot process

Step #2 Edit the Grub Menu

Now select the highlighted menu and press e to edit the same.

Note: You can select the menu by using up and down arrow on your Keyword.

Edit the Menu

Suggested Read:

Step #3 Make necessary changes

Now look for the line that contains Linux kernel executable that is vmlinuz. Highlighted in the below snapshot.

Linux kernel executable

Move the cursor to the end of that line then next to quiet give a space and type rd.break. Refer to the snapshot below.

Note: You can use up, down, right, and left arrow on your Keyword to move the cursor. To go to the end of the line press CTRL+E.

Type rd.break

After typing as instructed on the screen, press CTRL+X to proceed to the next step.

After following the above steps, we have successfully come into single-user mode and we’ve got bash prompt to type the commands.

Bash Prompt

Step #4 Remount the root Partition

In order to reset the root password, we need to make some changes to the root partition.

By default, the root partition remains mounted in Read-Only mode. Type the following command to check the current status of the root partition.

# mount | grep sysroot
Root User (Read Only Mode)

As you can see in the above snapshot, the root partition is currently in read only mode and we have to set the Read-Write permission to make any changes to the partition.

Type the following command to remount and change the permission.

# mount -o remount,rw /sysroot/

You can run the following command to check whether the permission has been changed.

# mount | grep sysroot
Read and write permission granted

Step #5 Reset the Root Password

Type the following command to get the shell prompt.

# chroot /sysroot

then type passwd command to change the password of the root user. Refer to the snapshot below.

Change the password of root user

Now one final and most important step. We have to relabel the SELinux context.

To do so type the following command.

# touch /.autorelabel
Relabel SELinux context

Now you can exit the shell prompt and restart the server.

Reboot the Server

You will get the following screen when the server is rebooted and it will take some time for the login screen to appear. Have patience.

Screen While Rebooting

Once the login screen appears, You can log in to the server with the help of new root password.

Method #2

This method is similar to the first method but has some changes.

Step #1 Reboot your RHEL/CentOS 7/8 Server

Like the first method, we have to do this through single-user mode.

Hence restart the Server and press any key to interrupt the normal boot process.

Step #2 Edit the Grub Menu

Now press e to edit the Grub menu.

Step #3 Make necessary changes

Now look for the line that contains Linux kernel executable that is vmlinuz. Highlighted in the below snapshot.

Linux Kernel Executable

Now Look for the word ro inside that line. Refer to the snapshot below.

Look for the Word ro

Replace the word ro with rw init=/sysroot/bin/sh. Refer to the snapshot below.

Do necessary changes

After typing as instructed on the screen, press CTRL+X to proceed to the next step.

After following the above steps, we have successfully come into single-user mode and we’ve got bash prompt to type the commands.

Bash Prompt

Step #4 Reset the Root Password

Type the following command to get the shell prompt.

# chroot /sysroot

then type passwd command to change the password of the root user. Refer to the snapshot below.

Change the Password of root User

Now one final and most important step. We have to relabel the SELinux context.

To do so type the following command.

# touch /.autorelabel
relabel SELinux context

Now you can exit the shell prompt and restart the server.

Reboot the Server

Once the login screen appears, You can log in to the server with the help of new root password.

Conclusion

I hope you have learned something from this article.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

If you know any other method to reset forgotten root password then you can share in the comment box.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment

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