How do I reset a lost password (using recovery mode requires me to type the password)?

Since you cannot access recovery mode, you'll have to change the password by accessing your installed Ubuntu system from a live CD/DVD or live USB system. What follows is a detailed walkthrough on how to do that.

This is easiest if you can already use the Ubuntu system (even without administrative access). But it's not too much harder if you can't.

  • In my experience, most Ubuntu users who end up locked out of their own systems have automatic login enabled, which is how they forget their passwords (because they don't have to type them in to log in). This may or may not be the case in your situation, but I have presented how to do this if you can use the installed Ubuntu system first because I think that will help the most people who read this post.

If You Can Use the Installed Ubuntu System, Even As a Non-Administrative User

  1. If you don't already have one, write an Ubuntu live USB flash drive (on Ubuntu, Windows, or Mac OS X), or burn an Ubuntu live CD/DVD (on Ubuntu, Windows, or Mac OS X).

  2. If you know the device name of the partition that contains your Ubuntu system's root filesystem, feel free to skip to step 5.

  3. In your Ubuntu system (not the live CD/DVD/USB system), run this command in the Terminal:

    mount | grep ' on / '
    

    You should include the spaces before on and after /.

  4. That command produces something like /dev/sda1 on / type ext4 (rw,errors=remount-ro,commit=0) as the output. The text before on (not including the space) is the device name of the partition that contains your Ubuntu system's root filesystem. Remember it (or write it down).

  5. Boot the computer from the live CD/DVD/USB and select Try Ubuntu without installing (not Install Ubuntu).

  6. Open up a Terminal window (Ctrl+Alt+T).

  7. Run this command:

    sudo mount /dev/sda1 /mnt

    Replace /dev/sda1 with the device name of the partition containing your Ubuntu system's root filesystem, if different.

    If you get an error message about how a device or partition does not exist or about an "unknown filesystem type," then you probably used the wrong device name or partition number. As explained above, your system's root partition might not be /dev/sda1.

  8. Run this command:

    sudo chroot /mnt
    

    For the purposes of being able to reset a password, or being able to change what groups users are members of, that's all you need to do to chroot in. In particular, you do not also need to mount other filesystems like /dev, /dev/pts, /sys, and /proc. More sophisticated chrooting procedures, which allow you to fully use the system you are chrooted into--for example, to update and install software inside it--do require that, as well as additional steps after entering the chroot, but this does not.

    If you run sudo chroot /mnt and you see this message, it almost always means that you mounted the wrong partition to /mnt--remember, it will not actually be /dev/sda1 on all systems--though it would also happen if you deleted /bin/bash:

    chroot: failed to run command ‘/bin/bash’: No such file or directory
    

    If that does happen, then you can unmount it with sudo umount /mnt and then proceed to mount the correct partition.

  9. Perform one of these tasks, to obtain/restore access to the install Ubuntu system.

    • If you want to reset a user's password:

      passwd username

      Replace username with your username. (This is the username on the system installed on the hard drive, and not "ubuntu" which is the username of the default user on the Ubuntu Desktop Install CD.)

      Enter the password you want for that user.

      • If you don't know your username, you can get a list of users on the system by running:

        ls /home
        

        This works because /home contains all the users' home directories, and the name of a user's home directory is the same as the name of the user.

    • Alternatively, if you want to set/reset the root password:

      Since you're in a root shell, you can use the passwd command with no arguments to reset root's password:

      passwd
      

      But please see this page, which explains why having the root account enabled is not recommended in Ubuntu.

    • Alternatively, if you want to make a user an administrator (so they can perform administrative actions including running commands as root with sudo):

      In Ubuntu 12.04 and higher (you can run lsb_release -r to see what version of Ubuntu you have, just make sure you run it in the chroot or it will tell you what version the live CD has), run:

      usermod -a -G sudo username

      In Ubuntu 11.10 and lower, administrative abilities were conferred by membership in the admin group rather than the sudo group. So then you would instead run:

      usermod -a -G admin username

      In both cases, replace username with the name of the user account you want to give admin powers.

  10. Run these three commands:

    exit
    sudo umount /mnt
    exit
    

    The last of those commands quits the Terminal window.

  11. Reboot the system by clicking the power icon on the upper-right corner of the screen and clicking Shut Down. (Then click Restart in the dialog box that comes up.) Make sure to remove the CD/DVD or USB flash drive before the system boots up again, so that you can get into your Ubuntu system on the hard disk.

    • If you're running an old enough Ubuntu live CD that there is a Restart option in the power menu, you should click that rather than Shut Down.

If You Cannot Use The Installed Ubuntu System At All

If you can't log on to obtain the device name of the partition that contains your Ubuntu system's root filesystem, you can figure it out after booting the live CD. There are several ways to do this. I present the one here that I consider easiest and least likely to lead to mistakes. (However, you may also be interested in this other method.)

  1. If you don't already have one, burn an Ubuntu live CD/DVD (on Ubuntu, Windows, or Mac OS X) or write an Ubuntu live USB flash drive (on Ubuntu, Windows, or Mac OS X).

  2. Boot the computer from the live CD/DVD/USB and select Try Ubuntu without installing (not Install Ubuntu).

  3. Open GParted.

    • To do this in Unity, which is the default desktop environment in most versions of Ubuntu, click the home button (i.e., the button at the upper-left corner of the screen with the Ubuntu logo on it) or press Super, which is also known as the Windows key. Then type in gparted. GParted will come up, and you can click it.

    • Ubuntu 17.10 and later use GNOME 3 with the GNOME Shell instead of Unity. To open GParted, click on the grid of nine dots that appears on the lower-left corner of the screen to open the application panel. Then click on the GParted icon.

      Two screenshots of GNOME 3 with the GNOME Shell in an Ubuntu 17.10 live environment, contributed by Videonauth, placed side by side, with hand-drawn markings added to show where the button is for the *applications panel*, as well as what the GParted icon looks like in the applications panel. Note that this icon is labeled GParted, so it is not necessary to recognize it by the appearance of the icon.
      Special thanks to Videonauth for explaining how to open GParted in GNOME 3, as well as for providing these screenshots.1

    • On very old versions of Ubuntu that use GNOME 2 instead of the Unity or Unity 2D interface, open GParted from the top menu by clicking SystemAdministrationGParted Partition Editor.

    • Other Ubuntu flavors, like Ubuntu MATE, Xubuntu and Lubuntu, have a similar system of nested menus, through which you can access GParted while running them from a live USB or live CD/DVD.

  4. Now you can see all your partitions graphically. If you have more than one drive, you may need to select the one that contains your Ubuntu system, in the drop-down menu at the upper-right corner of GParted.

  5. The partition that contains your Ubuntu system's root filesystem is most likely a large partition of type ext4 (or for very old Ubuntu systems, ext3). Usually there is only one ext4 (or ext3) partition, or just one big one. Otherwise, one might be the / partition (containing the root filesystem, this is what you want) and the other might be the /home partition. So if there are two large ext4 or ext3 partitions, you can assume the first one (shown farthest to the left) is probably the one that contains your system's root filesystem.

    This could possibly be wrong, but nothing will be damaged by these instructions if you are. In some other situations, outside the actions described in this post, it would not necessarily be safe to make this assumption.

  6. Remember, or write down, the device name of the partition that (probably) contains the root filesystem. GParted shows this to you. It usually takes the form /dev/sdXn where X is a lower-case letter and n is a number.

  7. Quit GParted.

  8. Follow the instructions above ("If You Can Log On as a Non-Administrative User"), starting with Step 6.

These post was originally adapted from post #9, which I wrote, in this Launchpad Answers question.

1 Credit goes to Videonauth for explaining to me how to launch GParted in an Ubuntu 17.10 live environment. I don't have GNOME 3 and would not have been able to provide those instructions otherwise. He also supplied both screenshots. (All I added were the ugly hand-drawn arrow and circle.)


If you need to change your root password but do not know the old one, you will need to drop to a root shell. When you boot into your computer, as soon as the BIOS screen disappears (the screen with your computers logo on it), start tapping the Shift key until the GRUB menu comes up, and if this option does not work, then tap the Esc key. (If it is already configured to come up then this step is not necessary.)

When you see the menu choose the option of the latest kernel (or the one you normally use) and choose the option right below called "recovery mode".

It should look like this:

enter image description here

This will take you to a menu looking something like this:

enter image description here

Next choose the Root option.

Wait and let everything load until you get to a shell. Now comes the fun. Enter the following command:

passwd your-username

Replace your-username with your actual username.

If you are unsure of your username you can look at the names of the files in your home folder via:

cd /home

Then run:

ls

This will show you the names of users on your computer.

Now type the command I stated above: passwd your-username. So that if your username is jane, I would type:

passwd jane

Next you will be presented with an option to enter your new unix password. This is where you enter the password. It will ask you to retype the command and then after two successful passwords were entered the password has been changed. Now log back into your system via

reboot

If you are asked for your root password when entering your new password this will usually help:

Alternative Method

If you get asked for the root password when entering single user mode use init=/bin/bash on the kernel append line which should boot the machine into a bash console where you can get read/write access to your /etc/shadow file. You can then either run passwd or edit the shadow file directly to put in an empty string. This allows you to reboot the machine into its normal environment and log in as root leaving the password blank and then run the passwd program to set the root password. This should really only be done when the machine is detached from any networks.

(Quote taken from Shawn Lee in an article listed below.)

To boot with init=/bin/bash as suggested, access the GRUB menu at boot time and press e to enter the editor. Move the cursor to the line starting with linux and then move the cursor to the end of that line. Enter the text

init=/bin/bash

and press F10 to boot. You should land in a root shell in text only mode. The filesystem will be mounted read only. To make the filesystem writable, enter the command

mount -o remount,rw /

You can then run

passwd username

where username is the name of the user whose password you want to set. When done, you can reboot with the reboot command.


If you need more information these links can help:

  • Recovery Mode | Ubuntu Wiki
  • How to reset your password in Ubuntu
  • How-To: Recover root password under linux with single user mode
  • How to Reset debian Root Password

If there is a root password set and you have forgotten it, but can boot normally and can use sudo try this from a terminal

sudo passwd -dl root

It is not apparent if you are getting the normal (and sudo) password and a root one confused.

The links you have for resetting passwd whould normally work.