Resetting a Raspberry Pi root password

Mon, Sep 25, 2023 2-minute read

Quick tip, because I cannot recall how many times I’ve had to do this.

Despite being fairly rigorous about using a password manager whenever I create a password for something, I haven’t been able to break the bad habit of whenever I (re-)provision a Raspberry Pi for something, whilst I do invariably remember to change the default username and password but I also invariably forget to write down what I changed them to. Sub-ideal.

It’s reasonably easy to just reflash and start over, but sometimes you really don’t want to have to do that.

So here is the simplest method I use to regaining access to your Pi, which assumes:

  1. You have physical access to it (and its SD card)
  2. You have some other computer you can use

In my case I’m using macOS.

It’s hammer time

  1. Grab the SD card out of the Pi, and shove it in to your spare computer. In macOS (FressBSD) it will mount, and in the root of the card you will see a file called cmdline.txt. This should be the same irrespective of your OS.
  2. Open cmdline.txt in a text editor, and append init=/bin/sh to the end of that line. It will look a bit like this:
console=serial0,115200 console=tty1 root=PARTUUID=jsahkjahjka rootfstype=ext4 rootwait init=/bin/sh
  1. Eject the SD card, put it back in the Pi and give it some power. It will boot but then eventually halt on a prompt, which may be a # symbol
  2. You need to mount the boot partition, with:
# mount -o remount, rw /
  1. If you don’t know your actual username, use getent to find it:
# getent passwd

It will list all system users, but hopefully you’ll recognise yours (and it will be towards the end.). If you want to be fancy, you can strip all the extraneous guff, using awk

# getent passwd | awk -F ':' '{print $1}'
  1. You can now change its password using passwd
# passwd yourUser

This will prompt you to create a new password twice.

  1. With that done, power off the Pi, take out the SD card, pop it in your other machine again, and edit the cmdline.txt file again to remove the init=/bin/sh from the end.
  2. Finally, eject the card again, pop in the Pi and power on. You should now (hopefully) be able to get in right nice.
  3. Write down your username and password! Bookmark this post for the next time you need it.

I hope this helps!