If you forgot the root password for your Debian system, do not panic. There is an easy way to reset the password. Linux provides a special mode called "recovery mode" or "single user mode," whereby a Linux host boots into a single user environment where you get to access shared resources such as file systems exclusively. The recovery mode is often used when you need to perform file system maintenance and recovery, or conduct security vulnerability auditing in an isolated environment. You can also reset the root password in the recovery mode.
In order to boot into the recovery mode on Debian or Ubuntu, hold down SHIFT key after powering on your Linux machine. You will then be presented with GRUB menu. Select "recovery menu" option by pressing down arrow key. Do not press ENTER at this point, but rather press "e" key to enter edit mode.
You will see "GRUB" edit screen where you can edit GRUB boot parameters. Move your cursor to the line starting with "linux /boot/vmlinuz-.......", and go to the end of that line. Append "init /bin/bash" right there, as shown below.
Then press Ctrl+X to proceed. After all subsequent booting sequence is over, you will finally get a root prompt.
In the recovery mode, the root partition is mounted as read-only by default, and so you cannot change the root password as is. In order to be able to reset the root password, you need to re-mount the root partition as read-write.
First, find out what the root partition of your system is, by using "fdisk -l" command.
As shown in the screenshot above, the partition marked with "*" under "Boot" column is the root partition of your system (e.g., /dev/sda1).
Remount the root partition as read-write, and finally reset the password, using passwd command.
Subscribe to Xmodulo
You can have daily Linux tutorials and FAQs delivered to your email inbox. Simply enter your email address below to subscribe to our mailing list. You will receive hands-on guides and carefully written tutorials related to Linux, everything for free.