Methods of payment Abuse

Resetting a password in Linux

19.05.2021, 22:32

Every user has at least once forgotten passwords to websites, social networks that they haven't used for a long time. The same situation can happen with a Linux distribution. It is not difficult to forget it, especially if there are many different distributions with different passwords. Next we will talk about what to do if the Linux password has been forgotten, how to reset it correctly and not to erase your data. Resetting a Linux password is not a very difficult task. But it should be clarified that you cannot find out your password, you can only reset it.

Resetting the password using LiveCD

To reset the password, you need to arm yourself with a LiveCD disk with any Linux distribution, it is only important to match the bit capacity of the system to be recovered and the system on the disk. It is best to take the latest versions of distributions. They are more convenient to work with, although even Gentoo and ArchLinux will do.

Next, boot from the LiveCD disk and open the terminal. If the disk boots into a command shell without a graphical interface, that's okay. You need to know which hard disk contains the root file system of the distribution you are going to install. You can view all available disks with the command:

$ fdisk -l

Resetting a password in Linux

As a rule, the disk name starts with the letters sd for regular disks and nvme for M.2 SSDs. The root partition is small if a separate partition was allocated for the home folder during installation and it has a Linux file system. If no separate partition was made for the boot loader, the root partition will also be marked with an asterisk as bootable. For example, this is /dev/nvme0n1p1. It should be mounted to /mnt:

 $ mount /dev/nvme0n1p1 /mnt/

Resetting a password in Linux

So we can see /dev/nvme0n1p1 - the partition where the system was installed. It might as well be /dev/sda1 or /dev/sdb5. Then enter the chroot environment:

$ chroot /mnt /bin/bash

Resetting a password in Linux

We now find ourselves in the environment of the distribution that is installed on the mounted disk. This is such a variant of recovery mode without starting the operating system.

Next, the user can set the password of the root linux user using the passwd command:

$ passwd root

Resetting a password in Linux

It may be that the utility will give an error, but if it did not give any error, the password has been successfully updated and you can use your system.

There is one more way. The passwords of all users are stored in the /etc/shadow file like this:

user_name:encrypted password:user_id:group id_id

For example:

root:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA:0:0:::::
bin:!!:9797:0:::::
daemon:!!:9797:0:::::

For each entry, all parameters are separated by colons, the first parameter (root) is the username, the second parameter (AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...) is the password hash. To change the password to the desired one, you must first manually create its hash. To do this, you can use this command:

$ openssl passwd -1 -salt xyz new_password

Resetting a password in Linux

Then paste it in place of the previous one:

$ vi /etc/passwd

Resetting a password in Linux

It is not possible to completely remove the Linux password, without the password you will not be able to authorize in the system, here the best option is to replace it with a new one. After replacing it, save the changes and you're done. Before rebooting, don't forget to exit chroot and unmount the root of the system:

$ exit
$ umount /mnt

Resetting the password with Grub

If you don't have a disk at hand, you can use Grub for this purpose. In the boot options menu, press E to edit the kernel parameters:

Resetting a password in Linux

Here in the line vmlinuz in after all parameters (see screenshot) you need to add the parameter init=/bin/bash:

Resetting a password in Linux

Press Ctrl+D to continue booting. After initialization is complete, the kernel will pass control to the bash shell command, which we passed in the kernel parameters, where you can recover the password. By default the file system is mounted in read-only mode, to be able to change anything you need to remount it for writing:

$ exit

$ umount /mnt

All other actions with the /etc/shadow file are similar to the first point.