Tuesday, 13 December 2016

Reset raspberry Pi forgotten password

Background

Well the background is bit embarrassing really :) To secure my raspberry Pi I set a very strong password and guess who forgot after a while :). Well I made my way back into the Pi but it was interesting. So will share this with you as well. Another constraint that I had is that I did not have my external keyboard with me so had to hack my way in. Will should you the same. However I will also list down a neater way in case you do have a keyboard.

NOTE : If you have not changed the password then it might still be raspberry. Try it. 



Without the keybaord

First and foremost you must know where passwords are stored. For Linux OS it is stored in /etc/shadow file. Ofcourse it's a one way hash and there is no way to reverse engineer it. But the file and its contents are important to us.

  • You should have another Linux distro with you. In this create a new user using command
    • useradd testuser
  • Now go to /etc/shadow file and you should see an entry corresponding to testuser. Something like -
    • testuser:!:16406:0:99999:7:::
  • Now remove the ! (or two) from between the first two colons, so it is testuser::16406.... This makes this a passwordless account.
  • Now change the current user to testuser using su command and change the password using passwd command -
    • su testuser
    • passwd
  • One you set the password you can go back to the /etc/shadow file and the line corresponding to testuser should now have a has password hash - something that most probably begins with $6
  • Copy this string and note it down.
  • Now take out your SD card from your Pi. Plug it into another Linux distro. Now on this open up the /etc/shadow file of pi and replace the hash that you might be having with the one you just noted above (You can do this for pi user).
  • And thats it you can use the same password you have for testuser to log into pi now. Try an ssh. You should be good.





Alternate route - the one with USB keyboard 

This is kind of non hacky route assuming you have a USB keyboard.
  • Remove SD card from your Pi. Plug it into some other Linux distro. 
  • In the boot partition you should find a file named cmdline.txt. 



  • Edit it to append 'init=/bin/sh' at the end. Before and after screenshots below -



  •  Save the file. Put the SD card back in Pi and boot up. Now Pi will boot up in single user mode.
  • When you get a cursor type in
    • passwd pi
  • and change the password of user pi. You can then do a normal startup using command -
    •  sync exec /sbin/init 
  • And that's it. Do not forget to remove the appended text that we put in cmdline.txt file. Remove it once you have successfully changed the password and your subsequent boots should be normal again.


Related Links

No comments:

Post a Comment

t> UA-39527780-1 back to top