How to Reset the Root Password in Linux

An image of a woman working in front of a laptop with the screen showing a lock icon.

In Linux, regular users and superusers are allowed to access services via password authentication. In the case a regular user can’t remember their password, a superuser can reset the password of a regular user right from the terminal. However, what if the root user loses their password? This article takes a look at how to recover a lost root password in Linux using four different methods.

Note: while the following will work on most Linux systems, it’s important to keep in mind that some distros, such as Ubuntu, disable the root user by default. For those, you can access the root shell by running sudo -s.

Reset the Root Password using Sudo

The easiest way to reset the root password is by taking advantage of a regular user that’s a currently a member of the “sudo” user group. To check whether your current user is a sudoer, use the groups command.

A terminal showing the groups command to check whether the current user can run root commands through sudo.

Note: some distros will use the group “wheel” to indicate that the current user is part of sudoers.

Once you determine that the current user can use sudo, run the following command:

sudo passwd root

Provide the new password for your root user, then type it again to confirm it.

A terminal showing the process of changing the root password using sudo.

Test whether your new root password is working properly by running su.

A terminal showing the su command testing whether the root password was changed properly.

Good to know: learn how to manage user passwords in Linux today.

Reset the Root Password Using GRUB

Apart from sudo, you can also reset the root password of your Linux system through the GRUB bootloader. This is useful if you don’t have a sudo-capable user to perform a password change from the terminal. To do this, you need to force the machine to boot in “single user mode” which automatically loads a workable root shell.

Start by rebooting your Linux system, then press the Down Arrow to prevent the machine from booting past the GRUB menu.

A screenshot of the GRUB boot menu selecting the "Advanced options for Arch Linux" item.

Press E on the GRUB menu to make a temporary change on the system’s boot script.

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.

A screenshot showing the GRUB menu with highlights on the "rw" boot option and "init" for the single user mode.

Press F10, then select the first entry in the bootloader menu. This will display a screen with a prompt.

A terminal showing the system running in single user mode.

Mount your root file system in read-write mode:

mount -n -o remount,rw /

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

passwd root
A terminal showing the process of changing the root password in single user mode.

Once done, run the following command to exit the prompt and boot into your computer:

exec /sbin/init

On a side note: interested in learning more about bootloaders? Check out our article where we compared GRUB with Systemd-boot.

Reset the Root Password using Systemd Debug

On top of using the GRUB bootloader, it’s also possible to use the Systemd Debug mode to reset your system’s root password. Begin by opening the GRUB configuration file using your favorite text editor:

sudo nano /etc/default/grub

Look for the line that contains “GRUB_CMDLINE_LINUX,” then add the following inside its quotation marks:

systemd.debug-shell
A terminal highlighting the modified GRUB config file for the Systemd Debug Mode.

Save your GRUB config file, then recreate the GRUB setup for your system:

sudo grub-mkconfig -o /boot/grub/grub.cfg

Reboot your system, then wait until you reach your machine’s login prompt.

Press Ctrl + Alt + F9 to boot into the debug root shell.

A terminal showing the Debug Mode prompt working properly.

Run passwd on your debug root shell. This will bring up a prompt where you can type and confirm your new root password.

A terminal showing the process of changing the root password in the Debug Mode.

Confirm that it’s working properly by going into a different TTY and logging in as the root user. For example, pressing Ctrl + Alt + F2 will move your session to an unused TTY in your system.

A terminal showing the confirmation that the root password change worked successfully.

Note: the Systemd Debug mode will persist until you remove the “systemd.debug-shell” value in the GRUB config file and remade the GRUB setup for your system.

Reset the Root Password Using a Live ISO

If you have a Linux Live ISO, you can also boot into it and use it to reset the root password. Start by downloading the latest version of Ubuntu, and create a bootable drive from it.

Boot the removable drive instead of your hard drive. You can do this by going into your system’s BIOS and setting the boot priority to your removable drive.

On the display screen, select Try Ubuntu. This will bring you to the Live desktop.

A screenshot highlighting the "Try Ubuntu" option for the Live ISO session.

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

sudo -s

Find the location of the root partition of your hard disk using the following command:

fdisk -l

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

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

mkdir /mnt/recover
mount /dev/vda4 /mnt/recover
A terminal showing the process of mounting the root partition in the Live ISO system.

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

chroot /mnt/recover

Use the following command to reset your Linux root password:

passwd root
A terminal showing the process of chrooting and changing the password of the root user.

Once completed, exit from the chroot shell:

exit

Unmount the root partition and exit your root:

umount /mnt/recover
exit

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

A terminal showing the changed root password working properly.

Learning how to change the root password in Linux is just the one part of keeping your machine in tip-top shape. Explore how you can further protect your system from malicious actors by encrypting your hard disk today.

Image credit: Grok via x.ai. All alterations and screenshots by Ramces Red.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Ramces Red Avatar