Lost SSH Key: Regain Access to Your VPS
In this article
If you've lost your SSH private key, you can still regain access to your VPS by using the hosting provider's web-based console or recovery mode to reset your key or password. This article explains the practical steps to recover access, how to prevent future lockouts, and answers common questions about SSH key recovery.
Understanding SSH Key Authentication
SSH keys are a pair of cryptographic files: a private key that stays on your local machine and a public key that you place on the server. When you connect, the server verifies that your private key matches the public key. If the private key is lost or deleted, the server will reject your connection because it cannot verify your identity. This is a common scenario for developers who switch computers, reinstall their OS, or accidentally delete their .ssh directory.
Immediate Steps to Try Before Using Recovery Tools
Before you panic, try these quick checks:
- Check for backups: Look in your cloud storage, password manager, or old hard drives for a copy of the private key.
- Check other user accounts: If you have another user on the VPS with a valid key, log in as that user and edit the
~/.ssh/authorized_keysfile for your main account. - Check if password authentication is enabled: If the SSH server allows passwords, you can simply log in with your user password and generate a new key pair.
If none of these work, you'll need to use the hosting provider's recovery options.
Using Your Hosting Provider's Web Console
Most VPS providers (DigitalOcean, Linode, Vultr, AWS, etc.) offer a web-based console that acts as a virtual screen and keyboard connected to your server, bypassing SSH entirely. Here's how to use it:
- Log in to your hosting control panel.
- Find your VPS instance and look for a "Console", "VNC", or "Recovery" option.
- Open the console; you'll see a login prompt. Enter your username and password (if you have it). If you only used keys, you may need to reset the password first via the provider's "Reset Root Password" feature.
- Once logged in, edit the
authorized_keysfile:nano ~/.ssh/authorized_keys(or for root:nano /root/.ssh/authorized_keys). - Paste your new public key into this file, save, and exit.
- Test SSH from your local machine with the new key.
If you don't have a password, most providers allow you to reset it from the control panel. After resetting, you can log in via console and set up your new key.
Recovery Mode and Bootable Rescue Systems
If the console doesn't work (e.g., the server is in a boot loop or SSH is misconfigured), most providers offer a recovery mode or rescue system. This boots a temporary operating system from the provider's network, giving you access to your disk as a mounted volume. Steps generally include:
- Enable recovery mode in your provider's panel (often called "Rescue Mode" or "Recovery Console").
- Reboot the server; it will boot into a minimal Linux environment.
- SSH into the rescue system using the credentials provided (usually root and a temporary password).
- Mount your server's root partition:
mount /dev/sda1 /mnt(the device name varies; check withlsblk). - Edit the
authorized_keysfile on the mounted disk:nano /mnt/root/.ssh/authorized_keys(or for non-root users:/mnt/home/username/.ssh/authorized_keys). - Unmount and reboot back to normal mode.
This method is powerful because it works even if SSH is completely broken, as long as the disk is intact.
Generating a New SSH Key Pair
Once you regain access, you should generate a new key pair to replace the lost one. On your local machine, run:
ssh-keygen -t ed25519 -C "your_email@example.com"
This creates a new private key (default location ~/.ssh/id_ed25519) and a public key (~/.ssh/id_ed25519.pub). You'll then add the public key to the server's authorized_keys file as described above. For older systems that don't support Ed25519, use ssh-keygen -t rsa -b 4096.
Preventing Future Lockouts
To avoid this situation again, adopt these practices:
- Always keep a backup of your private key in a secure location like a password manager or an encrypted USB drive.
- Enable password authentication as a fallback, but use a strong password and consider disabling it after you've set up keys.
- Set up multiple keys: one for each device you use (laptop, desktop, phone).
- Use an SSH agent with a hardware token (like YubiKey) for extra security.
- Periodically test your recovery options (console, rescue mode) so you know they work.
When to Consider Rebuilding the Server
If you cannot access the console or recovery mode (e.g., the provider doesn't offer them, or the disk is corrupted), the last resort is to rebuild the server from a backup or reinstall the OS. This is destructive, but it's often faster than fighting a lost key. Before doing this, check if you have any snapshots or backups available in your provider's panel. If you do, you can restore a backup to a new instance and then add your new key.
Frequently Asked Questions
Can I recover my SSH private key from the server?
No. The private key is only stored on your local machine; the server only holds the public key. There is no way to reconstruct the private key from the public key. That's why backups are essential.
What if I don't have a password for the VPS?
Most providers allow you to reset the root password from the control panel. After resetting, use the web console to log in with that password and then set up a new key.
Is it safe to enable password authentication as a fallback?
It's less secure than key-only, but you can mitigate risks by using a strong password and tools like fail2ban. Many admins keep password auth enabled temporarily after a key loss, then disable it again.
Can I use a recovery mode if my provider doesn't offer one?
If your provider doesn't offer a rescue system, you can try booting from a custom ISO if the provider supports it. Otherwise, you may need to contact support or rebuild the server.
How do I add my new public key to the server if I can only access it via console?
Log in via console, then use a text editor to add the public key to the authorized_keys file. Make sure the file permissions are correct: chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys.
What if I lost the key for a non-root user?
You can log in as root (via console or password) and edit the user's authorized_keys file. Alternatively, you can use sudo if you have access to a user with sudo privileges.