As it turns out, you don't have to flash your RPi to recover from this. In fact, you don't need another Linux system at all, all you need is another SD card flashed with your RPi OS flavor of choice and a USB SD card reader to put your original SD card in. The
init=/bin/sh method did not work for me (I think there was a display conflict with the custom Adafruit kernel needed to use 3.5" TFT screen embedded with the RPi screen) so I used the
chroot method which I discuss below. If the
init=/bin/sh method works for you, you should be able to use the steps below in the recovery console to achieve a similar result.
My answer to this post was based on my experience with this problem.
Turns out using the
chroot method to change your password gives several options to discover your forgotten username. The easiest would likely be to
cd /home and then
ls to view the directories, but that might not be helpful enough.
Like most things Linux, that is not the only way, there is also the
/etc/passwd file. "I looked in there and there are a ton of users I don't know anything about!" Fear not, most of them are task specific and were placed there by software for those tasks. Lets take a look at the file anyway,
sudo nano /etc/passwd shows us this:
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
systemd-timesync:x:100:103:systemd Time Synchronization,,,:/run/systemd:/bin/false
systemd-network:x:101:104:systemd Network Management,,,:/run/systemd/netif:/bin/false
systemd-bus-proxy:x:103:106:systemd Bus Proxy,,,:/run/systemd:/bin/false
avahi:x:105:110:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
First off, you can discard from consideration anything ending with
/nologinand anything that ends with
/false as these types of user accounts are not for human users but for system agents and software pointers.
This leaves us with a much more reasonable list of candidates.
Ah ha! I enjoy my pi computers, but my dessert of choice is generally cupcakes! I do so love the icing. Exit nano WITHOUT SAVING to preserve the original contents.
In the comments below @ferrybig points out that we can also discount any account with a
UUID under 1000. The
UUID is the number that appears after the second colon. This tip is a really good one in that if you have more than one user, you can infer a lot about each user based on the order in which they are created, which is expressed in the sequence of their
UUID's. While its not a hard and fast rule of system administration, its a good first choice to go with the lowest
UUID that is higher than 1000. Admins tend to build the managing accounts before the users after all.
Now that I have the username, it's probably a good idea to blank the password for this user while I'm in the guts of the locked system. We wouldn't want to put everything back together to find our password memory is just as bad as our username memory would we?
I should note that for this example I'm showing the contents of a new install with a LAMP stack, it might have items your install doesn't. Also I renamed the default
pi user to
cupcake in a text editor before copying to this post. The
UID for the default
pi user is always 1000 and any replacement user you create will have a different
You might think that passwords are stored in a file called "passwd" but they are not. Linux programmers are sneaky that way. An 'x' in between the first and second colon indicates that a password is required for the user. Passwords are stored in the
/etc/shadow file. Not all Linux systems do it this way, but Raspbian does.
Lets take a look at this file,
sudo nano /etc/shadow shows us this:
Lots of entries here, just like the /etc/passwd file before we discarded all of the /nologin and /false items. Good news is since we know
cupcake is the user we are interested in this time, we don't have to discard anything to find it. The only line you are going to mess with is the line with your found user account on it.
/etc/shadow colons are used as separators. The actual password here is all the characters between the first and second colon, but it's encrypted. What you need to do here is remove everything between the first and second colon. When your done it should look like this:
Now this user will be permitted to login without a password, just hit enter at the password prompt. This is a bad practice normally, so after you login you should set a password.
sudo passwd [username] and follow the prompts, be sure to replace [username] with your custom username, if your logged not into the account, it can be omitted if you are logged into the account your changing passwords for. Using
sudo for this command eliminates the need to enter your current password.
If that doesn't work for you, say you can't find your username in the list or something else goes wrong, there is a last-ditch method you can use.
Any user listed in
/etc/shadow with an asterisk between the first and second colon is disabled. By default, Raspbian disables the
root user. remove the asterisk like the encrypted password above. This method is even more dangerous to leave in place, because you've eliminated the password on your root user. Once logged in, you'll need to create a new user with elevated permissions, reboot and login to that new user and then
sudo nano /etc/shadow again and disable the
root user. Leaving the
root user enabled is not necessary provided you have access to
sudo through your regular login. In fact it's a security risk.
Thats it, you've recovered your forgotten login and password. But where does that leave us on security? Since an accomplished hacker could bypass your digital security with physical access to your system just like I have shown here, it's probably safe enough to put the password on a post it or even writing on painter's or masking tape affixed to the case of your RPi. Especially if your RPi is in a physically secure location.
More info on the
/etc/shadow files, including what all of the fields in between the colons mean, can be found here.