Howtos Reset A Forgotten Root Password
From 5dollarwhitebox.org Media Wiki
Contents |
Summary
This is just a reference for the beginners who will inevitably forget their root password sooner or later. This should work on any Linux distro using LILO or GRUB.
With The Boot Loader
Reboot the box Then at the boot loader do one of the following:
LILO
linux init=/bin/bash
If "linux" is not the name of your default kernel, please substitute that here.
GRUB
Grub is a little bit more detailed, but still easy:
- Type 'e' to edit the default kernel line
- Then 'e' again on the line that starts with 'kernel'
- Add 'init=/bin/bash' to the end of the 'kernel' line
- Press <ENTER>
- Type 'b' to boot it
Example Kernel Line:
kernel /vmlinuz-2.6.12.4 root=/dev/hda2 ro init=/bin/bash
Once In Bash
Once you're at a /bin/bash prompt...
Remount the filesystem read/write (will be ro when bin/bash'ing):
# mount -o remount,rw /
Then change the passwd:
# passwd root
Remount the filesystem back to read/only (keep things clean):
# mount -o remount,ro /
Then CTR-ALT-DELETE (though this will result in a kernel panic most likely). After rebooting the system and you should be good to go.
With A LiveCD
Doing this with the boot loader will not work if you've locked down the boot loader and have misplaced that password as well. If that is the case you will need a live cd such as Knoppix.
Boot to the LiveCD, and let me know once your there. . . . [OK]!
Mount The Root Filesystem
You will need to mount the root filesystem somewhere within the LiveCD. Because I am no mind reader, I cannot tell you which partition is your root partition. However, I can give you a hint... Its usually the biggest partition when looking at fdisk:
# fdisk -l /dev/hda Disk /dev/hda: 80.0 GB, 80026361856 bytes 255 heads, 63 sectors/track, 9729 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/hda1 * 1 12 96358+ 83 Linux /dev/hda2 13 1228 9767520 83 Linux /dev/hda3 1229 1352 996030 82 Linux swap / Solaris
I'm just assuming that it's hda... if using scsi this might be sda or whatever. We can see that /dev/hda2 in my example is the largest of the three partitions... so I'm just going to wing it with that one.
I generally create a directory "/mnt/root" which I will do here, and then mount the root partition:
# mkdir /mnt/root # mount -t ext3 /dev/hda2 /mnt/root
If you don't know your filesystem type and '-t ext3' doesn't work for you just try the command without the '-t ext3'.
Chroot To Root
Now we want to use the chroot command to put us into our 'root' filesystem and then we can change the password:
# chroot /mnt/root /bin/bash
This puts us into /mnt/root (so now '/' is actually /mnt/root) and gives us a '/bin/bash' shell to play with.
Change Password
Now when we execute 'passwd root' we are editting the password database found at /mnt/root/etc/shadow. Change roots password:
# passwd root
Backout And Finish
Now we are done. Cleanup and get out of there:
Exit the Chroot Jail: # exit Umount the disk: # umount /mnt/root Reboot: # reboot
Please remove LiveCD from drive.
Tada!!! You have reset root's password.
