Recover root password on CentOS
The easiest way to reset the root password on CentOS is to log in as the user who is a member of the wheel group and then run the sudo passwd root command. The members of the wheel group have sudo privileges, meaning that they can execute as if they were the root user. Here is an example:
[tunapeyo@it-courses ~]$ sudo passwd root [sudo] password for tunapeyo: Changing password for user root. New password: Retype new password: passwd: all authentication tokens updated successfully.
Notice how the system asked me for the password of the tunapeyo user – since tunapeyo is a member of the wheel group, it can execute administrative commands by providing its own password.
The second way to change the root password works only if you have direct access to the machine, serial terminal access, or, if CentOS is installed in a virtual machine, access to the virtual machine console. Here are the steps:
1. Restart the operating system.
2. Once the boot process starts, press ESC to bring up the GRUB boot prompt. Once inside the prompt, press e to edit the first boot option:
3. Find the line starting with linux16 and change ro to rw init=/sysroot/bin/sh:
Before:
After:
4. Press CTRL-X to start your system with new parameters.
5. Once the system starts, enter the chroot /sysroot command to change the file system’s root:
:/# chroot /sysroot :/#
6. Run the passwd command and change the root password:
:/# passwd Changing password for user root. New password: Retype new password: passwd: all authentication tokens updated successfully.
7. Reboot the system using the reboot -f command and try to log in as root using the new password.