Reset root password in Ubuntu (or Debian)

Posted by | Posted in Command-Line, Guides, Linux, Ubuntu | Posted on 28-07-2009

When you lose/misplace/forget your root password on a RedHat-based linux distro, resetting the root password isn’t too difficult. I’ve added those instructions at the bottom in case you are interested.

In Debian distros (which includes Ubuntu) you are required to have the root password in order to change the root password (even when you boot into single user mode, as you would with Redhat versions). While this adds a level of security, it also adds a level of annoyance when you have lost your password and are in a pinch.

Not to worry though, there is a way around (albeit, not as easy).

The first steps are the same for either Redhat distros or Ubuntu. Reboot the server, and get into the boot menu by pressing ESC to enter the menu:

ESC to enter the menu

Next you’ll need to choose which kernel you’re going to edit (it doesn’t really matter, but I usually choose the first in the list that is *not* recovery mode):

Select kernel to edit

Type ‘e’ to edit that line, and it will take you to another menu, which brings you to the line you’ll edit:

Edit kernel line

Scroll down to the ‘kernel’ like, and hit ‘e’ again. This will insert the kernel line into the grub> prompt, where you can add/edit the boot instructions. Yours will not be identical to the example below, but should have the same components:

 kernel /boot/vmlinuz-2.6.17-11-386 root=UUID=7d2d4dd5-f7fd-45ad-8bc9-51b1b284fcaa ro quiet splash

You will want to edit that to :

 grub> kernel /boot/vmlinuz-2.6.17-11-386 root=UUID=7d2d4dd5-f7fd-45ad-8bc9-51b1b284fcaa init=/bin/bash

When finished, hit enter to go back to the previous screen, and hit ‘b’ to boot that newly edited line.

After it boots, you’ll be at a shell prompt, but it isn’t writable! So any changes you make (like changing the root password) won’t hold through the reboot. So you’ll need to mount your root directory (which is simply labeled: / ) by typing the following command:

mount -o remount,rw /

Be sure to note the spacing in that command

Next you’ll need to change the root password. Since you are in the root prompt, it will change the password for user ‘root’ by default. I’ve added the root user below so you can see how to change other user passwords as well (simply replace ‘root’ with another user):

 passwd root

Enter the new password (one that you’ll remember, but is sufficiently secure).

It should reboot as it is now, but if you want to be safe you can reset the drive to read only:

mount -o remount,ro /

Then type:

 shutdown -r now

When your server reboots, you should now be able to log in as root with your newly set password. Granted, this will only work if you are using the cli (or have already set to allow root login through the settings in gnome/kde).

To perform this same task on a RedHat linux distro (RedHat, Fedora, CentOS, etc.) you follow the same instructions as above, except you add the word ‘single’ instead of ‘init=/bin/bash’ from above. Then continue to boot.

When you boot that kernel, it will drop you into a root prompt, at which point you can type passwd to change your password (with passwd, just like above). Next time you reboot your server, you will have root access with your newly formed password.

Comments posted (2))

  1. Hi Buddy,

    I like this article very much.
    Very well documented and ilustrated.
    Although, it didn’t work for me…. i followed all the steps but the promp simple can’t find the passwd command and it is not inside the /bin folder…..but i must say it gives an warning in the prompt just after login that the groups were not found or something is wrong with the groups….do you know what mught happening here?

    Best regards and keep up the good work.

  2. Hey there — glad you enjoyed it.

    As to the problem you encountered, are you sure you typed the full line:



    The passwd command is in /bin/bash (not just /bin).

    As for the groups issue, I’m not sure what is up with that. Once you get logged in, you should be able to sort out the groups issue easily enough (thru the gui or command line, whichever you’re more comforable with).

    That’s a good idea for another tutorial though!

    Let me know how it goes.

Join the mailing list

Check your email and confirm the subscription