How to Reset the Root Password in Linux

Linuxpassword Lock

In Linux, regular users and superusers are allowed to access services via password authentication. In the case a regular user can’t remember his/her password, a superuser can reset the password of a regular user right from the terminal. However, what if the superuser (or root user) loses his/her password? They will have to recover the lost password prior to booting into the login screen. This allows any malicious user with physical access to your Linux host to gain complete ownership. This article takes a look at how to recover a lost root password in Linux using two different methods.

Note: the method of resetting a root password is similar for most distros. Here we demonstrate the process using Ubuntu. We are also using “root password” throughout the tutorial for simplicity, but it can be interpreted interchangeably as the superuser password.

Also read: How to Easily Rename Files in Linux

1. Reset Lost Linux Root Password from the Grub Menu

1. First and foremost, to recover a lost root password, we need to restart the Linux host, assuming you forgot the password for root.

2. Once the GRUB page appears, quickly select the “*Advanced options for GNU/Linux” option by pressing the down arrow key and Enter button.

grub-advanced-options

3. Now press e to edit the commands.

You need to modify it or change it from “read-only” mode to “read-write” mode. Find the line beginning with “Linux.” Look for ro and change it to rw. Add init=/bin/bash at the end of the line.

grub-edit-kernel-loading-menu

4. Press F10. This will display a screen with a prompt.

grub-boot-bash-screen

5. Mount your root file system in read-write mode:

mount -n -o remount,rw /

6. You can now reset your lost root password by using the following command:

passwd root
grub-bash-screen-reset-password

Once you are done, type:

exec /sbin/init

This will exit the prompt and reboot the computer.

Also read: How to Mount a Windows Share Folder on Linux

2. Reset Lost Root Password Using Live CD

If you have a Linux Live CD/USB, then you can boot into it and use it to reset the root password.

1. Download the latest version of Ubuntu, and create a bootable Live CD/USB from it. Boot your system from the removable drive as opposed to your hard drive.

2. On the display screen select “Try Ubuntu.” This will bring you to the Live CD desktop.

ubuntu-live-cd-try-ubuntu

3. Open the terminal and type the following command to become root:

sudo su

4. Find the location of the hard disk partition using the following command:

fdisk -l

In most cases it will be “/dev/sda1,” although it can differ depending on how your hard disk is partitioned.

5. Mount the hard disk partition of the system to be recovered using the following command:

mkdir  /mnt/recover
mount  /dev/sda1  /mnt/recover
ubuntu-livecd-mount-partition

6. At this point we need to jail ourselves in the “mnt/recovery” directory. This means that we are pretending to be on the regular Linux filesystem. This is simply known as chrooting.

chroot  /mnt/recover

Also read: How to Delete a Directory in Linux

7. Use the following command to reset your Linux root password:

passwd root

8. Once completed, exit from the chroot shell:

exit

9. Unmount the root partition:

umount /mnt/recover

and exit your root:

exit

10. Lastly, remove the Live CD and reboot into your Linux system.

Changing the root password in Linux is easy once you get over the intimidation of the extra steps you have to take. Do note that anyone who has access to your computer can use this method to reset your superuser or root password. If you would like to be more cautious with who has access to these sorts of privileges, you should encrypt your hard disk so that it can’t be booted into or mounted easily.

Also read: 5 Better USENET Readers for Linux

Frequently Asked Questions

1. How does full disk encryption affect my ability to change my root password?

Although encryption can complicate things when you’re trying to repair a problem on Linux, this is not the case when you’re trying to change the root password. You can still do it easily from grub as described above as long as you are able to gain access to the disk.

The only difference now is that outsiders won’t be able to just slide in and do the same themselves.

2. What is the difference between root and superuser password?

In most cases, the root user is the same as the superuser. Changing the root password should also change the superuser password. In Ubuntu, the superuser is the one with User ID 0. If you have configured another user with the UID 0, then in this case, the root and superuser passwords are not the same.

3. Can I create an expiration date for passwords?

Yes, you can! Using the -x flag, you can set the lifespan for the password of a particular user. For example, typing passwd -x 30 root will make the root password valid for only 30 days. Upon expiration, you must change the password.

Additionally, you could use the -w flag to set the number of days required for an advanced warning about the need to change the password . Typing passwd -w 7 root will send you warnings about changing the root password a week prior to the deadline you previously set with the -x flag.

Want to keep digging into Linux? We have a guide for setting up Bluetooth on Linux. You can also check out our list of tools to create your very own Linux distro!

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Miguel Leiva-Gomez
Miguel Leiva-Gomez - Staff Writer

Miguel has been a business growth and technology expert for more than a decade and has written software for even longer. From his little castle in Romania, he presents cold and analytical perspectives to things that affect the tech world.