How to Reset a Lost Root Password on Rocky Linux
Introduction
Losing access to the root account on a Rocky Linux server can be an issue when you need to perform administrative tasks or troubleshooting. You can regain root access by resetting the root password using the GRUB bootloader. This article will guide you through the process, which works for both Rocky Linux 8 and 9.
Resolution
There are two primary methods to reset the root password: using init=/bin/sh
or rd.break
at boot time. Both are effective, but init=/bin/sh
is generally more straightforward and works reliably across Rocky Linux 8 and 9, including virtual and physical machines. The rd.break
method is often preferred as well and works in situations where you need to perform more extensive operations within the initial ramdisk environment before the root filesystem is fully mounted-such as dealing with complex LVM configurations or troubleshooting boot time issues.
Using init=/bin/sh
This method is recommended because it is simple, does not require chrooting, and works consistently across most Rocky Linux installations:
- Reboot the server.
- At the GRUB menu, highlight the kernel you wish to boot and press
e
to edit. - Find the line starting with
linux
orlinux16
orlinuxefi
. - At the end of this line, add:
init=/bin/sh
- Press
Ctrl + x
to boot with these parameters.
-
When the shell prompt appears, remount the root filesystem as read-write:
mount -o remount,rw /
-
Reset the root password:
passwd root
-
If SELinux is enabled, ensure proper relabeling on next boot:
touch /.autorelabel
-
Reboot the system:
/usr/sbin/reboot -f
Method 2: Using rd.break
This method serves as a helpful alternative if you experience difficulties with the primary approach, or if your system uses LVM or a more complex partitioning scheme.
-
Reboot the server.
-
At the GRUB menu, highlight the kernel and press
e
to edit. -
On the line starting with
linux
, addrd.break
to the end. -
Press
Ctrl + x
to boot. -
At the
switch_root
prompt, remount the sysroot as read-write:mount -o remount,rw /sysroot
-
Change root into the sysroot:
chroot /sysroot
-
Reset the root password:
passwd root
-
Relabel for SELinux:
touch /.autorelabel
-
Exit the chroot and reboot:
exit exit
Notes
- Console or vconsole directives in the GRUB boot line (e.g.,
console=ttyS0,115200n8
) may need to be removed if you are working in a virtual machine or if you do not see the expected shell prompt. - If SELinux is enabled and you do not relabel the filesystem, you may encounter login issues after resetting the password.
- The relabel process can take anywhere from a few minutes to over half an hour, depending on your system.
- These procedures require physical or virtual console access to the server.
References & related articles
Rocky Linux System Startup Guide
Rocky Linux User Management Guide