How to Reset Your Lost Root Password in CentOS

In Linux, when you forget your account password, you can easily reset it using a root account. But when you forget your root account password, then you’re in a bad situation.

You can’t reset your root account password using a regular user account as a user account can’t perform such tasks in general.

In this article, we’ll be covering how to recover your CentOS root password. So let’s see how to do it.

How to reset your root password — step-by-step

In CentOS, it’s possible to have the scripts that run from the initramfs debug the shell at certain points, provide a root shell, and continue when that shell exists.

While this is mostly meant for debugging, it can also be used to recover a lost root password.

Follow these steps to recover your lost root password.

First, reboot the system.

Interrupt the boot loader countdown by pressing any key.

Move the cursor to the entry that needs to be booted.

Press “e” to select that entry. After selecting that entry, the below kernel commands will appear.

In the kernel command line, move the cursor to the line that starts with linux16.

Press the “End” key to move the cursor to the end of it. Type “rd.break” (This will break just before control is handed from the initramfs to the actual system).

Then press “Ctrl+x” to save those changes. The Initramfs debug shell will appear.

Next, we have to provide read and write permissions to /sysroot by typing the below command:

mount -o remount,rw /sysroot/

Now switch into chroot jail.

chroot /sysroot

In here, /sysroot is treated as the root of the file system tree.

Next, you’ll set a new root password.

passwd root

And relabel the files.

touch /.autorelabel

Finally, type “exit” twice.

The first one will exit from the chroot jail.

The next one will exit from the initramfs debug shell and reboots the system.

Now you can login as root with your updated password.

Please feel free to let me know if you have any questions.

Thank you for reading.

Happy Coding!

This article is originally posted at freeCodeCamp.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Microservices in Golang and creating Docker image for easy deployment.

Nebula AI 2022 April AMA Report

Native vs Hybrid App-Which one should you choose?

Single-File Executables in .NET Core 3.1 and the Quest for a Sub-50MB Docker Container

Six Factors for Better URL Path Design in Web Apps

Python3: Mutable, Inmutable… Everything is an object!

Prefer java.net.URI’s Parse than android.net.Uri’s One

DOCKER GUIDE: HOW TO INSTALL DOCKER AND WRITE DOCKERFILES

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thanoshan MV

Thanoshan MV

More from Medium

Quick Overview: Defect Life Cycle/Bug Life Cycle

How to Convert KrutiDev to Unicode — Easy Steps for Beginners

How to Convert KrutiDev to Unicode

Zero Trust is the Lynchpin of Cybersecurity- Here`s Why

Cybersecurity is the need of the hour

Make Application Modernization This Year’s Top Resolution