Here is the proper step by step guide to reset the forgotten password of CentOS 7 or 8 Linux systems. You can use the same for RedHat 7/8 as well.
If it is not stored in a certain file, it is easy to forget the login password for operations that are rarely used several times a week like logging in to the server.
Thus, What should you do if you forget the root password on the Linux system?
Reinstall the system? Naa, that is not the solution, instead, simply reset it without actually knowing or remembering the password.
The steps are as follows
- To recover or change the forgotten password of CentOS 7.0, first, reboot it. When you will get the grub menu, press the “e” key
- Use the arrow key and go to the end of the screen where you will find the line
rhg quiet
and replace it withrd.break enforcing=0
- After that press
ctrl+x
to drop the current session into the scripting Shell. - Now, mount Filesystem in “read and write” mode-
mount -o remount,rw /sysroot
- Use the command
chroot /sysroot
to access the system. - Enter the command
passwd root
to reset the forgotten CentOS 7 password. - Type
exit
to leave the sysroot. - Make filesystem again read-only-
mount -o remount,ro /sysroot
- Exit session and reboot the system. For that first type –
exit
and thenreboot
- Now, you can log in to your system with the updated root password.
- After login, open the command terminal and run these two commands:
sudo restorecon /etc/shadow
sudo setenforce 1
To get a better idea see the screenshot for the above-given steps:
You can use the same method for CentOS 8 as well, however, here is the detailed article on resetting the CentOS 8 Linux password.