Services, Targets and BootLesson 4 of 4
Recovering root access: rd.break and SELinux relabel
Interrupt the boot before the real system starts, set a new root password, and make sure SELinux still lets you log in.
rd.breakmount -o remount,rw /sysrootchroot /sysrootpasswdtouch /.autorelabelrestoreconrescue.target
After this lesson you can
- Reset a forgotten root password by interrupting the boot at the initramfs.
- Explain each step: why /sysroot is read-only, why chroot, why the relabel file.
- Recognise the other recovery options (rescue and emergency targets) and when each fits.
The idea
Physical or console access beats a forgotten password: you can tell the boot loader to stop early, before the login and authentication stack starts, and edit the system directly. On RHEL the standard method is rd.break.
rd.break makes the kernel pause in the initramfs (the temporary root used to find the real one) just before it switches to the real root file system. At that moment the real root is mounted at /sysroot, read-only, and you have a root shell with no password prompt. From there:
mount -o remount,rw /sysrootbecause it is read-only until now; without thispasswdfails with a read-only file system error.chroot /sysrootso/etc/shadowand the tools refer to the real system, not the initramfs.passwd rootand type the new password twice.touch /.autorelabelbecause the initramfs has no SELinux policy loaded, so the rewritten/etc/shadowgets the wrong label. In enforcing mode, logins would then be denied with the correct password. The marker file tells the next boot to relabel every file.exitto leave the chroot,exitagain to continue booting. The relabel runs, the system reboots by itself and the new password works.
The other recovery targets from the previous lessons need the root password: rescue and emergency mode will prompt for it. If you know it, they are the tools for repairing a broken /etc/fstab or a service that prevents login. If you do not, rd.break is the only one of these that works.
Try it
This is a console exercise: do it in a virtual machine and take a snapshot first.
rd.break
Press Ctrl+x to boot. After a short time the screen shows:
switch_root:/#
mount -o remount,rw /sysroot
chroot /sysroot
sh-5.2#
passwd root
touch /.autorelabel
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
exit
exit
The machine boots, relabels the file system (this can take a minute or several on a large disk), and reboots once more. Then log in as root with the new password. Verify:
getenforce
ls /.autorelabel
Enforcing
ls: cannot access '/.autorelabel': No such file or directory
Which tool, when
| Situation | Method |
|---|---|
| Root password lost | rd.break |
| Password known, system boots but a service blocks the login | systemd.unit=rescue.target |
Password known, boot drops to a maintenance prompt (bad fstab, missing disk) | Enter the password at the prompt, fix, reboot |
| Filesystem damage | emergency.target, then fsck / xfs_repair on an unmounted device |
In the exam
This task appears often: “You cannot log in as root. Reset the password to X.” The steps are the five above, in that order. Practice until you can do it from memory in under three minutes. Type slowly: at the initramfs prompt a typo in chroot /sysroot leaves you changing the wrong system. Also check the keyboard layout when you type the new password, since the console may not use the layout you expect.
Going further
Instead of rd.break you can append init=/bin/bash, but then systemd never starts and the rest of the steps are fiddlier, so use rd.break. If the boot menu is protected by grub2-setpassword, you need that GRUB password first. On encrypted disks the LUKS passphrase is asked before the file system is available; recovery of root there still needs the passphrase. After any recovery, review last and journalctl -b for what happened before the lockout, and change any other password you suspect was known to someone else.
Practice
0 of 4 done
Do these on your own system. They get harder. Check each result, then tick it off.
- Practice
Reset the root password from the console
In your lab VM (take a snapshot first), reboot and edit the GRUB entry: append
rd.breakto thelinuxline, boot withCtrl+x. At theswitch_root:/#prompt remount/sysrootread-write,chrootinto it, set the root password toNewPass123, create the SELinux relabel marker, exit twice, and wait for the reboot. Then log in as root with the new password.Check your result
After the reboot you can log in as root with
NewPass123.ls /.autorelabelis gone (it is removed after the relabel).ausearch -m avc -ts bootshows no denials forlogin.Hint
The sequence:
mount -o remount,rw /sysroot,chroot /sysroot,passwd root,touch /.autorelabel,exit,exit. The relabel makes the first boot slower.Solution
At GRUB: press e, add rd.break at the end of the linux line, Ctrl+x switch_root:/# mount -o remount,rw /sysroot switch_root:/# chroot /sysroot sh-5.2# passwd root sh-5.2# touch /.autorelabel sh-5.2# exit switch_root:/# exit - Warm-up
Explain it to a colleague
Write one sentence each answering: why is
/sysrootread-only at that prompt, what doeschroot /sysrootchange, and what breaks if you skiptouch /.autorelabelwhile SELinux is enforcing?Check your result
Answers: the real root is mounted read-only (
roon the command line) until the system finishes booting; chroot makes/sysrootthe root sopasswdedits the real/etc/shadow; without relabel the new/etc/shadowhas the wrong SELinux context and logins are denied.Hint
Think about which process edits
/etc/shadowand what label a file created by an initramfs shell gets.Solution
1. The kernel mounts the real root read-only; it only becomes read-write once systemd is up. You remount it rw yourself to be able to write. 2. After chroot, the paths /etc/shadow etc. refer to the real system, so passwd changes the right file and uses the real PAM configuration. 3. The initramfs has no SELinux policy loaded, so the rewritten /etc/shadow gets an unlabeled/wrong context. In enforcing mode, login would then be denied. The marker file makes the next boot relabel the whole file system. - Practice
Choose the right recovery tool
For each situation say which method you would use and why: (a) the root password is known, a bad
/etc/fstabline stops boot at the maintenance prompt; (b) the root password is lost; (c) a service blocks the login prompt and you need a shell with the file systems mounted read-write.Check your result
(a) the maintenance prompt itself or
emergency.target, then edit/etc/fstab; (b)rd.break; (c)systemd.unit=rescue.target.Hint
Rescue and emergency modes ask for the root password;
rd.breakdoes not.Solution
(a) Enter the root password at the "Give root password for maintenance" prompt, remount / read-write if needed, fix /etc/fstab, then reboot. (b) rd.break: it runs before login, so no password is needed. (c) systemd.unit=rescue.target: file systems mounted read-write, few services, root shell. - Challenge
Check nothing was left in a weakened state
After the recovery, confirm that SELinux is still in enforcing mode (you did not boot with
enforcing=0), that no.autorelabelfile remains, that root can log in at the console, and thatsshdstill works. Record the commands as a short post-recovery checklist.Check your result
getenforceprintsEnforcing,ls /.autorelabelreports no such file,systemctl is-active sshdisactive, andjournalctl -b -u sshdshows no error.Hint
Recovery steps that need
enforcing=0should be avoided; the relabel file is the proper method.Solution
getenforce ls /.autorelabel 2>&1 systemctl is-active sshd sudo journalctl -b -u sshd --no-pager | tail -3 sudo ausearch -m avc -ts boot | head