Resetting Linux lost root password

Linux

Happened to me a few times:

You lost your root password and there is nothing you can do..

 there are 2 things you can ! 

 phase1:  find a linux CD that you can boot from and work from (something like Ubuntu or Knoppix will do).

phase2: after You are live with the CD You need to locate the main filesystem of the rescue-pending Linux installation

 

# fdisk -l

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x0003a956

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        2496    20049088+  83  Linux
/dev/sda2            2497        2610      915705    5  Extended
/dev/sda5            2497        2610      915673+  82  Linux swap / Solaris

 

It is usually the biggest Linux partition.. in this example We are sure its /dev/sda1.

phase3: create a mount point to mount the main Linux partition

# mkdir /tmp/sda1

# mount /dev/sda1 /tmp/sda1

phase4:  chroot into the mounted partition and try to change root's password

# chroot /tmp/sda1

# passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

We did it !! 

can now exit from chroot (just by 'logout' or 'exit' command)  and reboot.

if it didn't work:

phase5: If you have sudo installed on the pending-rescue Linux its also a solution, edit sudoers file and add your normal user (user that you do know his password, I use 'vi' editor in this example, you can use what u like and what is available on the linux cdrom).  In this example I edit the file from outside of the chroot:

vi /tmp/sda1/etc/sudoers

add a line that say: 

yourusername ALL=(ALL) NOPASSWD: ALL

on next time your user will be able to become root by running "sudo su -"  and will become root without a need for a password.