In any UNIX or Linux system, the root account is the top in the hierarchy. It holds the utmost power over the system. For any system change, the root has to grant permission. The root user can also override any user's permission and read or write data. Because the root account holds so much power, it is important to protect it from unwanted access.In the case of Ubuntu, the root account is disabled by default. However, you can still perform system changes. To do so, you need to know the root password. The problem arises when you have forgotten the root password.
Check out how to reset the forgotten root password in Ubuntu.
Reset forgotten root password
Although it is difficult, it is still possible to reset the forgotten root password in Ubuntu. There are a couple of methods to do so. In short, the process is to get access to the root shell and change the password from there.
Method 1: Root shell from boot
When you are booting into Ubuntu, press, and hold Shift. This should open up the GRUB menu. In case there is multi-boot configured, there is probably no need to press the Shift key.
Select “Ubuntu” and press “e”. This will open up the grub parameters to edit.
Now, use the arrow keys and scroll down to the line that starts with “linux /boot/vmlinuz”.
From that line, replace “ro quiet splash $vt_handoff” with “rw init=/bin/bash”.
Press “Ctrl + X” or F10. This will reboot the system with the tweaks we just made. It will start the system and directly land you on the root shell. It is just a one-time thing, so next time the system boots, it will boot normally into the operating system.
Run the following command to confirm if the root filesystem mounted successfully.
$ mount | grep -w /
Now, run the passwd command to change the root password.
Once the password is changed, you will see the success message.
The task is finished. Run the next command to reboot the system.
$ exec /sbin/initMethod 2: Change password from Ubuntu live session
For this method to work, we need to have bootable media made from Ubuntu ISO. Download Ubuntu ISO. In my case, I'm running Ubuntu 20.04.1 LTS.
Make a bootable USB flash drive. Check out how to make a bootable USB drive.
Boot into the Ubuntu bootable media. When prompted, select “Try Ubuntu”. This will start the Ubuntu live session.
Once you are in the live session, fire up a terminal.
We need to identify the root filesystem partition. The following command will list all the partitions in the system. The key point of the output is the last section, where there is a short table of partitions.
$ sudo fdisk -l
From the table, we have determined that the boot partition is /dev/sda5. Now, mount it by running this command.
Run the next command to cross-mount things from the live install.
Now, become the root of the system using chroot.
Finally, run the passwd command to change the root password.
$ passwd
Once the process is finished, reboot the system.
Final thoughts
As demonstrated, there is no need to fear when you have forgotten the root password in Ubuntu. Here, two methods are demonstrated. Follow the one that you feel comfortable with.
Now, these methods also open up the question of security. In case there is someone else having physical access to the computer. These methods could be used against you. To prevent this, you should configure a password to access the BIOS and boot menu. All modern computers have this feature built-in. However, this is a password that you MUST NOT FORGET.
Happy computing!