Restoring the root user’s password in Linux

Stuff happens in life, and root passwords get lost. So what do you do when you need into the system and the password is gone? The actual question: how do you restore the root password in Linux?

First, don’t panic — look for a LiveCD of the installed system. If it’s a hosted server, look in the admin panel for a rescue boot or network boot option, or anything else that looks like booting a helper kernel.

Once you’ve booted the system into recovery mode, you need to mount the disk with the files and data.

  1. List the disks with fdisk
    fdisk -l
    

    Running this command may give you the following error:

    WARNING: GPT (GUID Partition Table) detected on '/dev/sda'! The util fdisk doesn't support GPT. Use GNU Parted.
    

    In that case use gdisk:

    gdisk -l /dev/sda
    
  2. Mount the partition into /mnt (in my case - /dev/sda1. Go by what gdisk or fdisk showed you)
    mount /dev/sda1 /mnt
    
  3. Switch the system to work with the mounted disk:
    chroot /mnt
    
  4. Reset the password:
    passwd