Skip to content

Instantly share code, notes, and snippets.

@jlollis
Last active November 11, 2024 07:40
Show Gist options
  • Save jlollis/89a2e149dac57e50fb4bee1def7331a8 to your computer and use it in GitHub Desktop.
Save jlollis/89a2e149dac57e50fb4bee1def7331a8 to your computer and use it in GitHub Desktop.
Reset Forgotten Raspberry Password

Reset Forgotten Raspberry Pi Password

Remove SD Card

The first step is to turn off the Raspberry so you can remove the memory card without worry.

If the Raspberry Pi is not connected to a screen, the only way to turn it off is to unplug it. Otherwise, you can go through the GUI to turn it off properly, via the menu, before unplugging it.

Once the Raspberry Pi is turned off, you can insert the card into your computer and go to the next step.

Edit the cmdline.txt file

Open the file browser on your computer and go to the memory card. You will find in it a file named cmdline.txt.

This file should contain a line of text that resembles the line below.

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

You must modify this file so that the Raspberry Pi starts on the bin/sh script, which will allow you to change the password of your Raspberry Pi without knowing it!

To do this, simply add the line below to the end of the file and save it.

init=/bin/sh

WARNING: The text must imperatively be on the same line. It is possible that the “block note” editor will display it on the same line while it is not, consider checking with another text editor (eg sublimtext).

Change the password of your Raspberry Pi

Once the file has been modified, you can safely disconnect the memory card from the computer and reinsert it into the Raspberry Pi.

It is in this part that you will need a screen and a keyboard for your Raspberry Pi. So connect your keyboard and your screen to the Raspberry Pi and start there.

A series of command lines should scroll quickly. After a few tens of seconds, the screen will be completely frozen. You should now be able to type text.

We will now mount the root of the system. To do this, run the following command :

mount -o remount, rw /

All you have to do now is to change the password of the Raspberry Pi. To do this, simply type the following command and follow the steps on the screen (note that for security reasons the password may not appear when you type it):

passwd pi

Once the password is changed, run these commands:

sync
exec /sbin/init

The Raspberry will go out. Wait until the screen has completely frozen for at least 30 seconds and unplug it!

Revert to the original cmdline.txt

Remove the SD card from the Raspberry Pi and put it back in your computer.

Edit the cmdline.txt file again the init=/bin/sh part you previously add.

Put the SD card back into your Raspberry Pi and restart it. You should now be able to log in with the new password!

Conclusion

Now you know how to retrieve the lost password of your Raspberry Pi and retrieve access to your system!

Source: https://howtoraspberrypi.com/recover-password-raspberry-pi/

@xChAmeLIoNz
Copy link

I encountered some issues after following this guide. The filesystem got somehow corrupted. Fixed issuing the following commands:

  1. Plug SD Card in your PC using a card reader
  2. Check mount points using df -T or fdisk -l
  3. Unmount bootfs and rootfs
  4. Check and fix the filesystem (considering you're using ext4 and your device is /dev/sdb2)
fsck.ext4 -y /dev/sdb2
  1. Plug SD Card into raspberry. Now it should boot correctly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment