Recovering From a Lost Linux Root Password

Written by Mark Sanborn: Feb 25, 2008

Lost your Linux root password and need to do change it fast? Then this guide is for you. I recently had lost my Linux password and I needed to get it back right away after doing a bit of searching a ran into a few different ways of doing it. Some of them required nothing but changing the lilo/grub config to single user mode.

Some guides suggested deleting the /etc/shadow directory. Although those methods may have worked I decided to go with the method I was most comfortable performing. Which is to boot into a Linux live cd, chroot, and change the password the way you would normally do it (passwd root).

The first thing we need to do is boot up a working Linux distribution. Since our current distribution is crippled by our forgotten password we shall use a Linux live CD. Now there are a plethora of live cds to choose from. I have tested this method on SystemRescueCD, the latest version of Knoppix, and the Gentoo Live CD. Although, it should work with any live CD.

Once you get booted into the live CD, we must be root to use most of the commands that follow so lets switch to root.

su - root

We will then need to determine which device your Linux hard drive is listed as under linux. Look for the listing that says Linux it will have the root partition on it.

fdisk -l

We will then need to make a directory to mount to.

mkdir /mnt/maindrive

Mount the drive. Change the sdb1 to match your device that you found using, ‘fdisk -l’.

mount /dev/sdb1 /mnt/maindrive

Now we are going to issue the big command. This will change your root directory from the live CD to the one on your hard drive. The key here though is that you already have root access when switching therefore you will have root access to the Linux system on your hard drive. We also append the /bin/bash command to start the bash shell in your new environment.

chroot /mnt/maindrive /bin/bash

Now you should have root access and a basic bash shell. You probably know what to do from here. You simply issue the command you would always use to change a users password and since you are root you can change anyone’s password even your own (which is what we will do).

passwd root

You will then be greated with some text that says, “Enter new UNIX password” or something similar. Type in your desired password. Note: This goes without saying but I will say it anyways, don’t use ‘password’ as your password. If you want a good password go here.

password

You can now reboot into your normal distribution.

reboot

I tested this method using three different live cds. None of them took more than three minutes to boot in, and change the Linux root password. If you don’t start the X graphic system you can change the password in less than 30 seconds. Remember if someone has physical access to the computer you have no security.

Hopefully I saved you from having to reformat or pulling your hair out. If you know of a better way to change the root password let me know.

Need to print shipping labels on your site?

Checkout my product RocketShipIt for simple easy-to-use developer tools for UPS™ FedEx™ USPS™ and more.

Share: