Whether on our cell phones or our personal computers, we have all had that feeling of being locked out of a device. But what if instead of being locked out of your phone you were locked out of the tool you use to run your website or house your important data? That could cost you a lot of money in addition to the normal frustration. But fear not. In this article, we will show you how to recover your root password using Rescue Mode on VPS.
Booting Your Server in Rescue Mode
Since we don't have physical access to the server to change our root password, we will access the server using Rescue Mode. To do so, first log into the your VPS Manager. Click Virtual Private Servers. Select your VPS from the ensuing menu. Then, click the Reboot in rescue mode button from the "Boot" drop-down menu.
Once you have rebooted, it will take about three minutes for your server to be accessible again. Rescue mode credentials will be emailed to your primary email address associated with your VPS account. You will be able to SSH into the server using these credentials. Next, we will take a look at how to mount the partition and change the root password to regain access to the server.
Mounting the Partition and Changing Root Password
Once you have successfully accessed your VPS via SSH, the first step will be to mount the partition to the Rescue Mode environment. We can find out which partition we need to mount using the following command:
lsblk
Here is a sample output of this command:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 2.5G 0 disk
└─sda1 8:1 0 2.5G 0 part /
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 19.9G 0 part
├─sdb14 8:30 0 4M 0 part
└─sdb15 8:31 0 106M 0 part
This VPS has 20 GB of storage space. That means that the partition we need to mount is sdb1
since it contains 19.9 GB of storage and thus, contains the Linux filesystem. This partition will be in the /dev
directory by default.
Note: The name of your partition may be different so follow the output of your system. In this article, we will call it sdb1
.
After identifying the storage device, create a root directory within the /mnt
directory, and then use the mount
command to mount the partition to the Rescue Mode environment:
mkdir /mnt/root
mount /dev/sdb1 /mnt/root
Now, navigate to the newly mounted device and then use the chroot command to give yourself root access to the mounted filesystem:
cd /mnt/root
chroot /mnt/root
Now, we are able to change the root password using the following command:
passwd
If you wish to change the password for a different user just append the username to the end of the above command and it will change the password for that user. If you don't specify a user, as we did above, you are changing the root password by default.
Note: If you are using a Red Hat based OS (e.g., CentOS, Fedora, AlmaLinux, Rocky Linux, etc.), you will need to perform one additional step here. Run the command touch /.autorelabel
to make the OS reset SELinux contexts on boot.
Exit Rescue Mode and reboot your server. If you did everything correctly, your new password will grant you access to your live server environment.