How To Recover Your Lost Root Password in CentOS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

How to recover your lost root password in CentOS 12/26/23, 12:53 PM

Search 10,600+ tutorials Forum

Learn to code — free 3,00

FEBRUARY 5, 2020 / #LINUX

How to recover your lost


root password in CentOS
Thanoshan MV

In Linux, when you ADVERTISEMENT

https://www.freecodecamp.org/news/how-to-recover-your-lost-root-password-in-centos/ Page 1 of 12
How to recover your lost root password in CentOS 12/26/23, 12:53 PM

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 recover
https://www.freecodecamp.org/news/how-to-recover-your-lost-root-password-in-centos/ Page 2 of 12
How to recover your lost root password in CentOS 12/26/23, 12:53 PM

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.

https://www.freecodecamp.org/news/how-to-recover-your-lost-root-password-in-centos/ Page 3 of 12
How to recover your lost root password in CentOS 12/26/23, 12:53 PM

CentOSLinux(3.10.0-957.e17.x86_64)?(Core)
CentOSLinux(0-rescue-05a8e80b5d95487a9c5a0c600a1cc1f1)7(Core)

Interrupt the boot loader


countdown by pressing any
key.
Urethetandediketstschatedtheselection
toedittheselecteditem,or'c'foracommandprompt.
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.

https://www.freecodecamp.org/news/how-to-recover-your-lost-root-password-in-centos/ Page 4 of 12
How to recover your lost root password in CentOS 12/26/23, 12:53 PM

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

https://www.freecodecamp.org/news/how-to-recover-your-lost-root-password-in-centos/ Page 5 of 12
How to recover your lost root password in CentOS 12/26/23, 12:53 PM

system).

Then press “Ctrl+x” to save ADVERTISEMENT

those changes. The


Initramfs debug shell will
appear.

Next, we have to provide


read and write permissions
to /sysroot by typing the

https://www.freecodecamp.org/news/how-to-recover-your-lost-root-password-in-centos/ Page 6 of 12
How to recover your lost root password in CentOS 12/26/23, 12:53 PM

below command:

mount -o remount,rw /sysroot/

Now switch into chroot jail.

chroot /sysroot

https://www.freecodecamp.org/news/how-to-recover-your-lost-root-password-in-centos/ Page 7 of 12
How to recover your lost root password in CentOS 12/26/23, 12:53 PM

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.

https://www.freecodecamp.org/news/how-to-recover-your-lost-root-password-in-centos/ Page 8 of 12
How to recover your lost root password in CentOS 12/26/23, 12:53 PM

touch /.autorelabel

Finally, type “exit” twice.

The first one will exit from


the chroot jail.

https://www.freecodecamp.org/news/how-to-recover-your-lost-root-password-in-centos/ Page 9 of 12
How to recover your lost root password in CentOS 12/26/23, 12:53 PM

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


ADVERTISEMENT
know if you have any
questions. You can contact
and connect with me on
Twitter.

Thank you for reading.

https://www.freecodecamp.org/news/how-to-recover-your-lost-root-password-in-centos/ Page 10 of 12
How to recover your lost root password in CentOS 12/26/23, 12:53 PM

Happy Coding!

Thanoshan MV
System.out.println("Hey there, I am Thanoshan!");

If this article was helpful, share it .

Learn to code for free. freeCodeCamp's open source


curriculum has helped more than 40,000 people get jobs as
developers. Get started

ADVERTISEMENT

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity


organization (United States Federal Tax Identification Number: 82-
0779546)

Our mission: to help people learn to code for free. We accomplish this
https://www.freecodecamp.org/news/how-to-recover-your-lost-root-password-in-centos/ Page 11 of 12
How to recover your lost root password in CentOS 12/26/23, 12:53 PM

Our mission: to help people learn to code for free. We accomplish this
What is Programming?
by creating thousands of videos, articles, and interactive coding
Open-Closed Principle
lessons - all freely available to the public.
Compare Strings in JS
Donations to freeCodeCamp go toward our education initiatives, and
Python str() Function
help pay for servers, services, and staff.
Append to Python Dict

time.sleep() in Python
You can make a tax-deductible donation here.
Python Requirements.txt

What is a String in JS?

What's Ethical Hacking?

Get Current URL with JS

About Alumni Network Open Source Shop Support Sponsors Academic Honesty Code of Cond

https://www.freecodecamp.org/news/how-to-recover-your-lost-root-password-in-centos/ Page 12 of 12

You might also like