Like any good computer person I own a few Raspbberry Pi's - and they currently sit mostly idly (who could've guessed). And recently, I've wanted to update them and the code I wrote for them, but bumped into issues logging in.
On one Pi (4) I must've logged in via ssh previously, ssh was just turned off, somehow?
This I've easily overcame plugging the SD card into my Mac and adding the ssh
file in /boot
directory
(as per raspi docs)
and rebooting it.
But this wasn't the issue-of-the-day. When I wanted to log into my other Pi (0 W),
it again rejected me at login - "Login Incorrect
". No biggie, just: take out SD card, plug it in,
touch the /boot/ssh
, eject, reboot.
"Login Incorrect
"
Huh? Well, maybe it actually wasn't created? I should've made sure it appeared, I guess.
But no biggie, just: take out SD card, plug it in, ls -la
. Well, I don't see it, curious.
Is the ssh
file on my other's Pi SD card? Take out card #2, plug in card #1, ls -la
. Nope.
Probably gets deleted when it's turned on then. Take out card #1, plug in card #2, touch ssh
.
But also, let's make sure it was created and I'm not wrong on the deletion-upon-acceptance.
ls -la
. It's there. Great.
But maybe, let's be extra cautious. Maybe my card is jesting me into thinking it's there?
Take out card #2, plug in card #2, ls -la
. It's there. Great x2.
Eject it, reboot.
"Login Incorrect
"
Well, that's not funny. I'm pretty sure this was my password for this Pi, but maybe I'm wrong?
Let's check other passwords.
"Login Incorrect
". "Login Incorrect
". "Login Incorrect
".
Maybe there's just a default user there? "Login Incorrect
"
Maybe, there's just no user there?
As per raspi docs,
I just have to create a userconf.txt
. Great news.
I just have to use openssl passwd -6
, make the hash, paste it in <user>:<hash>
to userconf.txt. Done.
Eject, reboot, "Login Incorrect
". Huh?
I've repeated the previous step with multiple users, a few times.
After that, I assumed that this just plainly wasn't working in my case, or my SD card was broken after all.
But I saw the userconf.txt
persisted, and I was hoping I could get to what I already had on that card ("easily"1), so I went with the not-working-for-me route2.
I started looking for an alternate way of getting in. I wasn't getting any hits at first,
but then I stumbled upon an article mentioning I can append init=/bin/sh
to my config.txt
and it would get me a
pre-boot root shell instead of trying to authenticate a user. Great news (am I repeating myself?).
I do that, nothing happens. Time for more research.
Turns out it's not to be appended to config.txt
, but cmdline.txt
.
I do that, get a shell. 🎉
But I can't change the password.
"cannot lock /etc/passwd; try again later.
"
Curious again. I assumed the system I got was 'it', just skipping the auth procedure - which was supported
by a snippet I've read about why rootwait
was at the end of my unmodified cmdline.txt
- but clearly it wasn't.
Research again. I either can't lock /etc/passwd due to a lock, or due to the fs being mounted as read-only.
No lock of any kind, guess it's time to remount the fs. mount -o remount, rw /
.
"can't find PARTUUID=<...>
"
Guess I screwed up? Reboot, mount -o remount, rw /
, "can't find PARTUUID
".
Researching only brought up "convoluted" (by which I mean, probably too complicated comparing to my issue) results,
so I've had a classic bash-fan thought. Maybe, all my issues could be fixed simply by using bash.
So, init=/bin/bash
, reboot, and voilà ; apart from a meaningless
"bash: cannot set terminal process group (-1): Inappropriate ioctl for device
"
"bash: no job control in this shell
"
I got a shell in which mount -o remount, rw /
gave me a blissful
"EXT4-fs (<...>): re-mounted. Opts (null)
"
Now we can go back to passwording. sudo passwd pi
, and job's done.
After that, sync
and exec /sbin/init
to finish the boot. And editing out init=/bin/bash
from cmdline.txt
.
Post Scriptum
Seems I've also haven't changed my user on that Pi, so, run:
sudo adduser temp
sudo adduser temp sudo
exit
<login as temp>
sudo usermod -l <user> pi
mv /home/pi /home/<user>
sudo usermod -d /home/<user> <user>
sudo groupmod -n <user> pi
exit
<login as <user>>
sudo deluser temp
rm -rf /home/temp
substituting <user>
for your preferred user, to do just that.
I also left the default raspberrypi
hostname, and I hate typos,
so I also ran sudo hostnamectl set-hostname <hostname>
to change it,
and also sudo nano /etc/hosts
to fix it there too.
-
I'm aware that, in theory, I could've just mounted the SD card, get whatever I wanted from it, and reflashed it. But I currently don't have a linux pc, and on Mac I could've only gotten a read-only fs (not perfect, too lazy to reflash) with ext4fuse, but I hadn't had to setup fuse prior so I didn't want to mess with it now. ↩︎
-
I assume, based also on stuff I've read, that this only works on first boot. ↩︎