Lost Root Password?
Losing the root password can be a problem. However, there are two ways to recover the password:
The first is to use a user account that is part of the “Sudoers” group and can change the root password using the sudo command:
$ sudo su - # passwd
If it is not possible to log into the system with a privileged account, it is still possible to restart the computer and enter emergency mode to change the root password.
On Debian/Ubuntu systems, the step by step is:
- Restart the computer;
- Enter the Grub Menu;
- Choose the Menu line that will be used for booting; Press the
- “E” key to edit the line; Search the editor for
- the line that begins with “Linux” or “linux16”;
- At the end of this line, give a space, and add the number 1; 9. Press Ctrl-X to restart the system;
- Use the passwd command to change the root password;
- Restart the system normally;
On systems like CentOS and Fedora, especially those using SELinux, it’s a bit different:
- Restart the computer;
- Enter the Grub Menu;
- Choose the Menu line that will be used for booting; Press the
- “E” key to edit the line; Search the editor for
- the line that begins with “Linux” or “linux16”;
- Search for this line by “ro” (readonly). Delete the “ ro” and type instead: “rw init=/sysroot/bin/sh” 11. Press Ctrl-X to restart the system;
- Type “chroot /sysroot”;
- Use the passwd command to change the root password;
- If the system uses SELinux, force it to relabel the root partition with the command ” touch /.autorelabel” 17. Type “exit “;
- Restart the system normally with the” reboot “command;