Forgot Root Password?
Linuxcommandshow-to
Losing the root password can be a problem. However, there are two ways to recover the password:
The first one is to use a user account that belongs to 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 process is:
- Restart the computer;
- Enter the Grub Menu;
- Choose the Menu line that will be used for boot;
- Press the "E" key to edit the line;
- Find in the editor the line that starts with "linux" or "linux16";
- At the end of this line, add a space, and append the number 1;
- 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 that use SELinux, it is a bit different:
- Restart the computer;
- Enter the Grub Menu;
- Choose the Menu line that will be used for boot;
- Press the "E" key to edit the line;
- Find in the editor the line that starts with "linux" or "linux16";
- Look for "ro" (readonly) on this line. Delete "ro" and type instead: "rw init=/sysroot/bin/sh"
- 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"
- Type "exit";
- Restart the system normally with the command "reboot";
That was just one piece. The course teaches the whole path — from your first command to exam day.