TranslateProject/sources/tech/20190104 Three Ways To Reset And Change Forgotten Root Password on RHEL 7-CentOS 7 Systems.md
darksun 9bfa0f2828 选题: 20190104 Three Ways To Reset And Change Forgotten Root Password on RHEL 7/CentOS 7 Systems
sources/tech/20190104 Three Ways To Reset And Change Forgotten Root Password on RHEL 7-CentOS 7 Systems.md
2019-02-19 17:41:22 +08:00

10 KiB
Raw Blame History

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, dont 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 cant 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 dont know or dont 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 im in single user mode and my root file system is mounted in read only mode.

It wont 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.

Screenshot for init=/bin/sh.

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 im in single user mode and my root file system is mounted in read only (RO) mode.

It wont 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

本文由 LCTT 原创编译,Linux中国 荣誉推出