Showing posts with label Linux Tips. Show all posts
Showing posts with label Linux Tips. Show all posts

Friday, August 31, 2012

In Linux reset forgotten root password

1. Introduction


This method of reseting/recovering of lost Linux root password should work on most of linux distributions. I have tested this method for couple years already, starting with debian woody trough redhat and suse to ubuntu hardy.
If you run lilo boot loader instead of grub you can use the same method but with some modifications on how to edit lilo boot prompt.

2. Edit Grub boot menu options

First you need to get into grub menu options. This menu is displayed right at the beginning of the boot. If you cannot see your grub menu options press "ESC' key.

You should get something similar to this:
grub boot menu
Now we attempt to edit grub's boot option. Press "e" to edit the first grub menu option and navigate to kernel line:

edit grub boot option with

Press "e" key again to edit and remove:
quiet splash 
and add:
init=/bin/bash 
You may have some different boot options but the main part you need to change/add is init=/bin/bash. You will get something similar to this:
change to init=/bin/bash
Press enter:
ready to boot from edited grub menu

At this point, we have edited grub boot menu, and we are ready to boot. Press "b" key to boot.

3. Remount / and /proc

After successfully boot you will be presented with bash command prompt:

booting to a bash command prompt

On some linux systems, you will need to completely mount / and /proc partitions. To do that, enter following commands:
mount -o remount,rw / mount -o remount,rw /proc 
* NOTE: If you are not sure that if your partition is already mounted RW, run the above command anyway as, otherewise on some systems you will not be able reset your root password. If you fail to do so, you get this error displayed on the screen:
passwd: Authentication token lock busy 
* NOTE: On some Linux distributions, you will have /proc mounted already if this is not your case, just run following command:
mount /proc 
mount and remount partitions in single boot mode

4. reset / recover forgotten linux root password

To reset a actual root password is now simple as typing :
passwd 
reset / recover forgotten linux root password

5. Reboot

Before you reboot it is recommended but not compulsory to run
sync 
command. Your job of reseting a linux root password is accomplished.

How-To: Recover root password under linux with single user mode

       It happens sometime that you can't remember root password. On Linux, recovering root password can be done by booting Linux under a specific mode: single user mode.
This tutorial will show how to boot Linux in single user mode when using GRUB and finally how to change root password.
During normal usage, a Linux OS runs under runlevels between 2 and 5 which corresponds to various multi-user modes. Booting Linux under runlevel 1 will allow one to enter into a specific mode, single user mode. Under such a level, you directly get a root prompt. From there, changing root password is a piece of cake.

1. Entering runlevel 1

Some Linux distribution, such as Ubuntu for instance, offer a specific boot menu entry where it is stated "Recovery Mode" or "Single-User Mode". If this is your case, selecting this menu entry will boot your machine into single user mode, you can carry on with the next part. If not, you might want to read this part.
Using GRUB, you can manually edit the proposed menu entry at boot time. To do so, when GRUB is presenting the menu list (you might need to press ESC first), follow those instructions:
  • use the arrows to select the boot entry you want to modify.
  • press e to edit the entry
  • use the arrows to go to kernel line
  • press e to edit this entry
  • at the end of the line add the word single
  • press ESC to go back to the parent menu
  • press b to boot this kernel
The kernel should be booting as usual (except for the graphical splash screen you might be used to), and you will finally get a root prompt (sh#).
Here we are, we have gained root access to the filesystem, let's finally change the password.

2. Changing root password

As root, changing password does not ask for your old password, therefore running the command:
# passwd
will prompt you for your new password and will ask you to confirm it to make sure there is no typo.
That's it, you can now reboot your box and gain root access again