Saturday, September 16, 2023

How to Reset the Root Password in RedHat/CentOs 8 from Emergency Mode


Introduction

In the world of Linux system administration, there may come a time when you forget the root password for your Red Hat 8 server. While this can be a daunting situation, there's no need to panic. Red Hat provides a robust set of tools that allow you to reset the root password even if you can't log in. In this blog post, we'll walk you through the process of resetting the root password in Red Hat 8 from emergency mode.


What is Emergency Mode?

Emergency mode is a special boot mode in Red Hat and other Linux distributions that provides a minimal environment to repair or recover a system when it's in an unusable state. It allows you to gain access to the system's filesystem, even if you don't know the root password. This makes it the perfect starting point for resetting the root password.

Note: This guide assumes you have physical or console access to the server or virtual machine. If you're working on a remote server without console access, you may need to contact your hosting provider or data center for assistance.


Here's how to reset the root password in Red Hat 8 / CentOS 8 from emergency mode:


1. Boot into Emergency Mode:

   - Start by rebooting your Red Hat 8 server or virtual machine.

   - When the GRUB bootloader menu appears during boot, select the Red Hat 8 entry you want to boot into.

   - Press the `e` key to edit the boot parameters.


2. Edit the Boot Parameters:

   - Locate the line that starts with `linux` or `linuxefi`.

   - Append `rd.break enforcing=0` to the end of the line. This will boot the system into emergency mode without enforcing SELinux policies.

   Example:

 linux /vmlinuz-4.18.0-305.el8.x86_64 root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet rd.break enforcing=0

   

3. Mount the Filesystem:

   - Press `Ctrl` + `x` or `Ctrl` + `b` to boot into the modified kernel command line.

   - The system will boot into emergency mode, and you'll be dropped into a shell. The root filesystem is mounted as read-only.

   To remount it as read-write, run the following command:

 #mount -o remount,rw  /sysroot


4. Reset the Root Password:

   - Change the root password using the `passwd` command:

 #chroot /sysroot

 #passwd

   You will be prompted to enter a new root password.


5. SELinux Relabeling:

   - To ensure SELinux labels are correctly applied, execute the following command:

 #touch /.autorelabel

6. Exit Emergency Mode:

   - Exit the emergency shell:

 #exit

   

7. Reboot Your System:

   - Reboot the system:

reboot


After rebooting, your Red Hat 8 system should have the root password reset. You can now log in with the new password you set during step 4.


Conclusion

Forgetting the root password can be a stressful situation, but with Red Hat's rescue mode, you can regain access to your system and reset the password securely. Remember to keep your passwords in a secure place to prevent this from happening in the future. Always practice good password hygiene and consider using a password manager to keep track of your credentials.

No comments:

Post a Comment

Installing Jenkins on RHEL 8 is a straightforward process. Here's a step-by-step guide

Introduction  Jenkins is an open-source automation server widely used for continuous integration (CI) and continuous delivery (CD) pipelines...