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

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

FastComet
£1.41 /mo
Starting price
Visit FastComet
Rating based on expert review
  • User Friendly
    4.7
  • Support
    5.0
  • Features
    4.8
  • Reliability
    4.5
  • Pricing
    5.0
Kamatera
£3.15 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
    3.5
  • Support
    3.0
  • Features
    3.9
  • Reliability
    4.0
  • Pricing
    4.3
HostArmada
£1.96 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0

Part One: How to Monitor Nginx using Elastic Stack on a CentOS 7 VPS or Dedicated Server

This is Part I of a two-part tutorial explaining how to use the different compon
5 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Install Apache Cassandra on an Ubuntu 18.04 VPS or Dedicated Server

This tutorial will help you install and configure Apache Cassandra (an opensourc
3 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Host Multiple Websites on an Ubuntu 18.04 VPS or Dedicated Server

This article will show you how to host two or more websites on a single Virtual
5 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Configure Nginx and Apache on the same Ubuntu VPS or Dedicated Server

Nginx and Apache are great and powerful web servers. However, they both have dra
2 min read
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top