sources/tech/20190104 Three Ways To Reset And Change Forgotten Root Password on RHEL 7-CentOS 7 Systems.md
10 KiB
Three Ways To Reset And Change Forgotten Root Password on RHEL 7/CentOS 7 Systems
If you are forget to remember your root password for RHEL 7 and CentOS 7 systems and want to reset the forgotten root password?
If so, don’t worry we are here to help you out on this.
Navigate to the following link if you want to reset forgotten root password on RHEL 6/CentOS 6.
This is generally happens when you use different password in vast environment or if you are not maintaining the proper inventory.
Whatever it is. No issues, we will help you through this article.
It can be done in many ways but we are going to show you the best three methods which we tried many times for our clients.
In Linux servers there are three different users are available. These are, Normal User, System User and Super User.
As everyone knows the Root user is known as super user in Linux and Administrator is in Windows.
We can’t perform any major activity without root password so, make sure you should have the right root password when you perform any major tasks.
If you don’t know or don’t have it, try to reset using one of the below method.
- Reset Forgotten Root Password By Booting into Single User Mode using
rd.break
- Reset Forgotten Root Password By Booting into Single User Mode using
init=/bin/bash
- Reset Forgotten Root Password By Booting into Rescue Mode
Method-1: Reset Forgotten Root Password By Booting into Single User Mode
Just follow the below procedure to reset the forgotten root password in RHEL 7/CentOS 7 systems.
To do so, reboot your system and follow the instructions carefully.
Step-1:
Reboot your system and interrupt at the boot menu by hitting e
key to modify the kernel arguments.
Step-2:
In the GRUB options, find linux16
word and add the rd.break
word in the end of the file then press Ctrl+x
or F10
to boot into single user mode.
Step-3:
At this point of time, your root filesystem will be mounted in Read only (RO) mode to /sysroot. Run the below command to confirm this.
# mount | grep root
Step-4:
Based on the above output, i can say that i’m in single user mode and my root file system is mounted in read only mode.
It won’t allow you to make any changes on your system until you mount the root filesystem with Read and write (RW) mode to /sysroot. To do so, use the following command.
# mount -o remount,rw /sysroot
Step-5:
Currently your file systems are mounted as a temporary partition. Now, your command prompt shows switch_root:/#.
Run the following command to get into a chroot jail so that /sysroot is used as the root of the file system.
# chroot /sysroot
Step-6:
Now you can able to reset the root password with help of passwd
command.
# echo "CentOS7$#123" | passwd --stdin root
Step-7:
By default CentOS 7/RHEL 7 use SELinux in enforcing mode, so create a following hidden file which will automatically perform a relabel of all files on next boot.
It allow us to fix the context of the /etc/shadow file.
# touch /.autorelabel
Step-8:
Issue exit
twice to exit from the chroot jail environment and reboot the system.
Step-9:
Now you can login to your system with your new password.
Method-2: Reset Forgotten Root Password By Booting into Single User Mode
Alternatively we can use the below procedure to reset the forgotten root password in RHEL 7/CentOS 7 systems.
Step-1:
Reboot your system and interrupt at the boot menu by hitting e
key to modify the kernel arguments.
Step-2:
In the GRUB options, find rhgb quiet
word and replace with the init=/bin/bash
or init=/bin/sh
word then press Ctrl+x
or F10
to boot into single user mode.
Screenshot for init=/bin/bash
.
Step-3:
At this point of time, your root system will be mounted in Read only mode to /. Run the below command to confirm this.
# mount | grep root
Step-4:
Based on the above ouput, i can say that i’m in single user mode and my root file system is mounted in read only (RO) mode.
It won’t allow you to make any changes on your system until you mount the root file system with Read and write (RW) mode. To do so, use the following command.
# mount -o remount,rw /
Step-5:
Now you can able to reset the root password with help of passwd
command.
# echo "RHEL7$#123" | passwd --stdin root
Step-6:
By default CentOS 7/RHEL 7 use SELinux in enforcing mode, so create a following hidden file which will automatically perform a relabel of all files on next boot.
It allow us to fix the context of the /etc/shadow file.
# touch /.autorelabel
Step-7:
Finally Reboot
the system.
# exec /sbin/init 6
Step-9:
Now you can login to your system with your new password.
Method-3: Reset Forgotten Root Password By Booting into Rescue Mode
Alternatively, we can reset the forgotten Root password for RHEL 7 and CentOS 7 systems using Rescue mode.
Step-1:
Insert the bootable media through USB or DVD drive which is compatible for you and reboot your system. It will take to you to the below screen.
Hit Troubleshooting
to launch the Rescue
mode.
Step-2:
Choose Rescue a CentOS system
and hit Enter
button.
Step-3:
Here choose 1
and the rescue environment will now attempt to find your Linux installation and mount it under the directory /mnt/sysimage
.
Step-4:
Simple hit Enter
to get a shell.
Step-5:
Run the following command to get into a chroot jail so that /mnt/sysimage is used as the root of the file system.
# chroot /mnt/sysimage
Step-6:
Now you can able to reset the root password with help of passwd command.
# echo "RHEL7$#123" | passwd --stdin root
Step-7:
By default CentOS 7/RHEL 7 use SELinux in enforcing mode, so create a following hidden file which will automatically perform a relabel of all files on next boot.
It allow us to fix the context of the /etc/shadow file.
# touch /.autorelabel
Step-8:
Remove the bootable media then initiate the reboot.
Step-9:
Issue exit
twice to exit from the chroot jail environment and reboot the system.
Step-10:
Now you can login to your system with your new password.
via: https://www.2daygeek.com/linux-reset-change-forgotten-root-password-in-rhel-7-centos-7/
作者:Prakash Subramanian 选题:lujun9972 译者:译者ID 校对:校对者ID