How to change your forgotten root password on CentOS 7

H

I’m not used to forget things, especially important ones, such as passwords. But unfortunately, such things can happen to anyone – even to me. And I’ve been to it recently: I’ve changed the password for a CentOS 7 system just before I went on vacation with a new password, designed by another algorithm than the one I have been using for years and years, but after a short holiday, it was enough to forget about my new password.

And, of course, I was not able to remember it in the first 15 minutes after returning from vacation and neither after repeated attempts to connect to the system. But was not all lost: there’s a relatively simple way to reset the root password WITHOUT knowing the one before. This article presents a simple trick (which I tried myself) to change the forgotten password – it can be a real help for Linux system administrators with CentOS 7/Oracle Linux who can no longer access that server. The tutorial works only if there is direct access to the machine – remote access is not helpful, as the boot menu is required. The steps to be followed were recreated on a virtual machine with CentOS 7 in Virtualbox.

So, for the beginning, let’s turn on the system with CentOS 7 on which you have forgotten your password. The boot menu will appear where we need to select the desired kernel. We will press e to edit the entry with the desired kernel.

In the page that opens, we will navigate with the up-down arrow keys until we find the line containing root =/dev/maper/cl-root ro.

We will replace the characters ‘ro’ with rw init =/sysroot/bin/sh.

After making this change, press CTRL + X to enter single the single user mode. In this shell we will change the root password.

In the single user mode we will run the command below:

chroot /sysroot

In the end, we will run the command to change the root password (we will be asked to write the password twice, WITHOUT requesting the old password first):

passwd root

To update the SELinux contexts, the following command must be run at the end:

touch /.autorelabel

The exit command will reboot and you can connect to your old system with CentOS 7 using the new password you chose! The method also works on Oracle Linux (but also on all Red Hat-derived distributions).

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories