0

I have a Raspberry Pi for which I forgot the password. I want to do this to get access. So I removed the SD card and now have it in another machine (running Ubuntu). I can navigate to /media/my_user_in_the_Ubuntu_machine/root/etc and there I find the shadow file. However, it is protected and cannot even read it. Is there any solution to recover access to this Pi?

user171780
  • 103
  • 2
  • 1
    Edit the file as root? – Bodo Apr 14 '22 at 15:05
  • How do you plan to create a cryptographic key to store in you /etc/shadow file? https://unix.stackexchange.com/a/81248/325016 – Dougie Apr 14 '22 at 20:31
  • sudo whatever /media/my_user_in_the_Ubuntu_machine/root/etc/shadow does not work. I guess that this would work if I am sudoing as one of the administrator users in the Pi. – user171780 Apr 15 '22 at 09:05

1 Answers1

0

I got tired of doing this by hand, so I took the code from https://unix.stackexchange.com/questions/158400/etc-shadow-how-to-generate-6-s-encrypted-password#comment259421_158402 (thank you @cristian-ciupitu https://unix.stackexchange.com/users/1174/cristian-ciupitu ) and I filled out the other necessary bits to manage the actual file.

mcra@yertle:~$ sudo bin/passwd-poke /media/mcra/rootfs/etc/shadow pi beammedownscottie
updated pi,
  was ['pi', '!$6$VbF8BUzOyvy3Fao1$vKu8ujm2sTgeWZQyyGGTFij8toc3RtUTkpNbfmSmJu4FlmrpSV.oL3dq1ugiIcEy3mk0bHZ5Fdt2cKhG2ytlz1', '18598', '0', '99999', '7', '', '', '\n']
  now ['pi', '$6$CXJ4W1ucar6HJVN9$IfE4ikt7UK6bQ0qX5MPlEL8qMoEwYXU4qf0Eda53TuHNnEFJZKJHRr5TjIQ1D4DehvfmbaoBt0.H3IC/BgwU4/', '19506', '0', '99999', '7', '', '', '\n']
mcra@yertle:~$ sudo ls -lrt /media/mcra/rootfs/etc/shad*
-rw-r----- 1 root shadow  964 May 13  2022 /media/mcra/rootfs/etc/shadow-
-rw-r----- 1 root shadow 1036 May 29 12:57 /media/mcra/rootfs/etc/shadow~
-rw-r----- 1 root shadow 1035 May 29 14:02 /media/mcra/rootfs/etc/shadow

Code is over at https://github.com/mcast/scripts/blob/master/bin/shadow-poke

mcast
  • 31
  • 3
  • 1
    If the issue is literally what it is in the question ("I have a Raspberry Pi for which I forgot the password"), it's probably easier to just delete the password: https://unix.stackexchange.com/a/168422/25985 ...Although that does require you be able to access shadow from another system if you cannot log in otherwise, note that this issue with it "being protected and I cannot even read it" is likely not reproducible: It is not possible to protect a file that way (ie., prevent it from being accessible if the storage is mounted elsewhere)... – goldilocks May 29 '23 at 14:21
  • ...All you can do is encrypt the fs or file (which doesn't apply here). Ie., the real issue has to do with the Ubuntu system or how the card was mounted (the OS can prevent an unprivileged user from accessing something, but not root). Note that using sudo depends upon the ubuntu user be an appropriately configured sudoer on the Ubuntu system, and that is not a normal default except on RpiOS. Ie., it won't work just because the it does on the Pi unless you make it so. – goldilocks May 29 '23 at 14:22
  • (I don't have enough points to comment on the Q itself)

    My interpretation of "cannot even read it" was just that the file is private to root. Using sudo on the host which mounts the SD will fix this - where it can be fixed, otherwise security is working as intended.

    – mcast May 30 '23 at 17:16