Need to restore root password linux on your server? We show you three simple methods for quick success!

Resetting a lost root password on a Linux system typically involves booting into a special mode (single-user or emergency mode) to gain privileged access and modify the password.

Accessing Single User Mode or Emergency Mode

The most common method involves interrupting the boot process via the GRUB bootloader.

  • Reboot your Linux system.
  • When the GRUB boot menu appears, use the arrow keys to select your desired kernel entry. Press the e key to edit the boot parameters.
  • Locate the line that starts with linux, linux16, or linuxefi. This line defines the kernel boot parameters.
  • Append one of the following parameters to the end of this line:
    • (recommended for systems using systemd with an initramfs)
    • init=/bin/bash (a more traditional method that boots directly to a bash shell)
    • (for systemd-based systems, boots into emergency mode)
  • After adding the parameter, press Ctrl+X or F10 to boot with the modified parameters.

Mounting The Root Filesystem

Once you are in the emergency shell, the root filesystem (/) might be mounted read-only or at a temporary location.

Need to restore root password linux on your server? We show you three simple methods for quick success!
  • If you used :

    The root filesystem is typically mounted read-only at /sysroot. You need to remount it as read-write:

    mount -o remount,rw /sysroot

    Then, change your root directory to /sysroot to operate on your actual system files:

    chroot /sysroot

  • If you used init=/bin/bash:

    The root filesystem (/) might already be mounted. If it's mounted read-only, remount it as read-write:

    Need to restore root password linux on your server? We show you three simple methods for quick success!

    mount -o remount,rw /

  • If you used :

    You may get a prompt asking for the root password to enter maintenance mode, or you may be dropped directly to a shell. The root filesystem should generally be mounted, but check if it's read-write. If read-only, remount it:

    mount -o remount,rw /

Changing the Root Password

Now that you have a read-write root filesystem and are in the appropriate environment (either directly or via chroot), you can change the root password.

  • Execute the password change command. If you are chrooted or already effectively root, you can simply use passwd:

    passwd

    Need to restore root password linux on your server? We show you three simple methods for quick success!

    Alternatively, explicitly specify the root user:

    passwd root

  • You will be prompted to enter the new root password and then to re-enter it for confirmation. Choose a strong password.

SELinux Considerations

If your system uses SELinux in enforcing mode, changing the password file directly (as done above) might cause context labeling issues. To instruct SELinux to relabel files correctly on the next boot, create an empty file named .autorelabel in the root directory of your filesystem.

  • While still in the chroot environment (if you used chroot /sysroot) or at the root of your filesystem:

    touch /.autorelabel

Exiting and Rebooting

After successfully changing the password and addressing SELinux (if necessary):

Need to restore root password linux on your server? We show you three simple methods for quick success!
  • If you are in a chroot environment (e.g., from /sysroot), exit it first:

    exit

  • If you used and remounted /sysroot read-write, it's good practice (though not always strictly necessary as the system will reboot) to unmount or remount it read-only:

    umount /sysroot or mount -o remount,ro /sysroot

  • Exit the emergency shell. This might involve typing exit again.

    exit

  • The system should then proceed to boot normally. If it doesn't, or if you are still at a prompt, you might need to explicitly reboot:

    reboot or systemctl reboot

Upon reboot, you should be able to log in as root with the new password you set.

Need to restore root password linux on your server? We show you three simple methods for quick success!
Share this article: