Interview Prep/Linux/Packages, kernel and boot

You've forgotten the root password of a VM with console access only. How do you regain access?

IntermediateScenarioPackages, kernel and boot
Answer

Situation read → Interrupt boot and get a shell before login.

What I'd do →

  1. At GRUB press e, append rd.break (RHEL) or init=/bin/bash (others) to the linux line, boot with Ctrl+x.
  2. RHEL with rd.break:
mount -o remount,rw /sysroot
chroot /sysroot
passwd root
touch /.autorelabel      # SELinux relabel
exit; exit
  1. With init=/bin/bash: mount -o remount,rw /, passwd, then exec /sbin/init.

Why → The .autorelabel step fixes the SELinux label on /etc/shadow, or nobody can log in.

Interview tip → Mention that this is why physical/console access equals root, and GRUB passwords exist.

All Linux questions Roadmap: Linux

More on Packages, kernel and boot