How to Reset Forgotten Root Password in Rocky Linux / AlmaLinux

It happens. Yes, sometimes you can lose track of your passwords, including the root password which is critical in performing root privileged tasks. This can happen for a myriad of reasons including staying for a protracted period without logging in as a root user or having a complex root password – in which case you should consider using a password manager to safely store your password.

In case you have forgotten your root password and have nowhere to retrieve it, worry not. If you have physical access to your server, you can reset your forgotten root password with a few simple steps.

[ You might also like: How to Reset Forgotten Root Password in RHEL 8 ]

Join us as we walk you through how to reset a forgotten root password in Rocky Linux / AlmaLinux.

Step 1: Edit the Kernel Parameters

First, reboot the system. On the first entry of the grub menu, press ‘e’ on the keyboard to access the GRUB editor.

Rocky Linux Boot Menu
Rocky Linux Boot Menu

Once you have accessed the shell of the grub editor, scroll down until you get to the line that begins with ‘linux’. Using your arrow forward key, navigate to the end of the line and add the following line to the directive.

rd.break enforcing=0 
Edit Kernel Boot Parameters
Edit Kernel Boot Parameters

To gain access to emergency mode, press Ctrl + x.

Step 2: Reset the Root Password

To reset the root password, we need access to the /sysroot directory with read and write permissions. To do so, mount the /sysroot directory with read and write permissions.

# mount -o rw,remount /sysroot

Take note of the space between the mount and -o, and between remount and /.

Next, change the directory environment to /sysroot.

# chroot /sysroot
Mount Sysroot Directory
Mount Sysroot Directory

To reset the root password, simply type the following command. You will be required to provide a new password and later on, reset it.

# passwd root
Reset Root Password in Rocky Linux
Reset Root Password in Rocky Linux

Step 3: Set the SElinux Context

Next, set the appropriate SELinux context as indicated.

# touch  /.autorelabel

The command creates a hidden file called .autorelabel in the root directory. During the reboot, SELinux detects this file and relabels all the files on the system with appropriate SELinux contexts. This process takes quite some time in systems with huge disk space.

Set SElinux Context
Set SElinux Context

Once you are done, exit the /sysroot environment.

$ exit

Then run the exit command to leave the switch root session and reboot the system.

$  exit

Once the system has rebooted, you can log in and seamlessly switch to the root user.

Conclusion

And there you have it. We have successfully managed to reset the root password in Rocky Linux. The same procedure should work on AlmaLinux.

If you read this far, tweet to the author to show them you care. Tweet a thanks
James Kiarie
This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.