Expert · Boot, recovery and containers · topic 41 of 57 · 30 min
Recovering a lost root password
Explanation
When you need this
An admin leaves without handing over passwords, or a lab VM's root password is forgotten. If no one with sudo can log in, the only way back is to stop the boot before normal logins and change the password directly. You need access to the console: the physical screen and keyboard, or the virtual console of a VM or cloud instance.
Method 1: rd.break (the classic RHCSA method)
rd.break tells the initramfs to stop just before it switches to the real root, and give you a shell. At that moment the real root file system is mounted read-only at /sysroot, and SELinux is not yet active.
# 1. Reboot. In the GRUB menu, select the normal entry and press e.
# 2. Go to the end of the line starting with "linux" and add: rd.break
# (on some VMs and cloud images you must also remove console=ttyS0 arguments
# to see the shell on your screen)
# 3. Press Ctrl+X. You get a prompt:
switch_root:/# mount -o remount,rw /sysroot
switch_root:/# chroot /sysroot
sh-5.1# passwd root
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
sh-5.1# touch /.autorelabel
sh-5.1# exit
switch_root:/# exitWhy touch /.autorelabel?
passwd writes a new /etc/shadow file. Because SELinux was not running, the new file is created without the correct label (shadow_t). On the next boot, an enforcing system would block programs from reading it, and nobody could log in. The empty file /.autorelabel tells the boot to relabel the whole file system. This can take several minutes on large disks; let it finish.
Method 2: init=/bin/bash
This method replaces systemd with a bash shell for one boot. It works on any distribution and is handy when rd.break does not behave on a particular system.
# In GRUB, press e, and at the end of the "linux" line add: init=/bin/bash
# Press Ctrl+X. You get a root shell on the REAL root, mounted read-only:
bash-5.1# mount -o remount,rw /
bash-5.1# passwd root
bash-5.1# touch /.autorelabel
bash-5.1# exec /sbin/init # continue a normal boot (or: /usr/sbin/reboot -f)| rd.break | init=/bin/bash | |
|---|---|---|
| Where you stop | Inside the initramfs, before switch-root | On the real root, instead of systemd |
| Real root mounted at | /sysroot (read-only) | / (read-only) |
| Needs chroot | Yes: chroot /sysroot | No |
| SELinux relabel | touch /.autorelabel | touch /.autorelabel |
Protecting against this
The same trick lets anyone with console access become root. Defences: a GRUB password (grub2-setpassword), a firmware password and disabled USB boot, locked server rooms, restricted access to VM consoles in vCenter or the cloud portal, and full-disk encryption (LUKS) so the disk cannot be read without its passphrase.
Common mistakes
- Forgetting touch /.autorelabel: the password is changed, but logins fail after reboot with SELinux enforcing.
- Forgetting to remount read-write: passwd fails with "Authentication token manipulation error".
- With rd.break, running passwd before chroot /sysroot: you change the password inside the temporary initramfs, which is thrown away.
- Interrupting the relabel because it "looks stuck". Wait.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Recovering a lost root password · 1/7
If no one knows the root password, you can reset it from the console.
Prefer a video by a teacher? Find free videos on “rhel 9 reset root password rd.break autorelabel” on YouTube ↗
In real life: A college lab after the old admin left
A college computer centre in Bhopal inherited RHEL lab servers from an admin who had left, and no one knew the root password. With the principal's written approval, the new admin reset the password on each server from the console with rd.break, recorded the change in the lab register, and then created named sudo accounts for the two lab assistants so a shared root password would not be needed again.
switch_root:/# mount -o remount,rw /sysroot
switch_root:/# chroot /sysroot
sh-5.1# passwd root
sh-5.1# touch /.autorelabel
sh-5.1# exit
switch_root:/# exitPractice (0/3 done)
After rd.break, where is the real root file system mounted?
Which line has the bug? (rd.break method) 1: mount -o remount,rw /sysroot 2: passwd root 3: chroot /sysroot 4: touch /.autorelabel
Take a snapshot of your lab VM, then reset its root password with rd.break. Afterwards reset it again using init=/bin/bash. Time yourself.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Containers with Podman: images, run, ports and logsBoot, recovery and containers · same module
- Users, groups and the files behind themPermissions, users and sudo
- Bash scripts: variables, quoting, arguments and exit codesAutomation basics: scheduling, archives and shell scripts
- Shell basics and getting helpLinux and the shell
- Linux security basicsCyber Security: from zero to SOC analyst · Systems and cryptography
- RHCSA (EX200): what it is and how to prepareCertifications and career
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Why must you run touch /.autorelabel after resetting the password this way?
Answer: The new /etc/shadow has no correct SELinux label; the relabel fixes it
SELinux was not running when the file was written.
Which kernel argument replaces systemd with a bash shell for one boot?
Answer: init=/bin/bash
init= sets the first program the kernel runs.
passwd says "Authentication token manipulation error". Likely cause?
Answer: The file system is still read-only
Remount read-write first.
What best protects a server from this reset by someone at the console?
Answer: A GRUB password plus physical/console access control (and disk encryption)
The reset bypasses the root password entirely.
What does SELinux add to normal Linux permissions?Related
Answer: Mandatory, label-based rules that even root processes must follow
It is mandatory access control based on labels.
Which file shows the kernel arguments of the current boot?Related
Where are RHEL 9 per-kernel boot entries stored?Related
Answer: /boot/loader/entries/
BLS entries live in /boot/loader/entries/.
Which file can only root read?Related
Answer: /etc/shadow
/etc/shadow holds the password hashes, so it is readable by root only.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Boot, recovery and containers” before its exam: Containers with Podman: images, run, ports and logs, Persistent storage and containers as systemd services.
- Module 8 exam: Boot, recovery and containers · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Ansible fundamentals. 57 topics left in the course before the final exam.