Resetting the root password on CentOS 7 involves a few steps that require physical access to the machine or access via a virtual console if you’re working with a virtual machine or a server hosted in a data centre. Here’s how to reset the root password:
1. Reboot and Edit GRUB
- Reboot the System: Start by rebooting your CentOS 7 machine. As the system starts, you’ll need to access the GRUB menu where you can modify the boot parameters.
- Access GRUB Menu: Right after the system starts, press any key to stop the automatic booting process. You might need to press the
Shiftkey depending on your system to bring up the GRUB menu.
- Edit GRUB Parameters: With the GRUB menu on the screen, highlight the version of CentOS you wish to boot (usually the first option) and press
eto edit the boot parameters.
2. Modify Boot Parameters
- Find the Kernel Line: Look for the line that starts with
linuxand navigate to the end of this line.
- Edit Boot Parameters: Add
rd.breakat the end of the line. This parameter interrupts the boot process before the control is handed over from the initramfs to the actual system, allowing you to modify the root filesystem.
- Continue Boot: Press
Ctrl + Xor
F10to continue booting with the modified parameters. The system will boot into a single-user mode with a minimal environment.
3. Resetting the Root Password
- Remount the Root Filesystem: Once in the single-user mode, you need to remount the root filesystem with read-write permissions. You can do this by running:
mount -o remount,rw /sysroot
- Change Root into the Sysroot: Now, change the root environment to the sysroot directory, which is the root of your actual CentOS filesystem:
- Reset the Root Password: Use the
passwdcommand to reset the root password. You’ll be prompted to enter a new password twice:
- Update SELinux Information: After resetting the password, ensure SELinux context is updated by creating a hidden file
.autorelabelin the root directory. This prompts SELinux to relabel the filesystem on the next boot, maintaining security integrity:
- Exit and Reboot: Type
exittwice to exit the chroot environment and then the initial ramdisk emergency mode. Now, reboot your system by typing
Ctrl + Alt + Del. Your CentOS 7 system will reboot, and you should be able to log in with the new root password.
- Ensure you have a backup of important data before proceeding, as accessing the system in single-user mode and modifying system settings can lead to data loss if not done carefully.
- The process involves downtime, so plan accordingly if you’re working on a production server.
- If you’re using a virtual machine hosted by a cloud provider, consult their documentation as they may offer simpler ways to reset the root password through their control panel.
Resetting the root password on CentOS 7 is straightforward if you follow these steps carefully. Always maintain secure password practices to prevent unauthorised access to your systems.