If you need to log in as a root user on your Rocky Linux system after some time and can't remember its password, the only method to recover is to reset it.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux system queries and configuration.
In this context, we shall look into the procedure of resetting the root password on Rocky Linux 8 from the GRUB menu.
The GRUB menu is a boot loader and software that loads and transfers control to an operating system, such as Linux, when a computer is turned on.
1. Reboot Rocky Linux system
First, restart Rocky Linux 8 and press 'e' on the keyboard as soon as the boot menu appears. This will display the Grub editing interface.
2. Edit Boot Grub Configuration
Next, we need to edit the Grub boot configuration interface. Use arrow keys to navigate the interface. Locate the line beginning with linux and scroll to the end of the line using the arrow forward key.
Then append the line rd.break enforcing=0 .
Thereafter, initiate the booting process using Ctrl+X and start the Emergency mode scripting shell.
3. Mount Filesystem in "read and write" mode
In order to reset the root password on Rocky Linux, we first need read and write access to the sysroot filesystem. Run the following command to mount the filesystem in 'read' and 'write' mode:
$ mount -o remount,rw /sysroot
Next, navigate the directory to the sysroot directory using the command below:
$ chroot /sysroot
4. Reset password
Finally, everything is in place, and we can now change the password of the available root user. Run the below command:
$ passwd root
The system will prompt you for a new password. Ensure to set a strong password with at least 8 characters, otherwise, you will be warned that the password is weak. If everything is correct, the password will be updated successfully. Press the Enter key to proceed. Next, exit the Sysroot directory using the exit command.
To reset the root password, we changed the file system rights to read and write. We need to set it back to read only. Therefore, execute the command:
$ mount -o remount,ro /sysroot
5. Exit session and reboot
Next, exit the switch root session and let your system restart with the below command:
You can now log in to your root user with the new password.
6. Set SELinux enforcing modes
According to the Redhat official guide of resetting system passwords, it is recommended to delete any residue entries from the /etc/shadow file that stores system passwords. So, once you have logged in execute the following command:
$ restorecon /etc/shadow
Next, enable policies of SELinux with the below command:
$ setenforce 1
This article covers the process of Resetting the root password on Rocky Linux. In fact, If you are locked out of your AlmaLinux instance and have lost access to the root account, you need to boot into single-user mode and reset the root password.