Tuesday, May 14, 2013

How to recover (reset) root password on Linux

Reset password before reboot

First of all, it's a good practice to reset root's password before you're going to reboot the host after doing some changes or if system was running a long time without reboot.
If it fail to boot you might be asked to provide root password for fsck or even if you try to boot in single mode.

Reset root password in single user mode

If you're lucky, and system is using default settings you may try to boot up in single user mode (hit ESC when you see grub menu, choose the kernel you want to boot, hit 'e' and add 'single' at the end of the line which starts with 'kernel...' word).
You should get a root shell (without asking you for password).
Then type:
and reboot the box.

Reset root password using init=/bin/bash

You may find your system more secure and configured to ask for password even in single user mode. In that case reboot the box and add init=/bin/bash to the line starting with 'kernel...' word.
System will boot up and launch a bash session for you.
Now, mount the root's filesystem in rw mode:
mount -o remount,rw /
and reset root's password:
Finally reboot the box and you should be able to login using new password.

How to setup single user mode to ask for password

If you would like to protect your system from allowing bad guys to logon in single user mode without password it may be easily fixed.

in RHEL5/CentOS5:

Edit /etc/inittab and add the following entry at the end of file:

in RHEL6/CentOS6:

Edit /etc/sysconfig/init file and change the SINGLE variable to:

No comments:

Post a Comment