Tuesday, October 8, 2013

Changing a Forgotton Root Password in Linux

I built a Linux server a few months ago for monitoring purposes for a customer, however I failed to write the root password down - doh!  In this post I will be showing you how to change the root password for Linux systems using GRUB boot loader.  If your running LILO (LInux LOader), this how to will not work for you.

The Linux distribution used in this demonstration is CentOS release 5.9 which can be viewed using the "cat /etc/*-release" command, a quick way of finding the distribution of a unix/Linux operating system.

Now to recover a lost root password, you must enter a operating mode known as "single user mode".  This is an environment which does not require login details to logon to the system which hence enables us to change the password of the root account.

Now the first ting you need to do to enter single user mode is at the grub boot screen, press "a" to enter "append" mode.  The grub boot screen in later versions can be graphic interfaces, however on older distributions of Linux it is generally text based.  In my version of CentOS server, my grub screen is a GUI and has a pretty pattern in the background.

This is where you press "a" to get into that append mode.  Be quick as this screen moves pretty fast.


Next pick select the Kernel you wish to boot, there can be multiple here especially if you have done Kernel upgrades in the past.  If you are not sure, select the latest version by highlighting it using the up and down arrows.

Once you have selected the version you want, press "e" to enter "edit mode".


Now in edit mode, you want to select the entry that has "kernel" in front of it.

You will have ro root=something, depending on your Linux distribution/installation this can look different.  For example in another installation this reads:

ro root=/dev/sda1

In this installation it reads:

ro root=/dev/VolGroup00/LogVol00

What you need to do is append the word "single" at the end, all lowercase to instruct Grub to boot "single user mode".  I will type the entire thing out:

kernel /vmlinuz-2.6.18-348.1.1.e15 ro root=/dev/VolGroup00/LogVol00

needs to become

kernel /vmlinuz-2.6.18-348.1.1.e15 ro root=/dev/VolGroup00/LogVol00 single

To make the change to the kernel boot line hit "e" to "edit" the entry.


Now in the screenshot you see I have appended the word single a the end of the line.  Sorry you cant see the entire command, it has gone onto another screen due to my screen resolution being so small.  The full path looks like this:

kernel /vmlinuz-2.6.18-348.1.1.e15 ro root=/dev/VolGroup00/LogVol00 single

After making the change press ENTER.


In the screenshot below you can see that I have appended the word "single".  Again it cuts out due to the screen resolution being so small but you can see the "s".

To boot the new kernel boot command, press "b" for "boot".


The system will now boot straight to single user mode and you will be presented straight away with a command prompt.

To change the root password at the command prompt, type:

passwd root

Enter the new password for your system.  After you have changed the password type "reboot" and hit enter to reboot the system.


I hope this post has been helpful.

No comments:

Post a Comment