How to reset root password on CentOS 7 / RHEL 7

question: How to reset root password on CentOS 7 / RHEL 7? I will guide you through the steps to reset the forgotten root password on CentOS 7 / RHEL 7.

For RHEL 8 / CentOS 8, please check: How to reset root password in RHEL 8

In order to be able to reset the root password on a CentOS 7 / RHEL 7 server, you need to:

  • Operating system-only root password unknown
  • GRUB bootloader working

Please note that CentOS 7 and RHEL 7 are included with GRUB version 2. It is not possible to reset the root password in single user mode supported by RHEL 6.x and CentOS 6.x. The root password can now be run in single-user mode and emergency mode.

How to reset root on CentOS 7 / RHEL 7

Use the following steps to reset the root password on CentOS 7 / RHEL 7 server

How to reset the root directory on CentOS 7 / RHEL 7: Method 1:

  1. Boot the system, then on the Grub2 boot menu screen, press e on the keyboard to enter.Grub Edit Mode
  2. press”Ë“, Scroll down to linux16 line and delete rhgb, quiet and Lang parameter. To easily move to the end of the line, press ctrl + e, then to the front of the line, press ctrl + a. The Linux16 series on UEFI systems will be linuxefi.
  3. In the case of UEFI systems, it is at the end of line linux16 or linuxefi. Add the following lines:
    rd.break 

    The whole line would look like:

    linux16 /vmlinuz-3.10.0-327.el7.x86_64 root=/dev/mapper/centos-root 
     ro rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rd.break
  4. To start the system with the parsed new parameters, press ctrl + x, which will take you to the prompt window.
  5. Mount the file system as read-write. If the file system is not writable, the password cannot be changed. You can reinstall by typing the following command.
# mount -o remount,rw  /sysroot

7. Chroot enters / sysroot

# chroot /sysroot

8. Change password now

# echo password  |  passwd --stdin  root
  • Replace Password Use the password you want to use
    You can learn how to add new users to your Linux system here

9. Set SELinux relabel on next boot

# touch /.autorelabel

10.Resume initialization and restore the system by

# exit# exit

See the caption below.

How to reset root on CentOS 7 / RHEL 7: method 2

1. Another method is to set mount to rw. The last line of linux16 contains:

rw init=/bin/bash

The complete line looks like this:

linux16 /vmlinuz-3.10.0-327.el7.x86_64 root=/dev/mapper/centos-root 
 ro rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rw init=/bin/bash

2. Press Ctrl + X , You will be prompted Slam Terminal window.

3. Change password now

# echo password  |  passwd --stdin  root
  • Replace Password Use the password you want to use

4. Set SELinux relabeling on next boot

# touch /.autorelabel

5. Restart by typing recovery exec / sbin / init Complete password reset.

# exec /sbin/init

You will now be able to log in with your new password.

Sidebar