CentOS 8 is widely used by 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.
The first step is to start 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.
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
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 than 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 the 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 root
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 to Switch root
As we have recovered the lost password problem, thus to get back to Switch root and make the system to its normal state.
Simply type:
exit
9. Make filesystem again read-only
As we have manually changed the mode of the default file system, thus make it as it was. Here is the command:
mount -o remount,ro /sysroot
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 the login screen enter the 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 things as per the official website, RHEL recommends, clean the /etc/shadow that stores system passwords in an encrypted format to remove any residue entries.
restorecon /etc/shadow
To enable policies of SELinux, run the below command to enforcing them.
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.