In a Linux computer, both the regular users and superusers (also known as the root users) can employ passwords to sign in to the computer. Therefore, if a regular user of any Linux device forgets the password, the superuser can reset the regular user account’s password via the terminal. But what if the superuser or root user also forgets their password? In this article, we will show you two step-by-step ways to recover the Linux root password. These methods are valid for most Linux distros, including the most popular Ubuntu distro. Without further ado, let’s dive in.
Recover the Forgotten Linux Root Password Using the Grub Menu
Using the Grub menu is one of the easiest ways to restore the root password for a Linux computer. Here’s how it works:
- First, you need to enter into the “GNU GRUB” menu of your Linux device. There are two ways to enter the Grub menu, which are:
- Restart your Linux device and start tapping the “Shift” key on your computer continuously. To be more precise, as soon as you see the “BIOS/UEFI” screen after restarting your computer, you must keep tapping the “Shift” key, and you should get into the “GRUB” menu.
- You can also enter the “GRUB” menu by editing the “/etc/default/grub” file. Here’s what you need to do:
- Open the “/etc/default/grub” file and find the “GRUB_TIMEOUT” line.
- Now, edit the line, so it would look like this: GRUB_TIMEOUT=10
- Next, go to the “grub.cfg” file and execute this command line: sudo update-grub
- After that, you just need to wait for 10 seconds, and you will start entering the “GRUB” menu.
- Once you gain access to the “GRUB” menu, press the “down-arrow” key on your keyboard to select the “*Advanced options for GNU/Linux,” before pressing the “Enter” key.
- Next, press “e” to enable permission to edit the commands on your screen.
- After that, find the line that starts with “Linux,” find the “ro” text, and replace it with the “rw” text. At the end of that line, add this: init=/bin/bash
- Now, press the “F10” key, and it will open a new prompt screen.
- Next, type and execute to mount your root file system in read-write mode: mount -n -o remount,rw /
- Once that is complete, type and execute the “passwd root” command, and then set your new password in the command screen.
- After you’re done, type and execute this command: exec /sbin/init
- Now, exit the window and restart your computer. The new password will be set, and you can regain access to the machine with this new Linux root password.
Use Live CD to Restore Root Password for Linux
Do you have an Ubuntu live CD or know how to make one? If your answer is yes, then you can use the live CD to recover the Linux root password following these simple steps:
- First, insert the bootable Ubuntu CD in your computer and boot your system using the “Removable drive” option instead of HDD.
- After your computer boots, click on “Try Ubuntu” on the “Welcome” screen.
- Now, open the terminal, type the “sudo su” command and execute it.
- Next, to find the hard disk partition location, execute the “fdisk -1” command. In most cases, the location will be “/dev/sda1.” However, it might vary depending on how you have partitioned your hard disk drive.
- Now, to mount the hard disk partition, execute these commands consecutively: mkdir /mnt/recover
mount /dev/sda1 /mnt/recover - After that, you have to perform “chrooting” or pretend to be a regular Linux system by executing this command: chroot /mnt/recover
- Once it’s done, execute this command to rewrite a new Linux root password: passwd root
- After you set your new password, you need to exit the “chroot shell” using this command: exit
- Next, execute the “umount /mnt/recover” command to unmount the root partition.
- Now, you need to exit the root using this command once again: exit
- Remove the live Ubuntu CD from your computer and reboot it.
Congratulations, the process to retrieve the Linux root password is now complete.