Recover – Reset Forgotten Linux Root Password

The root account, sometimes called super user, is the admin account on a Linux system, and is essential for performing all kinds of administrative tasks. You’ll need access to it in order to install or remove packages, manage other user accounts, and a lot more things. Anytime you access the root account, either through the su or sudo commands, you’ll be prompted for the root password.

If you have forgotten the password to your system’s root account, you don’t necessarily have to go back to square one and reinstall the whole operating system. It’s possible to recover and reset the root password, even without the old password. In this guide, we’ll take you through the step by step instructions of recovering a forgotten root password on Linux. This will work regardless of the Linux distribution you’re running, as long as its using the GRUB bootloader. Other bootloaders will have similar instructions.

In this tutorial you will learn:

  • How to reset a forgotten root password on Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software GRUB bootloader
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

Recover forgotten root password



Follow along with the steps below to reset your root password by entering GRUB recovery mode. In the screenshots below, we are using Ubuntu Linux for an example, but the instructions will apply to any distro. In some cases, you may have to adapt them a little.

  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 “Advanced options” selection. On some distros, it may just say the usual name of the operating system, such as “Fedora Workstation”.
  2. Highlight the advanced options, or just the name of your distro if this option does not exist

    Highlight the advanced options, or just the name of your distro if this option does not exist

  3. Next, press e on your keyboard to edit the commands.
  4. 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.
  5. 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



  6. 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. Note that on some distros, the line may be a little different, but should definitely include the ro (read only) text, which needs to be replaced.
    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.

  7. Add the read-write permissions along with a bash shell

    Add the read-write permissions along with a bash shell

  8. 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.
  9. 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 /
    
  10. 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

  11. Now, simply use the usual passwd command to set a new root password.
    # passwd
    


  12. The root password has been updated successfully by using the passwd command

    The root password has been updated successfully by using the passwd command

  13. 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 guide, we saw how to reset the root password on a Linux system, even if the original has been forgotten. Although it sounds complicated, this is a relatively simple task that GRUB can facilitate by allowing us to mount the root partition without loading into the operating system.



Comments and Discussions
Linux Forum