If you have forgotten the root password and are using the CentOS operating system, you can follow the steps below to reset the password:
- Restart the System:
As the first step, reboot or start the system.
- Access the GRUB Boot Screen:
You’ll need to access the GRUB boot screen during the normal boot process of CentOS. If it doesn’t automatically switch to the GRUB screen, press any key during the system’s startup. Then, press “e” to enter editing mode.
- Edit the Kernel Line:
In the editing mode, locate the “kernel” line, which usually starts with “linux” or “linux16.” Once you find this line, change “ro” (read-only) to “rw” (read-write). After rw add init=/sysroot/bin/sh
Note: Values such as “vmlinuz-4.18.0-80.el8.x86_64” and “UUID=abcdef” may vary depending on your system configuration.
- Save Edits and Exit:
After making the edits, exit the editing mode by pressing “Ctrl+X” or “F10” on your keyboard. This will boot the system in single-user mode.
- Log in as the Root User:
When the system starts in single-user mode, you can log in as the root user without requiring a password. Inside shell, type chroot /sysroot and press enter.
- Change the Password:
To set a new root password, use the following command:
passwd
When you run this command, enter the new password and confirm it.
- Review SELinux Status (Optional):
If SELinux is active on your CentOS system, you may need to adjust the SELinux context after changing the password.
- Restart the System:
After successfully changing the password, reboot the system using the `reboot` command.
You can now access the CentOS system with your new root password. We recommend that you choose a safe and complex password and keep it safe.