The cart is empty

Changing the root password is a critical operation in the administration of systems running on CentOS 7. The root password is a key security element that protects access to the highest privileges in the system. This article provides a detailed, expert-level guide on how to safely and effectively change the root password in CentOS 7.

Prerequisites

Before starting the process, ensure you have the following:

  1. Physical access to the server or remote access via SSH with root privileges or a user with sudo privileges.
  2. Basic knowledge of working with the Linux command line.

Steps to Change the Root Password

1. Log in as Root or a User with Sudo Privileges

Log into the CentOS 7 system as the root user or a user who has sudo privileges.

ssh user@your_centos7_server

2. Use the passwd Command

If you are logged in as root, you can change the password directly using the passwd command:

passwd

You will be prompted to enter the new password and confirm it.

3. Use the sudo Command (if not logged in as root)

If you are not logged in as root but have sudo privileges, use the sudo command to change the root password:

sudo passwd root

You will be prompted to enter your user password (for sudo authentication) and then to enter and confirm the new root password.

4. Verify the Password Change

To verify that the password has been successfully changed, log out and log back in as root using the new password:

su -

Enter the new root password. If the login is successful, the password change was successful.

Troubleshooting

  1. Accessing Recovery Mode

If you have forgotten the root password or do not have access to a user with sudo privileges, you will need to access recovery mode:

  • Reboot the server.
  • On the GRUB boot screen, select the recovery mode.
  • Edit the line starting with linux16 or linux and add rd.break to the end of this line.
  • Press Ctrl+X to boot into recovery mode.
  1. Mounting the Root Filesystem for Write Access

After booting into recovery mode, mount the root filesystem for write access:

mount -o remount,rw /sysroot
  1. Changing the Root Password

Chroot into the root filesystem:

chroot /sysroot

Now use the passwd command to change the root password:

passwd
  1. Exiting and Rebooting

After changing the password, exit chroot and reboot the system:

exit
exit
reboot

Changing the root password in CentOS 7 is a crucial operation for maintaining system security. This detailed guide walks you through all the necessary steps for a successful password change, whether you are logged in as root or need to use recovery mode. Following these steps will help ensure that your system remains secure and accessible only to authorized personnel.