Site icon Edumotivation

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

Reset forgotten root password

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.

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.

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.

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.

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.

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.

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

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

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.

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

To do so type the following command.

# touch /.autorelabel

Now you can exit the shell prompt and restart 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.

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.

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

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

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.

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.

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

To do so type the following command.

# touch /.autorelabel

Now you can exit the shell prompt and restart 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.

Exit mobile version