Occasionally I forget the Raspberry Pi password on my projects. It’s a pain but this guide provides a technique to reset the password and gain access to the system and get it back up and running. You need physical access to the SD card, a separate PC and the ability to connect your Pi to a monitor and keyboard. I used a Windows PC but this should work fine on a Mac or Linux machine.
Step 1 – Pi Password Recovery Requirements
To use this technique you must :
- Have physical access to the Pi and its SD card
- Access to a keyboard and monitor
- Suitable adapters if you are using a Pi Zero
- Have a PC/laptop with a microSD card reader
Step 2 – Grab The SD Card
Power down the Pi and remove the SD card. Insert it into your PC.
Step 3 – Edit cmdline.txt
The boot partition should be visible and contain a file named “cmdline.txt”. Edit this file in a text editor and add the following to the end of the existing text :
If the original content was :
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=04ceb741-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
it should now look like :
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=04ceb741-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait init=/bin/sh
Make sure it is all one line! Save the text file and eject the SD card from the PC.
Step 4 – Reset the Pi Password
Insert the card into the Pi with a monitor and keyboard connected. Power up the Pi. There may be a delay but you should be presented with a cursor.
At the prompt type the following command :
mount -o remount, rw /
If this results in an error message such as :
/bin/sh: 0: can’t access tty; job control turned off [ 21.366191] random: crng init done
simply try the mount command again :
mount -o remount, rw /
Finally you can now attempt to change the password :
You will then be prompted for a new password. Enter it carefully and press the [Return] key. It will now ask you to retype the password.
The password has been changed.
Now type the following commands :
sync exec /sbin/init
The Pi will continue to boot and return you to the normal command line prompt.
Shutdown the Pi and power it off.
Step 5 – Edit cmdline.txt
Remove the SD card from the Pi and using the PC edit the “cmdline.txt” file again and remove the “init=/bin/sh” text you added in Step 2.
Safely eject the SD card from the PC and re-insert into the Pi.
Power up the Pi and your new password should now be active.
Now, don’t forget that password again!