LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

Forgot Your Root Password? Here's the Easy Way (and the Hard Way) to Recover It

Lost Root PasswordIt happens.  No judging here.  You created a 32-character password chock full of special character goodness and you thought you’d made a note in your password manager but somehow, you didn’t.  You didn’t setup sudo or doas, and now you need to do something as root and you can’t.

Oh no!

But not all is lost.  There’s at least two ways to fix the issue.

Using Your Panel

The first thing to check is if the provider’s panel supports root password resets.  It’s almost like hosting companies get so many requests to reset root passwords that the hosting panel providers built it in to save them headaches…hmmm.

I logged into Solus on RackNerd and here is what I see:

Solus Root Password Reset

So let’s try that.

Solus Root Password Reset

After clicking change, you get this message:

Solus Root Password Reset

And then after clicking Yes:

Solus Root Password Reset

(Don’t worry – I’ll change it before this article is published!)

After a little processing, it’s successful:

Solus Root Password Reset

Now in my case, there was only one problem.  As part of my VPS setup, my Ansible scripts put this in /etc/ssh/sshd_config:

PermitRootLogin without-password

So it actually was not possible for me to login as root.  In this particular case, losing the root password didn’t actually matter because I could login with my ssh key, but you get the point.  If I’d lost the ssh key, I’d have been hosed.

Using Rescue

Let’s look at the SolusVM rescue mode:

SolusVM Rescue

Clicking that gives a warning:

SolusVM Rescue

I clicked “Enable Rescue Mode” and SolusVM politely asked me to confirm:

SolusVM Rescue

Yes, please.

After clicking Yes:

SolusVM Rescue

I let it boot a bit, and then was able to login from my PC:

$ ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@<my IP>

Whoa…what’s up with all those -o flags?  If you don’t use them, you’ll get the dramatic warning that “IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!”

The reason is that the IP is in my hosts file as associated with a certain host key, and when I boot in rescue mode, its’s a completely different OS image.  So my ssh client gets a totally different host key and throws the flag.  The -o flags I listed ignore that temporarily, without having to go in and modify the known_hosts file.

Now after logging in, it’s important to note that I am not logged into my VPS per se.  I’m logged into a rescue image.  For example:

rescue # cat /etc/hostname
rescue

If I was to run passwd root now, I’d be changing the rescue image password, which is pointless.

So we have a little work to do.  First, find the actual root partition of your VPS:

rescue # mount | grep "on / "
/dev/vdb1 on / type ext3 (rw,relatime,discard,errors=remount-ro,data=ordered)
rescue # lsblk -f
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sr0                                                      
vda                                                      
├─vda1 ext4         a09aaadc-93fd-4c32-af29-f007d7c429f6 
└─vda2 swap         ab346e4e-071f-491e-a512-2dcabaff20bd 
vdb                                                      
└─vdb1 ext3         0e8b889c-8efb-4093-9d42-817ae3ca1f90 /
rescue # 

OK, so the rescue environment is /dev/vdb1.  /dev/vda1 is my actual root.  So let’s prep a chroot environment for that.

rescue # mount /dev/vda1 /mnt
rescue # mount --bind /dev  /mnt/dev
rescue # mount --bind /proc /mnt/proc
rescue # mount --bind /sys  /mnt/sys
rescue # mount --bind /run  /mnt/run

And then enter the chroot environment:

rescue # chroot /mnt /bin/bash

Now I am in my actual VPS. For example:

root@rescue:/# cat /etc/hostname
vpn-chi-1
root@rescue:/# 

And now I can reset the root password:

root@rescue:/# passwd
New password: 
Retype new password: 
passwd: password updated successfully
root@rescue:/# exit
exit
rescue # reboot

No Comments

    Leave a Reply

    Some notes on commenting on LowEndBox:

    • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
    • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
    • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

    Your email address will not be published. Required fields are marked *