CentOS/RHEL 8CentOS/RHELLinux

How to reset root password in RHEL 8

Reset root password RHEL

In this tutorial, we will learn how to reset root password in RHEL 8 operating system which you may have forgotten.

What if we forgot root user password? Is it possible to reset a forgotten root user password in RHEL 8 / CentOS 8 Linux? If this is your question, then the answer is YES. It is easy to reset your root password if you have an active user account with sudo privileges. But if you don’t have access to a local user with sudo privileges and can’t recover root password via other means. This leaves you with the manual password recovery on your RHEL 8 server from the Grub boot menu.

Steps Involved to reset root password in RHEL 8:

If you don’t have access to a local user with sudo privileges and can’t recover root password via other means, you will have to do the following:

  • Interrupt the boot process
  • Edit the boot parameters
  • Reboot the system again
  • Re-mount the file system as writable
  • Reset the root password
  • Relabel SELinux

Step 1: Interrupt the boot process

a. Reboot the system and, on the GRUB 2 boot screen interrupt the boot loader sequence by pressing any key. Move the cursor to the entry that needs to be booted.

rhel boot selection -
RHEL GRUB 2 Boot Menu

b. Press the e key to edit the selected boot entry.

You will get the kernel boot parameters screen similar to below-

rhel boot parameters -
RHEL Boot Parameters

Step 2: Edit the boot parameters

a. Go to the end of the line that starts with linux. It looks like the below output:

image -
RHEL Boot Parameters Edit

b. Press Ctrl+e to jump to the end of the line.

c. Append rd.break options at the end of the kernel command line. This will break just before control is handed from the initramfs to the actual system.

image 1 -
RHEL Boot Parameters Edit 2

d. Press “Ctrl+x” and reboot the system. At this point, a root shell will be presented, with the root file system for the actual system mounted read-only on /sysroot.

rhel swith root -
RHEL Emergency Mode

Step 3: Re-mount the file system

At this point, the root file system for the actual system mounted read-only on /sysroot. Remounting the file system as writable allows you to change the password.

a. Therefore, we will re-mount the file system as writable. To do so run below command

switch_root# mount -o remount,rw /sysroot

b. Verify if the root filesystem is mounted as read-write:

switch_root:/# mount | grep -i sysroot
image 2 -
Remount /sysroot

Step 4: Reset the root password

a. Switch into a chroot jail, where /sysroot is treated as the root of the file system tree:

switch_root# chroot /sysroot

The sh-4.4# prompt appears.

image 3 -
sh-4.4# prompt

b. Set a new root password 

sh-4.2# passwd root
image 4 -
Reset root password

You will get the message: all authentication tokens updated successfully.

Step 5: Relabel SELinux 

SELinux is not yet enabled at this point, so any new files being created will not have an SELinux context assigned to them. Keep in mind that some tools (such as passwd) first create a new file, then move it in place of the file they are intended to edit, effectively creating a new file without an SELinux context.

a. To make sure that all unlabeled files (including /etc/shadow at this point) get relabeled on the next system boot. Enter below command

sh-4.2# touch /.autorelabel

b. Exit the chroot environment:

sh-4.2# exit

c. Exit the switch_root prompt:

switch_root# exit
image 5 -
exit chroot and switch_root
Wait until the SELinux relabeling process is finished. Note that relabeling a large disk might take a long time. The system reboots automatically when the process is complete.

Congratulations! You have successfully reset the root user password in RHEL 8. Now you will be able to login with root user with the password you set.

FAQs:-

1. What is GRUB 2?

GRUB2 is the latest version of GNU GRUB, the GRand Unified Bootloader. A bootloader is the first software program that runs when a computer starts. It is responsible for loading and transferring control to the operating system kernel.

2. What is Kernel?

The kernel is a computer program at the core of a computer’s operating system and generally has complete control over everything in the system. It is the portion of the operating system code that is always resident in memory and facilitates interactions between hardware and software components.

3. What is chroot?

Chroot jail is used to create a limited sandbox for a process to run in. This means a process cannot maliciously change data outside the prescribed directory tree.

4. What is sysroot in Linux?

A sysroot is a scaled down version of your target’s filesystem, it need only contain the libraries and headers which you will compile/link against. The sysroot directory works as a root for headers and libraries.

5. What is switch_root?

The switch_root is a utility used to switch from one root file system to another file system as the root of the mount tree. It is primarily used for initramfs/initrd cases.

6. What is SELinux?

SELinux stands for Security Enhanced Linux, which is an access control system that is built into the Linux kernel. It is used to enforce the resource policies that define what level of access users, programs, and services have on a system.

Reference Links:-

0 0 votes
Article Rating

Prashanth Nimesh

I'm Prashanth Nimesh, a System Administrator with over 5 years of experience in the IT field and a B.Tech in Computer Science. My passion for technology and problem-solving led me to create The Geeks Hub, where I share my expertise in Linux, Solaris, and server management. As the main contributor, I aim to simplify complex tech topics and provide practical guides to help IT professionals and enthusiasts navigate the digital landscape with confidence.
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button