Ubuntu – How to reset a lost password (using recovery mode requires me to type the password)


I need to reset my password. I have followed these steps:

How do I reset a lost administrative password?

However, then I go to "root" or "netroot" recovery options, it tells me:

Give root password for maintenance (or type Control-D to continue)

Clearly, I do not know the root password. If I type CTRL+D, I return to the list of options. From this page I read:

Under chapter 'The Other Way':

4. Highlight the line that begins kernel and press 'e' to edit`

But in the grub configuration file I have no line that starts with kernel. Only:

setparams 'Ubuntu...'
set gxfpayload...
insmod part_msdos
insmod ext2
set root=...
search --no-floppy...
linux /boot/vmlinuz-2.6.38...
initrd /boot/initrd.img-2.6....

Those are all lines in my GRUB. Which line should I edit? Or is there another way to reset my password?

Best Answer

  • 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:


        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:

      sudo umount /mnt

      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.)