Resetting Forgotten Raspberry Pi Password

We’ve all been there; it’s Friday evening, you’re about to go clubbing, and you just set up your raspberry pi to spy on the neighbors dog that keeps pooping in your yard. The next afternoon you wake up in a haze, walk out to get the mail, and step in some dog do-do.

In a fit of rage you hose off your loafers and storm back into the house to grab the raspberry pi camera footage.

Access denied

Oh. Crap.

Thankfully there are two different methods to reset a forgotten password. They both require a separate system for help, but they have different approaches. The first requires a monitor and keyboard hooked up to the raspberry pi, but doesn’t matter what OS the helper system is running. The second one doesn’t require the raspberry pi to have any peripherals, but does requires you have a second raspberry pi or native Linux box.

Don’t forget to try raspberry as the password before anything else, just in case you simply forgot it was the default.

Method 1: Modified boot password reset

This method of password reset requires the raspberry pi to be connected to a monitor and keyboard.

Step 1: Connect micro SD card to PC

Remove the SD card from your raspberry pi, add it to your adapter, and connect it to your PC.

Step 2: Modify cmdline.txt

Travel to the newly mounted USB drive labeled boot. And locate the file called cmdline.txt

Open it in your favorite text editor, and at the very end of the line of the file, add:

 init=/bin/sh

Note that there is a space before the init, separating it from the previous words. However it should still only be a single line of text in the file!

Step 3: Reset the password

Save the text file, safely eject the micro SD card, and stick it back in your raspberry pi. At this point make sure the monitor and keyboard are plugged into the raspberry pi before turning it back on.

You should now be at a command prompt. First we will mount the drive with the ability to make changes to it.

mount -o remount, rw /

Then reset the password.

passwd pi

We then need to sync the new password with the system.

sync
exec /sbin/init

This will boot up the raspberry pi normally. As soon as it’s ready to go, shut it back down!

sudo halt

Step 4: Remove the cmdline.txt modifications

Repeat step 2, just this time remove init=/bin/sh instead of adding it.

Then remove the microSD card safely, insert it back into the raspberry pi, and power it back on!

Method 2: chroot from a linux box

Your linux box could even be another raspberry pi!

Step 0: make yourself root to save typing sudo a lot

sudo su

Step 1: Mount the drive

Connect the microSD card to your linux box or secondary raspberry pi. Then run blkid -o list to discover where the drives are mapped.

# root@raspberrypi:/home/pi# blkid -o list
# device     fs_type label    mount point    UUID
# -------------------------------------------------------------------------
# /dev/mmcblk0p1 vfat    boot     /boot          0C61-73F5
# /dev/mmcblk0p2 ext4    rootfs   /              43f2d0bb...
# /dev/mmcblk0   (in use)
# /dev/sda1  vfat    boot     (not mounted)  0C61-73F5
# /dev/sda2  ext4    rootfs   (not mounted)  43f2d0bb...

Look for the ones that are (not mounted). And set the boot drive as the BOOT variable and rootfs drive as the ROOT variable, then mount them.

ROOT=/dev/sda2
BOOT=/dev/sda1 
mkdir -p /mnt/pi
mount -o rw $ROOT /mnt/pi
mount -o rw $BOOT /mnt/pi/boot

Step 2: Prepare to chroot

Now we are going to do some required setup to make the environment ready for us.

mount --bind /dev /mnt/pi/dev/
mount --bind /sys /mnt/pi/sys/
mount --bind /proc /mnt/pi/proc/
mount --bind /dev/pts /mnt/pi/dev/pts
sed -i 's/^/#CHROOT /g' /mnt/pi/etc/ld.so.preload

If you are using another raspberry pi / arm based device, you can skip this next part. But if you are using a native linux box, we need to be able to communicate with the odd binaries.

apt install qemu qemu-user-static binfmt-support -y
cp /usr/bin/qemu-arm-static /mnt/pi/usr/bin/

Step 3: chroot and change password

Now we are switch into the linux from the card, then change the password for the pi user on it.

chroot /mnt/pi /bin/bash

Notice you should now be in a new root of the new device. root@raspberrypi:/# Then change the password.

passwd pi

That’s it!

Step 4: Cleanup

Now exit the chroot and do some cleanup.

exit

Now you should be back to your old context. So time to undo what you did earlier.

sed -i 's/^#CHROOT //g' /mnt/pi/etc/ld.so.preload
umount /mnt/pi/{dev/pts,dev,sys,proc,boot,}

Now you can remove the SD card and put it back into your raspberry pi!