Table of Contents
Forgetting your CentOS 9 password can be frustrating, but you can regain access by following a few simple steps. This guide explains the process clearly, safely, and efficiently.
STEP 1: Reboot Your System and Enter GRUB Menu
To reset your password, start by rebooting your system. As the system starts, access the GRUB menu by pressing Esc or Shift (depending on your setup) during boot.
# Reboot the system
sudo reboot
STEP 2: Edit GRUB to Enter Single-User Mode
To gain root access for password reset, you need to edit the GRUB boot entry and boot into single-user mode. This allows you to safely modify the root password without logging in normally.
# In the GRUB menu:
# 1. Highlight your CentOS boot entry and press 'e' to edit.
# 2. Find the line starting with 'linux' and append 'rd.break' at the end.
# 3. Press Ctrl + X to boot into emergency mode.
STEP 3: Remount Root Filesystem as Read/Write
Before changing the root password, you must remount the root filesystem with read/write permissions. By default, it’s mounted as read-only in emergency mode, so this step ensures changes can be applied safely.
# Remount the root filesystem with read/write access
mount -o remount,rw /sysroot
# Switch to the actual root filesystem
chroot /sysroot
STEP 4: Reset the Root Password
Once the root filesystem is writable, you can safely reset the root password. This step ensures you regain full administrative access to your CentOS 9 system.
# Set a new root password
passwd root
# Follow the prompts to enter and confirm your new password
STEP 5: Relabel SELinux Contexts
After resetting the root password, it’s important to relabel SELinux contexts to ensure proper system security and functionality. This prevents potential permission issues when accessing files and directories.
# Create an autorelabel flag for SELinux
touch /.autorelabel
STEP 6: Reboot the System
Once all changes, including password reset and SELinux relabeling, are completed, rebooting the system ensures that settings take effect and the system starts in a stable state.
# Reboot the CentOS system
reboot
STEP 7: Log In with Your New Password
After rebooting, use the newly set password to log in to your CentOS system. This confirms that the password reset was successful and that you have full access to your user or root account.
# Log in normally with your updated credentials
ssh username@server_ip
