Summary
If you forget your password on your RPI, then you can't login to change it. This gives some steps to be able to do that.
Note: this may work only on Ubuntu 22.04 on an RPI.
Other OS's like Raspbian or other versions of Ubuntu may not work.
Step-by-Step Password Recovery
These steps assume you are doing this on an Ubuntu PC. It should work the same on a macOS or Windows but not tested.
- Power off RPI, remove SD card, put it in your PC's card reader (microSD)
- On your PC, you should see two mounted disks: "system-boot" and "writable"
- open the "writable" one. You should see bin, dev, etc, and others
- open "etc"
- find the file called "shadow" (no extension)6) copy it (just in case!) to "shadow_orig"
since that file has special permissions I had to do this from a terminal (not sure how this works in mac or windows)
cd /media/your-id/writable sudo cp shadow shadow_orig
- open shadow in an editor.
I assume that the editor writes linux/mac compatible line endings.
if you're doing this on windows, use notepad++ which should work okay.From the command line in Linux:
sudo nano shadow
- find the userid (near the bottom) in my case it looked like this:
your-id:$y$j9T$G355Ljqy.opQ8VJfhxB7E/$Iaznza7ds.lmdiQjfip1Y7J/WSkIcNZPwTcEpFNzSh4:20314:0:99999:7:::
- delete from first colon to the 2nd colon. It should look like this afterwards:
your-id::20314:0:99999:7:::
i.e. the encrypted passwords don't have a ":" in them, so delete only those characters between the two ":" - save the file; eject the sdcard; put it back in the RPI
- power on RPI; it should come up okay and you should see the login screen and the user id on the screen
- click on the userid and it won't ask for your password, it will just login.
- to set the password, I tried using the Ubuntu settings (GUI), that didn't work.This did:
- open a terminal
- enter
passwd your-id
(i.e. the username you want to change) - enter the new password twice as requested
- you should see "password updated successfully"
- reboot and login this time with the new password to double-check everything is okay
Doesn't work
FYI I tried adding init=/bin/sh
to the boot command as recommended by AI Gemini and
this video:
YouTube video
It did not work.
Note it may be okay with Raspian or older version of Ubuntu. I have not checked it.
- The process requires you to have a second computer with an SD card reader to access the card's file system.
- Remove the SD card from your Raspberry Pi. Insert it into your computer's card reader.
- Locate the cmdline.txt file. Your computer should mount two partitions from the card. The smaller one, often labeled "system-boot", "boot" or "bootfs", is the one you need. Inside this partition, you will find the cmdline.txt file. (Note: there is a config.txt, not that one!)
- Edit cmdline.txt.
Open the file with a plain text editor. It contains a single long line of text. Go to the very end
of the line and add a single space, followed by init=/bin/bash e.g.
init=/bin/sh
Do not add any new lines or remove anything else.Example cmdline.txt (before):zswap.enabled=1 zswap.zpool=z3fold zswap.compressor=zstd multipath=off dwc_otg.lpm_enable=0 console=tty1 root=LABEL=writable rootfstype=ext4 rootwait fixrtc quiet splash
Example cmdline.txt (after):zswap.enabled=1 zswap.zpool=z3fold zswap.compressor=zstd multipath=off dwc_otg.lpm_enable=0 console=tty1 root=LABEL=writable rootfstype=ext4 rootwait fixrtc quiet splash init=/bin/sh
- Save the file and eject the SD card. Safely unmount the card from your computer and put it back into the Raspberry Pi.
- Boot the Raspberry Pi. The system will boot directly into a root command line shell, bypassing the login prompt. You will not be asked for a password.
- Remount the file system.
The file system will be mounted as read-only.
You must remount it as read-write before you can change the password.
Type the following command and press Enter:
mount -o remount,rw /
- Reset the password. Use the passwd command to set a new password for the desired user.
Replace "username" with the user you want to reset (e.g., pi).
passwd username
You will be prompted to enter a new password twice. The characters will not be displayed as you type. - reboot the system
exec /sbin/init
check you can log in with the new password. - Revert the changes to cmdline.txt
You can do it in RPI:
sudo nano /boot/firmware/cmdline.txt
remove theinit=/bin/bash
at the end of the line - reboot
reboot -f