After forgetting your CentOS username and password, there are two ways to restore access rights: Reset the root password: Restart the server, edit the kernel command line in the GRUB menu, and add "rw init=/sysroot/bin/sh ” and press Ctrl x; mount the root file system and reset the password in single-user mode. Use rescue mode: Start the server from the CentOS installation ISO image, select rescue mode; mount the root file system, copy the chroot environment from the ISO image, reset the password, exit the chroot environment and restart the server.

How to Recover Forgotten CentOS Username and Password
Get straight to the point:
When you forget your CentOS username and password, there are two methods to restore access rights:
Method 1: Reset the root password
- Restart the server and enter GRUB menu.
- Press "e" to edit the Linux kernel command line.
- Add "rw init=/sysroot/bin/sh" at the end of the line containing "ro".
- Press Ctrl x to boot into single-user mode.
- Mount the root file system:
mount -o remount,rw /
.
- Reset root password:
passwd
.
- Exit the shell and restart the server.
Method 2: Using Rescue Mode
- Boot the server from the CentOS installation ISO image.
- Select "Rescue Mode".
- Mount the root file system:
mount -o remount,rw /
.
- Copy the chroot environment from the ISO image to the root file system:
cp -ax /mnt/sysimage /
.
- Reset root password:
passwd
.
- Exit the chroot environment:
exit
.
- Unmount the file system:
umount /
.
- Exit rescue mode and restart the server.
Detailed steps:
Method 1:
- In the GRUB menu, usually press and hold Press the Shift key to enter.
- In the kernel command line, replace "ro" with "rw init=/sysroot/bin/sh".
- Press Ctrl x to boot into single-user mode.
- When mounting the root file system, make sure to mark it as writable (-o remount,rw).
- Enter "passwd" and enter the new password twice.
- Type "exit" to exit the shell, then press Ctrl d to restart the server.
Method 2:
- Rescue mode can usually be selected in the boot menu.
- When copying the chroot environment, make sure to replace "/mnt/sysimage" with the actual path from the ISO image.
- When exiting the chroot environment, run the "exit" command.
- When unmounting a file system, be sure to unmount the root file system (/).
The above is the detailed content of What should I do if I forget my centos username and password?. For more information, please follow other related articles on the PHP Chinese website!