Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 99Security · RHEL

How to reset a lost root password on RHEL

Nobody knows the root password, and no other user has sudo — but you have the console (VM console, cloud serial console, iDRAC/iLO). On RHEL, Rocky and Alma you can stop the boot in the initramfs, change the password, and let SELinux fix the labels. This how-to walks through it step by step, explains the one step everybody forgets, and shows how to protect a server against others doing the same.

  • 5 Lessen
  • 11 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

99.1

The quick answer

  1. Reboot, press e at the GRUB menu, add rd.break to the end of the line that starts with linux, press Ctrl+X.
  2. At the switch_root:/# prompt:
mount -o remount,rw /sysroot
chroot /sysroot
passwd root
touch /.autorelabel
exit
exit

The server boots, relabels SELinux (some minutes, one extra reboot), and the new password works.

99.2

Step by step

  1. Open the console — this does not work over SSH. VMware/Proxmox console, cloud serial console (AWS EC2 Serial Console, Azure Serial Console), or the server's remote management card.
  2. Stop at GRUB: reboot; when the menu shows, select the normal kernel and press e. If the menu is hidden, press Esc or Shift during boot.
  3. **Add rd.break** at the end of the linux … line (use the arrow keys, End). Remove rhgb quiet too, so you see messages. Press Ctrl+X to boot.
  4. The emergency shell (switch_root:/#) runs from the initramfs; the real system is mounted read-only at /sysroot. Make it writable and enter it (quick answer).
  5. Set the password with passwd root. If you see Authentication token manipulation error, the remount did not work — check mount | grep sysroot.
  6. **touch /.autorelabel — never skip it.** passwd wrote a new /etc/shadow without its SELinux label; without the relabel nobody can log in after the reboot.
  7. Leave with exit twice. The relabel runs, the server reboots once more, then log in as root with the new password.
  8. Ubuntu/Debian: at GRUB add init=/bin/bash instead, then mount -o remount,rw /, passwd root, and exec /sbin/init (no SELinux relabel).
  9. Afterwards — protect it: give admins personal accounts with sudo (so the root password is rarely needed), store the root password in a vault, set a GRUB password, and encrypt disks of servers in unsafe places:
grub2-setpassword

99.3

Check that it worked

After the second boot, log in as root on the console (or su - from a user) with the new password, and check SELinux is still enforcing and labels are right:

getenforce
Enforcing
ls -Z /etc/shadow
system_u:object_r:shadow_t:s0 /etc/shadow

99.4

Common problems

ProblemCauseFix
Nobody can log in after the reboot/.autorelabel was skipped.Do it again and add touch /.autorelabel; or boot with enforcing=0 once, then restorecon -v /etc/shadow.
passwd: Authentication token manipulation error/sysroot still read-only, or not in the chroot.mount -o remount,rw /sysroot, then chroot /sysroot.
GRUB asks for a username and passwordA GRUB password is set.You need that password (or rescue media).
No GRUB menu appearsHidden menu / timeout 0.Press Esc/Shift early; in the cloud use the serial console.
The disk is encrypted (LUKS)Needs the passphrase first.You need the LUKS passphrase — without it, no access.
Cloud VM without console accessNo serial console.Detach the disk, attach it to a rescue VM, change the password there (and relabel).

99.5

Go further

1Which step is forgotten most often, and what happens then?

touch /.autorelabel — without it /etc/shadow has a wrong SELinux label and nobody can log in.

2How do you stop others from resetting the root password at the console?

A GRUB password (grub2-setpassword) and disk encryption for servers in unsafe places.

Alle commando's in dit hoofdstuk