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 :
init=/bin/sh
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 :
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.
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 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!
37 Comments
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.
/etc/shadow does not exist on my Wheezy Distro! And I always have the
current version…
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
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”.
Thanks Ricardo !
Thanks man!
sudo passwd pi did the trick for me, as I usually don’t need to type passwords. Thanks!
Or you could simply log in as a normal user, type the following in the shell:
sudo passwd root
It worked on my Pi.
The problem is if you’ve forgotten the “pi” user password you can’t log in!
Ricardo,thank you! Matt thank you!
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”
The command to run prior to changing the password is: mount -rw -o remount /
Thank You!
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!
I had this issue, too – worked perfect. Thanks (ps I entered the line at the # prompt)…
I use “mount -o remount,rw /” in lieu of “mount -rw -o remount /” in step 3 and it work fine for me. Thanks!
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 /
This doesn’t work.
It’s saying “only root can do that”.
Thanks.
Thanks for the help! Worked a treat, after I edited the cmdline in Notepad++. Knowing my luck I better save this in my favourites.
Thanks a lot for this post!
I’ve tried this, and when I get past the “mount” stage, it tells me that there was an “error while loading shared libraries: /lib/arm- linux-gnueabihf/libsepol.so.1:invalid ELF header”. Same error if I try exec /sbin/init, then a “Kernel panic”
Any ideas?
I have a Raspberry Pi B+ that I am pretty sure is running Raspian 4.0. I cannot find a file named “cmdline.txt”. Anyone know how to solve this problem?
Google “NOOBS”. I’m another noobie, so can’t cite specifics off the top of my head, but it has to do with the “NOOBS” install method hiding the part of the SD card that contains cmdline.txt.
I’m in the same predicament right now and am trying to resolve it by tapping Shift while booting, which throws me into a “Recovery Mode” where I can edit the hidden cmdline.txt…. only problem is that the recommended appending of init=bin/sh has had the effect of hanging the boot process instead of getting me to a PW reset screen.
This help me now, when i forgot the password for a pi i am setting up for mom with Ubuntu Mate 🙂
So I have forgotten both my login and password. I was able to successfully change the password using this method, but am still stuck not knowing my login.
How do I reset my login details? If I was able to reset my password, I don’t see why I couldn’t reset the login username too
Try: cat /etc/passwd
This should print out all usernames, each on one line, together with other user’s data. Usernames are always the strings before the first colon on line.
Eg. in:
pi:x:1001:1001:…
username is “pi”.
(You can use: cut /etc/passwd -d : -f 1
to print only usernames.)
Most of the lines represents “virtual system” users, but Your username is also there (it will be probably near the end of the file).
Hi,
In my case with latest release this did’t work: “mount -rw -o remount /”
But when I used this: “mount -o remount, rw /” all went fine 🙂
BR,
Thanks, I’ve updated the guide. I needed to follow this guide myself as I’ve just forgotten the Pi password on my pool monitoring system!
Hmm, I had to remove the comma between remount and rw statement in order for it to work.
Great tidbit of information MATT. Thanks! It just saved my bacon. 😀
If the remount fail and complain about PARTUUID, maybe your fstab is wrong, so use this:
mount -o remount,rw /dev/mmcblk0p2 /
It worked for me.
If you have installed Raspbian via NOOBS, you can edit cmdline.txt from the boot menu. Just hold the SHIFT key at startup.
One quick tweak would make this a little quicker: After the password is reset, you can just edit cmdline.txt from the command prompt (sudo pico /boot/cmdline.txt) instead of removing the SD card and editing it from another machine.
Even years later, this is the way. Thanks!