Saturday, May 15, 2010

Resetting Root Password via GRUB / Kernel boot options

n the field, it may become necessary to reset the root password on a Linux system. There are many ways to reset an unknown root password, to include LiveCDs and booting into single user mode. This document assumes the following

* The root password is unknown
* An account with sufficient permissions to reset the root password is unknown or unavailable.
* The sysadmin has access to edit the GRUB bootloader options.
* A normal boot into single user mode doesn’t work


Perform the following steps to boot Linux to a very minimal working shell:

* At the GRUB menu, where it says “Press Any key to enter the menu”, press ESCAPE
* Next, press ‘e‘ to edit the first kernel option.
* On the following screen, highlight the ‘kernel’ line using the arrow keys and press ‘e‘
* On the next screen, which should have a line that says ‘grub edit>’ append ‘init=/bin/sh‘ and press ENTER
* Finally, press ‘b‘ to boot the system.

This process passes the init=/bin/sh option to the kernel and tells it to run /bin/sh as init instead of the normal /sbin/init. By doing this, the normal init process is completely bypassed. Normal filesystem mounting via /etc/fstab is bypassed as well as any options in /etc/inittab such as the stanza that secures single-user mode (~:S:wait:/sbin/sulogin). Only the root filesystem is mounted, and even that is mounted read-only

Now, to reset the root password:

* mount the /proc filesystem (mount /proc)
* remount the root filesystem in read/write mode (mount -n -o remount,rw /)
* Reset the root password by doing any one of the following:
o edit /etc/shadow and replace the second field of root’s entry with a known password hash
o edit /etc/shadow and remove the second field of root’s entry. This will set root’s password to a blank value.
o mount the /usr partition (if it is a separate partition) and run /usr/bin/passwd to reset root’s password.
* issue the sync command to flush all uncommitted writes to disk.
* Power down the system manually (/sbin/shutdown, /sbin/init 0 , and /sbin/reboot) will not work

This process is known to work on both Linux!!!

1 comment:

Unknown said...

good job rajat.

Faisal