How To Recover a Forgotten or Lost Root Password on a Linux Server

In this tutorial, we will reset the root password of a Linux web server by booting into single user mode. This is a standard systems administration function common in DevOps or web hosting.

First, ensure that you have boot menu options access on the web server, as the root password reset process requires direct access to the Linux machine or serial terminal access.

Next, for new or existing Linux web servers, start by turning on the machine or rebooting system operations. Press any key to interrupt the boot process.

Special note: consult HostAdvice's  Best Linux hosting services page to find the leading web hosts in this category, including expert and user reviews.

By default, there are only 5 seconds for Centos 7 server to interrupt boot process. This will lead to a new admin menu where the root password can be changed.

Grub menu options

In the screenshot above, the Grub menu options are displayed. Make sure to select the default kernel version which is core. Then press ‘e’ to edit the kernel.

Search for the line starting with ‘linux16’ and replace the ‘ro’, standing for read-only to

rw init=/sysroot/bin/sh

which means read-write and start into the bash shell.

Highlight "ro"

Highlight "rw init=/sysroot/bin/sh"

Then press ‘Control+X’, to start in single user mode using the bash shell.

Special Note: While in single user mode, you are automatically logged as root user but with no password requirement.

Single user mode

While in single user mode, change the password:

# chroot /sysroot
# passwd root

After creating the password, you must update Selinux parameters:

# touch /.autorelabel

Otherwise, it’ll be impossible to login until you reboot and repeat all these instructions and include the autorelabel. This mainly applies for selinux in enforcing mode or freshly installed Centos 7.

Special Note: For freshly installed Centos 6, it may be tricky to reset root password with enforcing selinux. To go around this, boot with "selinux=0" appended to the grub kernel line, or do "setenforce 0" after logging in and before attempting to reset the password.

Now, exit and reboot the system.

# exit
# reboot

Change root password

Successful root login

You can now successfully login into your Linux server using the recently created root password in single user mode.

Check out the top 3 Linux hosting services

Was this article helpful?