Complete guide: Choosing an industrial controller
To reset a forgotten Raspberry Pi password without reinstalling the operating system, take out the microSD card, add init=/bin/sh at the end of the single line in cmdline.txt on the boot partition, and start the device. It boots to a root shell. Run mount -o remount,rw /, then passwd followed by the user name, then sync. Power off, remove init=/bin/sh from cmdline.txt and start again: the new password works and no data is lost. The method works on any device with Raspberry Pi OS on a microSD card, including the Raspberry PLC, the GateBerry and the TouchBerry Pi.
You need physical access to the microSD card, a second computer with a card reader, and a monitor and keyboard on the Raspberry Pi device.
Which method should you use?
| Situation | Method | Data kept |
|---|---|---|
| You can still log in with another user that has sudo | sudo passwd username | Yes |
| You can log in over SSH with a key | sudo passwd username in the SSH session, only if sudo does not ask for the forgotten password | Yes |
| No user can log in | SD card method (this guide) | Yes |
| The card is damaged or you want a clean system | Write a new image with Raspberry Pi Imager | No |
Step by step: reset the password with the SD card
1. Edit cmdline.txt on another computer
Power off the device and remove the microSD card. Put it in the card reader of your computer. The boot partition (FAT32) opens on Windows, macOS and Linux without extra tools. Open cmdline.txt with a plain text editor. The file has one line only. Add a space and init=/bin/sh at the end of that line, without pressing Enter:
console=serial0,115200 console=tty1 root=PARTUUID=4daa237a-02 rootfstype=ext4 fsck.repair=yes rootwait init=/bin/sh
Your line will have a different PARTUUID and maybe other options. Do not change them: only add init=/bin/sh at the end.
2. Boot to a root shell
Eject the card safely, put it back in the device and power it on. Instead of the login prompt, you get a root shell (#). A message such as can't access tty; job control turned off is normal: with init=/bin/sh there is no terminal control, and you can ignore it.
3. Make the file system writable
mount -o remount,rw /
4. Change the password
passwd pi
Replace pi with your user name. Recent versions of Raspberry Pi OS have no pi user by default: the user is created at the first boot or in Raspberry Pi Imager. If you do not remember the name, list the users with a home folder:
ls /home
Type the new password twice. Nothing appears on the screen while you type.
5. Save the changes and power off
sync
mount -o remount,ro /
Then disconnect the power. The usual halt or poweroff commands may not work, because the normal init system is not running.
6. Restore cmdline.txt
Put the microSD card in your computer again, delete init=/bin/sh from cmdline.txt and save. Put the card back in the device and power it on. It boots normally and accepts the new password.
On Raspberry Pi OS Bookworm and Trixie, the same file is at /boot/firmware/cmdline.txt when the system is running. On older versions it is at /boot/cmdline.txt. From another computer you always see it at the root of the boot partition. See the Raspberry Pi configuration documentation for the kernel command line.
On a Raspberry PLC, GateBerry or TouchBerry Pi
The Raspberry PLC, the GateBerry and the TouchBerry Pi of Industrial Shields run a Debian based Linux on a Raspberry Pi, with the same partition layout as a standard Raspberry Pi board, so the steps above are the same. The only difference is the access to the microSD card slot, which depends on the model: check the datasheet of your device. Connect a monitor and a USB keyboard to the device for steps 2 to 5.
A device installed in a cabinet often has no monitor nearby. If you can still reach it over the network, SSH access with a second admin user or a key is faster than the SD card method.
Password and access rules for devices in the field
- Use a password of at least 12 characters with letters, numbers and symbols, different for each device.
- Never leave a default password, such as the old
raspberry, on a device in production. - Use SSH keys and disable password logins in
/etc/ssh/sshd_configwithPasswordAuthentication no, after you check that your key works. - Install
fail2ban(sudo apt install fail2ban) to block repeated failed logins. - Keep the credentials of each device in a password manager, not in a shared spreadsheet.
- Remember that anyone with physical access to the microSD card can use this method. Keep the cabinet locked.
FAQ about resetting a Raspberry Pi password
What is the default password of a Raspberry Pi?
Older Raspberry Pi OS images had the user pi with the password raspberry. Current images have no default user: you create the user and its password at the first boot or in Raspberry Pi Imager.
Does this method delete my programs or data?
No. It only changes the password of one user. Programs, configuration and data stay on the card.
Does it work on Raspberry Pi OS Bookworm and Trixie?
Yes. Edit cmdline.txt at the root of the boot partition from another computer, as in step 1.
Can I reset the root password the same way?
Yes, with passwd root in step 4. On Raspberry Pi OS the usual practice is to keep root locked and use sudo with a normal user.
The device does not boot after the change. What should I check?
Check that cmdline.txt still has one single line and that init=/bin/sh was added with a space before it and nothing else changed.
More about Raspberry Pi in industry
- How to access the Raspberry Pi industrial PLC through SSH
- Installing an operating system in the Raspberry PLC
- Modbus TCP and RTU guide for Raspberry Pi PLCs: Python and Node-RED examples
- Setting up CODESYS for Raspberry Pi PLC in industrial automation
- Industrial applications: 50 real examples by sector
See the Raspberry PLC family, the GateBerry gateway and the TouchBerry Pi panel PC.
Take it further
With access to your Raspberry Pi again, see what a Raspberry PLC does in a real project, in a simulator and as a stack of containers.
Case study
Who gets in, how bright, how warm.
Raspberry PLCs run an office: biometric access, automatic lights and the climate on the phone.
Read the case study
Simulator
Keep a cold room in its band, door, defrost and power cut included
A Raspberry PLC 21 runs the compressor, the fans and the defrost, and keeps the record that proves the cold chain.
Open the simulator
PLC Solution Library
A four-container automation stack on one Raspberry Pi
Node-RED, MySQL, a Laravel frontend and Nginx in four Docker containers on a single Raspberry Pi, with volumes that survive recreation.
See the example