Tuesday, April 26, 2011

Resetting root pass without livecd in two steps

[tested on Gentoo 2.6.36 r8]
There are cases that we forget our root password and we don't have any live CD to boot from in order to mount our file system. Is there any solution?
Yes it is. It is a two steps procedure. We will:
1. Enter to our system in a single user mode.
2. Mount our disk volumes, chroot and create a new password.

Suppose that we have a linux box with the following configuration:
Device      Boot    Start   End    Blocks   Id  System
/dev/sda1   *         1      14    105808+  83  Linux
/dev/sda2            15      81    506520   82  Linux swap
/dev/sda3            82    3876  28690200   83  Linux



Step 1:
We are going to boot to our system in a "single user mode". 
If there is no entry in your grub menu to enter in a single user mode you will have to make one as follows (otherwise goto step 2):

Boot the box. When you see the grub menu (I suppose there is one) press "e" to enter in edit mode.
We are going to replace the real_root (at 2nd line) of the main menu: 
title Gentoo Linux 2.6.31-r6root (hd0,0)
kernel /boot/kernel-genkernel-x86-2.6.36-gentoo-r8 root=/dev/ram0 real_root=/dev/sda3
initrd /boot/initramfs-genkernel-x86-2.6.36-gentoo-r8
from
real_root=/dev/sda3
to:
real_root=/bin/sh
In case that you don't have defined real_root but only root then change
from
    root=/dev/sda3
to:
    root=/bin/sh
I must say that the official way to turn the above line to boot to a single user mode is:

For Grub:
title  gentoo-single
root (hd0,0)
kernel /vmlinuz root=/dev/sda3 1
or
title  gentoo-single
root (hd0,0)
kernel /vmlinuz root=/dev/sda3 softlevel=single

For Lilo:

image=/boot/vmlinuz
  label=gentoo-single
  read-only
  root=/dev/hda3
  append="1" 
 


In my Virtual Machine, the above (official) method did not worked. This is the reason that I used "root=/bin/sh" instead.

In any case, write the 'real_root=/dev/sda3' in a safe place because we are going to set it back, later.

It is not necessary for the /dev/sda3 to exist in your machine as is. It could be /dev/sda1 or /dev/sda2... etc.
Actually, can be /dev/sdaX where X in {1,2,...N}

After this, press enter to save your changes and select to boot from this item (by selecting root(hd0,0) and pressing 'b' ).
The system will boot and after a few seconds you will see an
error message that no ROOT directory found and toy will be asked to type 'shell' to enter to a maintenance mode . 
After typing 'shell' and pressing enter this you will see the # sign.
 
Step 2:
Now, mount and chroot into the above filesystem:

# mkdir /mnt
# mkdir /mnt/gentoo
# mkdir /mnt/gentoo/boot
# mount /dev/hda3 /mnt/gentoo
# mount /dev/hda1 /mnt/gentoo/boot
# mount -t proc none /mnt/gentoo/proc
# chroot /mnt/gentoo /bin/bash

ok, just enter
# passwd
to change the root pass.
Then, just reboot the box and enter as root with your new password.
If it is necessary, don't forget to change the grub menu item to its original value, by repeating step 1 and set
real_root=/dev/sda3

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.