Select font size:          

 

How to reset the root password on Linux

The scenario is that you forgot your user password with which you usually access Linux or the root password, if there is one set.
For this usecase we are using a rescue CD like SystemRescueCD or Mariposa Rescue Disk for troubleshooting, but the approach is similar, independent from the distribution or live-CD that you use.
In case you do not know how to boot from the rescue CD, check the articles about Ventoy or Rufus.


Boot your RescueCD and switch to root.

su - root

Find out the root logical volume.

lsblk -io KNAME,TYPE,SIZE,MODEL

The output is something like the following:

KNAME TYPE   SIZE MODEL
sda    disk  465,8G Samsung SSD 870 EVO 500GB
sda1   part    487M 
sda2   part   55,9G 
sda3   part   29,8G 
sda4   part      1K 
sda5   part  379,6G 

Then, in order to be really sure how the partitions have been mounted, enter:

less /etc/fstab

The output is something like the following:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#                
# / was on /dev/sda2 during installation
UUID=[...] /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
UUID=[...] /boot           ext4    defaults        0       2
# /home was on /dev/sda5 during installation
UUID=[...] /home           ext4    defaults        0       2
# swap was on /dev/sda3 during installation
UUID=[...] none            swap    sw              0       0

Alternatively you can enter the following command:

sudo lshw -class disk

The output is something like the following:

  *-disk:0
       description: ATA Disk
       product: Samsung SSD 870
       physical id: 0
       bus info: scsi@2:0.0.0
       logical name: /dev/sda
       version: 1B6Q
       serial: [...]
       size: 465GiB (500GB)
       capabilities: partitioned partitioned:dos
       configuration: ansiversion=5 logicalsectorsize=512 sectorsize=512 signature=[...]

and then:

sudo fdisk -l /dev/sda

The output is something like the following:

Disk /dev/sda: 465,76 GiB, 500107862016 bytes, 976773168 sectors
Disk model: Samsung SSD 870 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: [...]

Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1  *         2048    999423    997376   487M 83 Linux
/dev/sda2          999424 118185983 117186560  55,9G 83 Linux
/dev/sda3       118185984 180686847  62500864  29,8G 83 Linux
/dev/sda4       180688894 976771071 796082178 379,6G  5 Extended
/dev/sda5       180688896 976771071 796082176 379,6G 83 Linux

In this case, /dev/sda2 is our root logical volume.

Create an directory to which you can mount the root logical volume: mkdir /mnt/root

Mount the logical volume: mount /dev/sda2 /mnt/root

Change the root directory from SystemRescueCD to the one that just mounted:

chroot /mnt/root /bin/bash

Reset the root or user password: passwd root or passwd user1

You will be prompted to enter an new password.

Reboot.

exit

reboot

Check for more Linux related articles.