Reset a lost UNIX password

I found myself needing to reuse a box that was sitting in my room for a longer time unused. After I booted it up I realized that I forgot both the username and the password. That's kindof a problem. I tried to boot into recovery mode via the GRUB entry (debian stable), but it greeted me with a message Press Control-D to continue or give root password for maintenance. Well that's a catch 22.

So how do you reset a password on UNIX?

It's as simple as it gets.

Boot up an OS from a CD (it should be similar, or best, the same that you are recovering, and be the same architecture of course). In my case there was debian stable x64 on the box, and I used an Arch install ISO (because I was sure it has the chroot command which is essential for what we are planning to do.)

After it boots up and logs you in (or you log in with the default ISO credientials), find out where is your boxes / partition (or, to be precise, where is /etc/shadow or /etc/passwd)

fdisk -l

Now if you spot the partition which might look like your /, mount it:

mount /dev/sdXY /mnt 

Verify it's the / partition

ls /mnt

You should see the usual directories (/bin, /dev, /root etc.). If not, try another partiton (unmount this one with umount /mnt)

Now chroot into your boxes /

chroot /mnt /bin/bash

It's good to provide the shell to run to chroot (/bin/bash) in case the Live CD uses a different shell (in case of arch it uses zsh which was not installed on the debian box, and chroot gave an error). Now you can reset the password with

passwd

to reset root's password, or

passwd [username]

to reset it for a given user (provided you actually remember your username on the box which was not the case here ;) )

Its worth to mention that at this point, being chrooted to your / partition you can do many other things, like add users, change groups, run visudo, etc., but I think it's more conveinient to do that when booted from the normal system after you recover your root pass

exit
reboot

to exit chroot and reboot the machine, letting you eject the cd while it is rebooting

If everything went well you should be able to log in to your box with the new password