If you have some old CentOS 7 Virtual machine hanging for a long time and you don't remember the root password or you don't remember where you have stored it, but you have something important as data left over, you might need to recover root password for your CentOS 7 Virtual Machine.
I recently had to resolve that issue and here is the few easy steps to take to recover the lost root password.
Assuming you have tried to boot the VM and the VM boots fine and your few attempts to input manually some default passwords of yours failed, next
1. Reboot the Virtual Machine to the GRUB boot menu
The GRUB boot screen should appear and be there for few secs
2. Edit the boot loader kernel options ( add add rd.break enforcing=0 )
Press 'e' to Edit the boot loader and modify the boot commands options passed to the linux kernel.
In GRUB edit mode:
add rd.break enforcing=0
to the end of the line starting with linux at the end of passed parameters list as shown in the picture.
When done editing, press Ctrl-x (Control button x key simultaneously) to boot with changed parameters.
ALTERNATIVE WAY TO BOOT THE SYSTEM INTO ROOT WITHOUT PASSWORD PROMPT:
Alternative options to use instead of add rd.break.enforcing=0 are to substitute the rhgb quiet kernel option with init=/bin/bash
As you might wonder for the meaning of the passed 2 parameters:
rd.break breaks the boot process at initramfs while
enforcing=0 disables the SELinux (which often enabled by default on CentOS).
Another way is to
3. Boot in CentOS emergency mode and Reset the root password
When done editing, press Ctrl-x to boot with changed parameters.
As you might wonder for the meaning of the passed parameters:
rd.break breaks the boot process at initramfs while
enforcing=0 disables the SELinux (which often enabled by default on CentOS).
Whence system boots up with the modified kernel options cmd, the switch_root prompt will appear.
As the emerency mode boots the filesystem into read-only mode under /sysroot default directory, in order to be able to
modify the MD5 root password stored hash inside RO mounted /sysroot/etc/shadow you need to remount the Filesystme
in read-write mode.
To Remount the read-only file system /sysroot in write mode:
# mount -o remount,rw /sysroot
As the /sysroot is not the root directory to be able to use a standard passwd command you need to make /sysroot
as the default root folder for the booted linux by chrooting into it.
- Generate MD5 password manually (for Hardcore masochistic admins 🙂 )
If you're a hard core linux sysadmin of course, generate your own new md5 password and directly modify /etc/shadow copy pasting the md5 string.
If you want to manually generate the md5 string, you can do it depending on the required encryption algorithm with:
For (md5, sha256, sha512) encrypted pass
# openssl passwd -6 -salt xyz yourpass
For (md5, sha256, sha512) encrypted pwd
# mkpasswd –method=SHA-512 –stdin
For (des, md5, sha256, sha512) encrypted pw
# perl -e 'print crypt("YourPasswd", "salt", "sha512"),"n"'
Once the string is generated;
# vim /etc/shadow
and exchange the old with new string for MD5
- Change password with chroot (the easy common way)
# chroot /sysroot
That should drop you into another shell bash-4.x
# passwd
Changing password for user root.
New password:
Retype new password:
We need have to sync the entire filesystem we have to use the sync command, for novice sys admins who never heard about this command, below
short description:
The Linux sync command synchronizes cached data to permanent storage.
This data includes modified superblocks, modified inodes, delayed reads and writes, and others. sync uses several system calls:
sync()
syncfs()
fsync()
fdatasync()
For example, the sync command utilizes the sync() system call to write all buffered modifications to file data and metadata to an underlying storage device.
As a Linux systems administrator or developer, understanding the sync command can be crucial for efficient file synchronization. Additionally, sync can be helpful after crashes or when the file system becomes corrupted.
In this tutorial, we’ll explore the various aspects of the sync command. Also, we’ll see how we can use sync in different scenarios.
# sync
# exec /sbin/init
Try out the root password after booting normally into CentOS and the new set administrator pass should work.
Resetting forgotten (lost) root password on CentOS 6
The process is absolutely the same except on the Step 1 (in the modification of GRUB boot menu by pressing e key), add to
rhgb quiet
at the end one 'S'
This S character means 'boot CentOS into Single user mode'
rhgb quiet S
Then, press ENTER key and press b key to boot CentOS 6 into to single user mode.
More helpful Articles
Tags: about, absolutely, administrator, Admins, after, algorithm, ALL, com, command, default passwords, editing, filesystem, linux kernel, New, password, Reboot, Recover, root password, sync, www