8

I have an external hdd partitioned into two. One of the partitions has Linux (Ubuntu) installed on it (bootable). The thing is I have forgotten the password of it's user (single user).

How can I reset the password of user by plugging the external hdd to another Linux machine and then editing some file, using some command through terminal, changing the bash(remember doing something similar long time ago), or something else?

And I do not want to get into Grub etc. Booting through that drive is not an option (although it is but I am not willing to get into it and don't want to restart the running("another") Linux machine).

3 Answers3

20

Even though michas gave you the optimal answer, it still involves booting from the external hard drive, which for some reason you seem against. Here's a method you can use from another Linux system without booting from the external drive. I assume that your Linux partition on the external HDD is /dev/sdb1, modify the following as needed. Run this code on your other Linux installation:

[root@host]# mount /dev/sdb1 /mnt
[root@host]# chroot /mnt
[root@host]# passwd user
Enter new Unix password:
Retype new Unix password:
passwd: password updated successfully
[root@host]# exit
[root@host]# umount /mnt
Joseph R.
  • 39,549
  • chroot is indeed safer than manually editing /etc/shadow. – michas Dec 15 '13 at 21:43
  • 7
    chroot will not work if mount is from a different architecture (e.g, x86 vs ARM) because chroot runs /bin/bash – Aleksandr Levchuk Aug 05 '18 at 19:16
  • 6
    Indeed. This method doesn't work if you want to reset your password on a raspberrypi (and don't have a PC with ARM 32-bit). Is there a way to execute passwd without chroot? I tried passwd -R /run/media/username/drivename root But I got passwd: Cannot determine your user name. – Scindix Aug 21 '18 at 14:29
15

No need to put the disk into another machine.

I assume your computer uses Grub as a bootloader. (The OS-choosing thing at the very beginning of the boot process.)

You can use it to temporary edit the linux command line adding init=/bin/sh. This will instead of booting the whole system just open a root shell, which allows you to set a new password with passwd username.

After another reboot everything should be fine again.


If you really insist on setting another password using another computer, you have to mount that disk and edit the file $mountpoint/etc/shadow, which stores the hashed password. (It should be possible to paste a line from the other computer for a user you know the password.)

A safer way to achieve the same is first using chroot $mountpoint and then changing the password using passwd username. This will also change the same file, but you can be sure it stores the password in the correct way and don't risk an invalid line.

michas
  • 21,510
  • 1
    ... or you can edit $mountpoint/etc/passwd and replace the :x: password field with :: to clear the password entirely. – einpoklum Aug 04 '23 at 19:11
2

Just to extend a little Michas answer. For everyone who struggles with changing the password on another architecture (e.g. ARM) and cannot defeat '/bin/bash' no such file or directory there is a solution.

WARNING: This solution is actually "last hope" and not recommended to use (please, read)

The exact steps:

  1. Open the /etc/shadow computer where the password is known.
sudo cat /etc/shadow
  1. Choose the user you know the password of. It might looks like:
username:$6$rNA/BOfW$i9jrx/yF9bQfj.XOSNkvMlAzObxW345345EYzQx/5nSsl4cHiNtr8aky91/:18265:0:99999:7:::
  1. Copy the whole string from first : until the last three ::: i.e.:
$6$rNA/BOfW$i9jrx/yF9bQfj.XOSNkvMlAzObxW345345EYzQx/5nSsl4cHiNtr8aky91/:18265:0:99999:7
  1. Mount the root file system of the computer where you want to change the password. And replace the hash of the old password for the desired user with the copied one.