Forgot ubuntu server password?

If you have forgotten the password to your Ubuntu Linux server account, you do not necessarily have to go back to square one and reinstall the whole operating system. It is possible to recover and reset the server password, whether it be the root user account or a normal user, even without the old password. In this tutorial, we will take you through the step by step instructions of recovering a forgotten root or normal account password on an Ubuntu server.

In this tutorial you will learn:

  • How to reset password via recovery mode
  • How to reset password via kernel boot parameters
Forgot ubuntu server password?
Forgot ubuntu server password?
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Reset password via recovery mode




If you already have terminal access, simply run the passwd command to choose a new password for the desired user. This example command would reset the password for user linuxconfig.

# passwd linuxconfig

If you do not have access to the terminal because you can’t remember your username or password and have disabled root login, then you can follow the step by step instructions below to reset your forgotten account password:

  1. Power off the machine if it is currently on. Now boot it up while holding the Shift key on your keyboard. This will load the GRUB boot menu. Select “Advanced options for Ubuntu” from the boot menu.
    Select Advanced options for Ubuntu
    Select Advanced options for Ubuntu
  2. On the next menu, load the latest kernel in recovery mode.
    Select to boot into recovery mode
    Select to boot into recovery mode
  3. Next, select to “Drop to root shell prompt.” This will allow you to execute commands as root.
    Choose to drop to a root shell prompt
    Choose to drop to a root shell prompt
  4. You can now view the /etc/passwd file for a list of usernames in case you have forgotten yours. And then you can use the passwd command to reset the user’s password.
    # cat /etc/passwd
    # passwd linuxconfig
    
    Resetting the password for a normal account user on Ubuntu server
    Resetting the password for a normal account user on Ubuntu server
  5. Alternatively you can set a password for the root user by executing passwd with no other options.
    # passwd
    
  6. Once you are done, reboot the system and you can log in to the account with the new password.
    # reboot
    


Reset password via kernel boot parameters

Resetting an account password on Ubuntu server via kernel boot parameters is a little more tricky but may be necessary if you do not have access to recovery mode. We will need to once again boot into the GRUB boot menu, but this time we will add a temporary kernel boot parameter that allows us to reset an account password.

  1. First thing you’ll need to do is reboot the machine and access the GRUB menu. This can be done by holding down the Shift key as the computer is first booting up. Once the menu appears, use your arrow keys to highlight the “Ubuntu” selection.
    Highlight the Ubuntu option
    Highlight the Ubuntu option
  2. Next, press e on your keyboard to edit the parameters.
  3. Using your arrow keys once again, scroll down a bit until you see a line that begins with linux /boot/vmlinuz.... We will need to make some small changes to this line. Use the screenshot below for reference so you can make sure you’ve found the correct line.
    This line will need to be edited, specifically the ro text which mounts the partition as read only
    This line will need to be edited, specifically the ro text which mounts the partition as read only
  4. The last part of this line is ro quiet splash $vt_handoff. We will need to replace this text with the following line. Make sure to first backspace the current settings, then type these new ones.
    rw init=/bin/bash
    

    This will give us write permissions as well as a bash shell, so we can use the usual Linux commands to change the root password.

    Add the read-write permissions along with a bash shell
    Add the read-write permissions along with a bash shell
  5. Once you have made these changes, press the F10 key to save the changes and reboot your system. You will be brought back into a bash prompt, but only on this first reboot. Subsequent machine boots will be back to normal.



  6. Your root partition should be automatically mounted, with read and write permissions. You can verify this by executing the mount command. If it’s not already mounted, use the following command below to mount it.
    # mount -n -o remount,rw /
    
    The mount command shows that our root partition has been mounted and with read and write permissions
    The mount command shows that our root partition has been mounted and with read and write permissions
  7. Now, simply use the usual passwd command to set a new root password or specify the user whose password you want to reset.
    # passwd
    
    The root password has been updated successfully by using the passwd command
    The root password has been updated successfully by using the passwd command
  8. When done, we just need to reboot the system. The usual reboot and shutdown commands will not work. Instead, execute the following command to reboot the system and load into the operating system like usual.
    # exec /sbin/init
    

That’s all there is to it. Your computer should boot up like normal, and you will be able to login to the root account (or use commands with sudo) while specifying the password you just set.



Closing Thoughts

In this tutorial, you saw how to reset the root or normal account password on an Ubuntu Linux Server. Ubuntu gives us a couple of options to perform this recovery, whether you use the recovery mode or editing kernel parameters is up to you, and you should pick whichever method is available on your system.