Linux Labs EspañolES

Hardware and boot Labs

Reset a lost root password from the boot loader

Practitioner25 minYour VMRHEL and Debian families

Objective

Regain root access through the console without rescue media, and leave SELinux labels correct.

Requirements

  • A disposable VM with console access (Proxmox, KVM, VirtualBox). Take a snapshot first.
  • No GRUB password set, or you know it.

Setup

bash
# Optional: make the starting point realistic on the VM
sudo passwd -l root || true
getenforce 2>/dev/null || echo "no SELinux"

Steps

  1. Reboot and interrupt the GRUB menu. Edit the default entry for one boot only.
  2. RHEL family: append rd.break to the linux line. Debian and Ubuntu: replace "ro quiet splash" with "rw init=/bin/bash".
  3. Make the real root writable, set a new root password and handle the SELinux context on RHEL.
  4. Continue the boot and log in as root with the new password.

Verification

bash
sudo -k; su - -c 'id -u'
ls -Z /etc/shadow 2>/dev/null
sudo passwd -S root
Show the solution

RHEL family, after Ctrl-x with rd.break:

bash
mount -o remount,rw /sysroot
chroot /sysroot
passwd root
touch /.autorelabel
exit
exit

The next boot relabels the filesystem and reboots once. ls -Z /etc/shadow then shows shadow_t.

Debian and Ubuntu, after Ctrl-x with init=/bin/bash:

bash
passwd root
sync
exec /sbin/init

If passwd says "Authentication token manipulation error", root is still read-only: run mount -o remount,rw / first.