How To Reset A Forgotten Raspberry Pi Password

20

I’ve got a Raspberry Pi running in my garage as part of a security system. It’s been running for so many months that I forgot the password so I was unable to access it via SSH.

Luckily there is was a technique that I could use to reset the password 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 – Grab The SD Card

Power down the Pi and remove the SD card. Insert it into your PC.

Step 2 – 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 :

init=/bin/sh

If the original content was :

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

it should now look like :

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait init=/bin/sh

Make sure it is all one line! Save the text file and eject the SD card from the PC.

Step 3 – Reset the Password

Insert the card into a Pi that is connected to a monitor and keyboard. Power up the Pi. There may be a delay but you should be presented with a flashing cursor.

At the prompt type the following command :

mount -rw -o remount /

then

passwd pi

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.

Reset Lost 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.

sudo halt

Step 4 – Edit cmdline.txt

Using the PC edit the “cmdline.txt” file again and remove the “init=/bin/sh” text you added in Step 2.

You can now return the SD card to your Pi, reboot and use the new password.

 

Share.

20 Comments

  1. Or you could just edit the /etc/shadow file and remove the encrypted password string and then you can login without needing to enter a password. Then you use passwd to set it again.

  2. My Pi starts up as normal after adding the text to the existing line in cmdline.txt file. I never get the chance to type “passwd pi” – only the usual login is displayed. Is there another way?

    • Not that I know of. The only thing I can suggest is double checking the config.txt file and making sure every character is correct and that it only takes up one line.

    • I had the same problem. Even bought a new keyboard cause I read that it might not be picking up on my wireless one. In the end, init=/bin/sh was on another line. Try opening the file with leafpad. That’s how I discovered it was formatted wrong. Good Luck

  3. For those who like Henrik that its Pi boots to normal login prompt, I assume you’re editing in Windows and are using Notepad. That’s the problem.

    Notepad will place the new text in a new line although it will seem it’s in the same. That’s because Notepad needs a different end-of-line than UNIX-like systems (Windows text files use a CR+LF sequence, UNIX-like text files only use LF) and will show everything in only one line in a text file from a UNIX-like system.

    I haven’t tried to delete the last space or even the last character of the line and rewrite it again and then add ‘init=/bin/sh’. Don’t know if it will work.

    What does work is install Notepad++ and it will keep the correct formatting and character sequence of the original file. This will work!!

    • Thank Ricardo. I use Notepad++ for all my text file editing. In the bottom right hand corner it states what end-of-line convention it uses. Usually “UNIX” or “Windows”. For all my Pi stuff I make sure it is “UNIX”. If it isn’t, it can be changed using “Edit” > “EOL Conversion” > “UNIX/OSX Format”.

  4. Other solution : make a new SD card or USB Key with raspbian, boot on your favorit Linux and copy shadow from USB Key to your SD Card Pi.
    cp /mnt/USBKEY/etc/shadow* /mnt/SDcardPi/etc/

    It’s works now with default password ” raspberry”

  5. I was getting this type of error on my first attempt:

    passwd: Authentication token manipulation error
    passwd: password unchanged

    I did a bit of searching on the web and found that you have to re-mount the root directory where the shadow file is in order to be able to change the password. You can accomplish that by entering the following command:

    mount -o remount,rw /

    Thanks for writing this article!

  6. Michael Grant on

    I was getting this:

    # passwd pi
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: Authentication token manipulation error
    passwd: password unchanged

    I had to remount / read-write like this before I could change the password:

    mount -rw -o remount /

  7. Thanks for the help! Worked a treat, after I edited the cmdline in Notepad++. Knowing my luck I better save this in my favourites.

Leave A Reply