2

I have an RPi that I have forgotten the password to. I have history on the ubuntu command line on my PC of ssh logins and I was hoping there would be a way to get text history entered into to try to find my password. If not is there a way to recover an RPi password? I am running Ubuntu Server (not Raspbian) and was unable to access GRUB.

Earlier I was using the history command but that only shows the ssh commands and not the details entered in.

Any help would be massively appreciated!

EDIT - Some solutions don't work because they are for the Raspberry Pi 3B and below and I have a Raspberry Pi 4B

EDIT - I have found a solution thanks to a friend of mine, I will put an answer below. Thanks to everyone who helped me :)

Bobcat33
  • 53
  • 1
  • 7
  • 3
    You can list commands, not passwords. – ChanganAuto Aug 15 '21 at 12:43
  • If your file system is not encrypted, you can create a new password according to the answers in this link. I suggest that you try according to the answer by Jorge Castro. – sudodus Aug 15 '21 at 12:48
  • No, all of these require access to the GRUB menu which I am unable to access on my RPi – Bobcat33 Aug 15 '21 at 12:53
  • In a PC you get to the grub menu if you press the left shift key during boot. Maybe it works in RPi too. – sudodus Aug 15 '21 at 12:58
  • @sudodus It unfortunately doesn't, I spent about 10 minutes trying that this morning – Bobcat33 Aug 15 '21 at 13:00
  • @NateT I know but I was hoping that there would be a simple solution... – Bobcat33 Aug 15 '21 at 13:01
  • 1
    Can you boot from an Ubuntu live USB drive, or is it [still] necessary to boot RPi from the SD card? Have you access to a PC computer, where you can connect your SD card with the system? – sudodus Aug 15 '21 at 13:04
  • 1
    Unless you used something like sshpass -p mypassword or echo mypassword | sshpass then you didn't enter your password into the ubuntu command line, it would have been read directly by the ssh client – steeldriver Aug 15 '21 at 13:05
  • @NateT oof! Thanks for your answer, I just came across a similar answer here - glad it can be resolved! – Bobcat33 Aug 15 '21 at 13:14
  • 1
    Can you read the files in the SD card, when connected to the PC computer? In that case the fastest solution might be to make backup copies of your personal files (documents, pictures etc), and then make a fresh installation of Ubuntu into the card, or get a new (maybe bigger) card and install Ubuntu into the new card. – sudodus Aug 15 '21 at 13:32
  • 1
    @sudodus the previous solution isn't working so I will probably try that, thanks :) – Bobcat33 Aug 15 '21 at 13:33
  • Wait WHAT ABOUT .BASHRC?! IT SHOULD BE ON THE MEMORY CARD! iT AUTORUNS SH!!!! – Nate T Aug 15 '21 at 14:02
  • Just a shot in the dark for posterity: You don't have postgres installed do you? If so, you can ssh as postgres user. – Nate T Aug 15 '21 at 14:45

3 Answers3

3

To solve this issue I took the SD card out of the pi and opened it in another linux machine.

In the terminal I used the command below to remove the "x" from the root user line

sudo nano /mnt/sdcard/etc/passwd

(You may need to mount the SD card or USB stick reading the SD card)

Then I was able to put the SD card back into the pi and log in to the root user without typing a password.

Finally I could create a new password using the passwd command.

Bobcat33
  • 53
  • 1
  • 7
2

You can show a lot of it with:

cat .bash_history

or just

history

However, once the buffer is full, only some of them will be written. The rest will be lost. You can deal with this in at least 2 ways.

  1. Write a bash script to save it. You can, for example, tee everything to STDIN and a file of your choosing.

  2. Edit ~/.bashrc -- There is a line in this file that sets a cap on the count and another for file size.

As for login credentials, no. Otherwise, users could just look at each other's passwords.

I am not sure where the credentials are stored, but I would almost guarantee that they are encrypted.

EDIT From here

  1. Power down and pull the SD card out from your Pi and put it into your computer.
  2. Open the file 'cmdline. ...
  3. Put the SD card back in the Pi and boot.
  4. When the prompt comes up, type 'su' to log in as root (no password needed).
  5. Type "passwd pi" and then follow the prompts to enter a new password.
Nate T
  • 1,524
  • Thanks! I also came across a very similar answer here! – Bobcat33 Aug 15 '21 at 13:15
  • Actually it seems these answers have not worked as I am using a Raspberry Pi 4B and the software has been updated since then... – Bobcat33 Aug 15 '21 at 13:25
  • Nice try Nate :-) I was hoping too, that the method you suggest in the edit would work. Anyway I upvoted your answer - I think the method will work for users of older versions of RPi. – sudodus Aug 15 '21 at 13:39
  • Thank you. What about using sudo? Is remote root access enabled? There is another maybe option but its too much work for a maybe. It involves loading a bridged VM/hypervisor or container/daemon onto the memory card with a bridge. Then you should be able to wake it up via network.If it is configured via files, rebooting will connect it without user. The problem would then be 'getting out of the box.' – Nate T Aug 15 '21 at 13:51
  • Kind of like you can tunnel in, you just can't dig up. In other words I have no idea... – Nate T Aug 15 '21 at 13:57
0

As this seems unresolved for newer versions of Ubuntu: Take the SD card from the Pi and insert it into a computer that can read/write the ext4 filesystem. Mount the second partition (e.g. /dev/sdb2) and edit the <mountpoint>/etc/shadow file.

If you need a password to insert, you can use

mkpasswd --method=SHA-512 --stdin

to generate one. Erase the old one, add the new one. Put card back into Pi. Boot up as normal, the password you created with mkpasswd should now be set.

Greenonline
  • 2,081
crh
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Mar 24 '23 at 17:37