In this video, you'll learn how to reset a lost root user password.
So on Red Hat, you do the following.
Most startup states prompt for a password, and that is why you cannot use systemd unit
is emergency.target or systemd unit is rescue.target.
You can use two options, either you use init is bin bash or you use rd.break as a kernel
boot argument from the grub prompt.
Now is bin bash is more generic of these two, and that is why I'm going to show you init
is bin bash.
rd.break is interesting, but it will boot into your init ramfs, and it's Red Hat specific,
and I rather teach you something that works on all distributions.
After getting into your troubleshooting mode using init is bin bash, you have the root
file system, but the root file system is in read-only state.
So the next command is mount-o remount,rw on slash.
That will make it read-writable, and from there, it's very easy.
You can just reset the password using passwd.
But there is one thing that you cannot forget about, and that is selinux.
Red Hat is protected by using selinux, and in order to make sure that selinux is handled
correctly, you use the command touch slash.auto-relabel to properly apply selinux context labels while
you are rebooting.
Otherwise, your system will be inaccessible.
On Ubuntu, it's a little bit easier.
You can even use systemd.unit is emergency.target, but alternatively, you can also use init is
bin bash.
It both works.
Next, you need to remount the root file system in read-write mount using mount-o remount-rw
slash, and then you simply use passwd to change the password, and you reboot, and that's all.
Let me show you, and I'm going to show you on Red Hat for the simple reason that this
is the more complex situation.
So here I'm back on my CentOS machine, and I'm going to reboot it.
Oops, and I was a little bit too eager.
I'm in my boot manager now, so what do I want in my boot manager?
Well, I want to go to CentOS Linux, and then I need my GRUB boot prompt.
Oh, no, I failed it.
Now, the trick is if you want access to the GRUB boot prompt and it doesn't work out well,
reboot your machine while it is booting.
On the next time it boots, it will detect that the last time it didn't complete booting
successfully, and you automatically get access to the GRUB boot prompt.
Then I'm pressing E for edit on my default line, and there I'm removing RHDB and QUIET,
and I'm adding in it is slashbin slash bash.
Control X to start with this option, and there it goes.
So as you can see, I'm on a root prompt, and I haven't entered any password at all.
Also, when you type mount on the top line, you can see that the root file system is mounted,
but it is RO, read only.
And that's why I need mount minus O remount comma RW on slash.
Now I can use passwd and set my new password once and twice.
But as this is a CentOS system, and CentOS, like every Red Hat family distribution, has
SELinux by default, I do need touch slash dot auto relabel.
That will make sure that SELinux context labels are going to be fixed.
Next, we need to restart.
Now I started in init is bin bash, and how do you restart from here?
Well, two options.
Either you are just going to reset your virtual machine.
That works.
What also works is if you use exec slash user lib systemd systemd.
That's kind of a funny way to start systemd, but that is because right now PID1 is the bash shell,
and systemd cannot be a child of the bash shell.
And that is why the normal fork procedure for running your processes is not valid here.
We need to replace the current bash PID1 with systemd, and that is what exec is doing.
At this moment, you can see the SELinux context labeling is happening.
You need to wait until that completes.
Once complete, it will reboot again.
That is expected behavior and nothing to worry about.
So give it a moment.
Normally on demo systems like this, it's done within a minute or so.
Here goes the reboot, and now that it is rebooting again,
well, once you have access to the prompt, you can log in as the root user.