Losing access to your server due to a forgotten or misplaced password can be frustrating. Fortunately, you can recover access by booting the server into rescue mode, mounting your file system, and resetting the password from a chroot environment.
๐ ๏ธ Step 1: Restart the Server into Rescue Mode
First, you need to boot the server into rescue mode to gain root access and modify your system files.
For Dedicated Servers
Use your hosting provider’s rescue mode feature for dedicated servers. This usually involves:
- Logging into your hosting control panel (e.g., OVH, Hetzner, etc.)
- Enabling rescue mode for the affected server
- Rebooting the server into that mode
- Accessing the server via SSH using the temporary rescue credentials provided
For VPS
Most VPS providers also allow enabling rescue mode through their control panel. After enabling it:
- Connect via SSH using the temporary credentials
- Proceed with mounting the affected partitions
Mount the Filesystem
Once logged in to rescue mode, identify the correct system partition with:
lsblk
Then mount the system partition (e.g., /dev/sda2
) to a temporary location:
mkdir /mnt/recovery
mount /dev/sda2 /mnt/recovery
If necessary, mount essential filesystems too:
mount -t proc /proc /mnt/recovery/proc
mount --rbind /sys /mnt/recovery/sys
mount --rbind /dev /mnt/recovery/dev
Enter the Chroot Environment
Now chroot into your mounted partition:
chroot /mnt/recovery
You now have shell access as if you had booted into the original system.
๐ Step 2: Reset the User Password
In the chroot shell, reset the desired userโs password:
passwd username
You will be prompted to enter and confirm the new password:
New password:
Retype new password:
passwd: password updated successfully
Note: No characters will appear when typing the password โ this is normal for Linux password prompts.
๐ Step 3: Exit and Reboot
After resetting the password:
exit
umount -l /mnt/recovery
reboot
Disable rescue mode from your hosting control panel, if applicable, and reboot the server normally.
You should now be able to log in with the new password.
โ Final Notes
- Always maintain secure, redundant methods of access (e.g., SSH key-based authentication).
- Consider setting up a secondary user account with sudo privileges as a backup.
- If you’re using LUKS encryption, make sure you have the decryption passphrase available before entering rescue mode.