Recovering a forgotten root password in Linux can be a bit tricky but it is possible. Here’s a general guide on how to do it, but keep in mind that the process may vary depending on your Linux distribution.
Boot into Single User Mode:
- Reboot your Linux system.
- When the GRUB or boot loader menu appears, select the Linux distribution you want to boot into.
- Press the “e” key to edit the boot parameters.
- Find the line starting with “linux” or “linux16” or “linuxefi” (depending on your system) and look for the section that includes “ro” or “ro quiet.” Replace “ro” with “rw” and append “init=/bin/bash” at the end of the line.
- Press “Ctrl + X” or “F10” to boot with the modified parameters.
Remount Root Partition:
The system will boot into single-user mode with a root shell. The root partition will be mounted as read-only, so you need to remount it as read/write with the following command:
mount -o remount,rw /Reset the Root Password:
Now that the root partition is mounted in read/write mode, you can reset the root password by using the passwd command:
passwdYou will be prompted to enter the new root password twice.
Update SELinux Context (if applicable):
If you are using SELinux, you may need to update the security context on the password file. Use the following command to do so:
touch /.autorelabelReboot:
After resetting the root password and updating the SELinux context (if necessary), you can reboot your system:
rebootAfter the reboot, you should be able to log in as root using the newly set password. Keep the new password secure and remember it to avoid having to go through this process again.
Use a live CD or USB
- Boot your system from a live CD or USB.
- Once you are booted into the live environment, open a terminal window.
- Type the following command to mount the root partition of your hard drive:
mount /dev/sda1 /mnt- Change to the
/mntdirectory:
cd /mnt- Change the root password using the
passwdcommand:
passwd root- Once you have changed the root password, reboot your system.
It’s important to note that these steps are intended for use on your own system or a system that you have explicit permission to modify. Attempting to reset the root password on someone else’s system without their consent is illegal and unethical.