May 12, 2008

How I Recovered My Root Password in Debian Etch

Debian Etch wouldn't let me into single user mode without the root password. I booted from the Debian CD and tried the rescue mode... didn't quite understand how to do it. So I had to take an alternate route.

I understand that it is not the safest method, but it works. I booted into Ubuntu on my machine (I have Debian and Ubuntu running on the same machine; you can use a live CD instead) and modified passwd and shadow files on the root partition of Debian. Here are the steps.

  1. Boot into Ubuntu (or any other Linux) on the machine.

  2. Become super-user.

    su

    password: top-secret

  3. Create a mount point.

    mkdir /mnt/deb

  4. Mount the Debian root partition.

    mount /dev/hdXY /mnt/de
    b
    #X and Y identify the Debian root partition.

  5. Open passwd and shadow files with an editor.

    gedit /mnt/deb/etc/passwd /mnt/deb/etc/shadow

  6. Delete the password field for root in /etc/passwd (delete the x in second field)and comment out the root line in /etc/shadow (add a # at the start of the root record).

  7. Now save the two files and reboot into Debian.

  8. Hit Ctrl+Alt+F1 for the first terminal.

  9. Enter root at the login prompt. You will be greeted by a root prompt (#). Type passwd and create a new password for root. Now, return to GUI by hitting Ctrl+Alt+F7.

  10. But this method leaves encrypted password in the world readable passwd file. So, I cut-and-pasted the second field in /etc/passwd over the same field in /etc/shadow; and then restored x back to its place in the passwd file.
It just worked.