How to Reset a Root Password on Linux (Ubuntu, Debian and CentOS 7)

If you need to recover the root password or a user password in Linux, this article describes how to do the recovery in Ubuntu, Debian and CentOS Linux distributions.

Ubuntu

Restart your system. Then hold down the shift key. The system should come up with a GRUB, or boot menu, with options displayed.

Select “Advanced Options for Ubuntu.”

Now select the the highest revision, followed by (recovery mode). Press Enter.

The system should display a menu with different boot options. Use the arrow keys to navigate to the option labeled root and press Enter.

The system should respond by giving you 2 options.

Press Enter for Maintenance (or press Ctrl-D to continue):

Press “Enter.”

This will give you command-line interface with a prompt. Now, we need to remount the drive with the appropriate permissions.

At the prompt, type:

mount –o rw,remount /

Press Enter. This should allow you to make changes to the data on the hard drive.

Change the Password

At the prompt, type:

passwd username

Substitute the name of the user for username, then press Enter. The system asks you to type a new UNIX password and then to retype it.

Once you’ve entered and confirmed the new password, reboot the system by entering the following:

shutdown –r now

Hit Enter, and your system should restart. Let the system come up to the login screen, and test to make sure the new password works.

Debian

Boot the system. You will see a menu. Press “e” on the keyboard. This will drop you into the Grub menu.

On the Grub editing screen, use your arrow key and move to the end of the line starting with “linux“.

There at end of this line type, type the following:

rw init=/bin/bash

Next, re-boot your system with Ctrl+X or F10.

Now we will have Debian 11’s shell with root access. Let’s check if we have read and write access to the file system where the OS has been installed.

For that use the command:

mount | grep -w /

If the output is: (rw,realtime)., then it means you have real-time- read and write access to the file system.

Let’s change the password as we do normally on a Linux system using the command terminal.

Type:

passwd

If a different user, type:

passwd user

Replace “user” with the user you are changing the password for. Enter the password 2 times.

Once you are done resetting your Linux password then restart your system to log in with the changed password. For rebooting, type:

exec /sbin/init

Hit “Enter.”

The password should be changed.

CentOS

Power on your CentOS 8 system. Select the kernel you want to boot into. Next, press ‘e’ on the keyboard to interrupt the boot process and make changes.

Locate the ro (read-only) kernel parameter as highlighted below.

Scroll down till you find the line starting with “linux16.” Here change the “ro with rw init=/sysroot/bin/sh.”

To access the system, enter the following command:

chroot /sysroot

Reset the password by the following command:

passwd root

Update the SELinux information using this command:

touch /.autorelabel

Exit the chroot and reboot your system.

exit
reboot

You may see the system reboot twice. The password should be changed.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.