If a normal user forgets his password it can be reset by root. But if root forgets it’s password it is not that easy. But there is a way. You need to boot your system into recovery mode to circumvent the login process.
This is only possible if you physical access to the system (or the console of a VM) and may reboot the system.
Enter Grub Menu
While the system boots you need to enter the grub menu.
Select the advanced options
and move to one of the recovery kernels.
Now press e
to edit the grub entry. This will give you an editor window.
In there search for the line starting with linux
. These are the boot-parameters for the Kernel.
At the end of this line add the option init=/bin/bash
. The line then should look something like this.
linux ... init=/bin/bash
This will instruct the kernel to start a bash as the first process.
Normally this would be you systemd process which then start all over services. In our case we don’t want that. Keep in mind that some functions of the bash might not work since no services are running in the background.
Remount Filesystem
Since we are using a recovery kernel all our filesystems are mounted as read-only.
You can check this by running the mount
command. It will list all mounted filesystems and at the bottom you will find you /dev/sda1
. In the options you will see ro
for read-only.
Now remount the filesystem as read-write
.
mount -o remount rw /
If you run mount
again, you will see that it is now rw
.
This enables us to make changes to the system.
Reset Password
Now we can use the passwd
command as usual to change our password.
passwd
Reboot the System
Now we need to reboot the system.
You try to run reboot
or shutdown
but this will probably not work since both commands utilize systemd under the hood and since we specified bash as our init-process there is no systemd running.
The easiest way would be to reset the system with a hardware reset. Since we have no processes running in the background this shouldn’t be a problem.
After the reboot you can log in with your new password.