Skip to content
Tarisu Limited

Lost SSH Key Recovery: Regain Server Access and Set Up a New Key Pair

A practical recovery guide for server owners who lost the private SSH key: use a provider console or rescue mode, install a fresh public key, and verify login without reinstalling.

Guides 10 min read Tarisu Limited

Lost SSH Key Recovery: Regain Server Access and Set Up a New Key Pair

This guide is for the server owner who lost the private half of an SSH key pair and can no longer log in to a Linux VPS or dedicated server. It assumes you still have provider account access and can open a console, VNC window, rescue mode, or physical/IPMI console. If the key was compromised rather than merely lost, revoke it and rotate credentials as soon as possible. This article covers only machines you own or administer.

The steps target Ubuntu 22.04 and 24.04, Debian 12, AlmaLinux 9 and Rocky Linux 9. The client commands also work on Windows 10 and 11 with the built-in OpenSSH client. Windows Server 2019 and 2022 with OpenSSH Server uses a different default location for administrator keys; the recovery principle is the same, but confirm the path before editing files.

What you need before you start

  • Access to your provider account, or physical access to the server.
  • A console, VNC, rescue mode, IPMI/iLO/iDRAC, or equivalent local access method.
  • The server hostname or IP address and the username you normally use.
  • A local computer with OpenSSH: Windows 10/11, macOS, or Linux.
  • A password manager for the new private key and passphrase.
  • A current backup of the SSH files you are about to change.

Keep the console session open until the new key has been tested from a second terminal. A recovery key is not a reason to enable password authentication, weaken sshd settings, or disable the firewall.

Step 1: Open a console you still control

Use the console or VNC option in your provider account area. If you run a Tarisu VPS or dedicated server, use the console/VNC option in your Tarisu account area. If the normal console is unavailable, use the provider's documented rescue mode or the physical/IPMI console. Do not reboot unless the rescue procedure requires it.

If you have neither console nor rescue access, you are locked out of the SSH service itself. Contact your provider or the hardware support channel. A new key cannot be installed over SSH when sshd is already rejecting the old key.

Step 2: Confirm your identity and back up SSH state

First confirm who you are:

id
whoami

If you are logged in as root, back up the sshd configuration and root authorized keys file:

sudo cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%Y%m%d%H%M%S)
install -d -m 700 /root/.ssh
cp -a /root/.ssh/authorized_keys /root/.ssh/authorized_keys.bak.$(date +%Y%m%d%H%M%S) 2>/dev/null || true

If you are logged in as your normal sudo user, back up that user's files instead:

sudo cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%Y%m%d%H%M%S)
install -d -m 700 ~/.ssh
cp -a ~/.ssh/authorized_keys ~/.ssh/authorized_keys.bak.$(date +%Y%m%d%H%M%S) 2>/dev/null || true

The backup command may print an error if authorized_keys does not exist yet. That is normal. Do not delete the original file; the backup is there if the new key does not work.

Step 3: Generate a new key pair on your local machine

Do not generate the new private key on the server if you can avoid it. On Linux, macOS, or Windows PowerShell, run:

ssh-keygen -t ed25519 -C recovery-$(date +%Y%m%d) -f ~/.ssh/tarisu_recovery

On Windows PowerShell, use a Windows path if you prefer:

ssh-keygen -t ed25519 -C recovery -f $HOME\\.ssh\\tarisu_recovery

Set a strong passphrase and store it in your password manager. The passphrase cannot be recovered from the server. If you need an automation key, create a separate key with a narrow purpose and protect its file permissions.

Display the public key:

cat ~/.ssh/tarisu_recovery.pub

On Windows PowerShell:

Get-Content $HOME\\.ssh\\tarisu_recovery.pub

Copy the entire single-line output. It begins with ssh-ed25519 and ends with your comment. The private key stays on your local machine and is never copied to the server.

Step 4: Install the new public key on the server

If you are logged in as root, replace the example key line below with your real public key.

install -d -m 700 -o root -g root /root/.ssh
tee /root/.ssh/authorized_keys > /dev/null <<'EOF'
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... recovery-20250101
EOF
chmod 600 /root/.ssh/authorized_keys
chown root:root /root/.ssh /root/.ssh/authorized_keys

If you are logged in as your normal sudo user, install the key for that user instead:

install -d -m 700 ~/.ssh
tee ~/.ssh/authorized_keys > /dev/null <<'EOF'
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... recovery-20250101
EOF
chmod 600 ~/.ssh/authorized_keys

The heredoc uses > to replace the file, so the old key list is not preserved in the live file. Your backup from Step 2 is the recovery copy. To append instead, use >> and remove the old key later.

Step 5: Test the new key before closing the console

Open a second terminal on your local machine and test the new key:

ssh -i ~/.ssh/tarisu_recovery -o IdentitiesOnly=yes user@server

On Windows PowerShell:

ssh -i $HOME\\.ssh\\tarisu_recovery -o IdentitiesOnly=yes user@server

You should see a normal shell prompt and no password prompt. If you set a passphrase, you will be asked for it first, then reach the shell. If you see Permission denied (publickey), do not close the console. Use the troubleshooting section below.

Step 6: Add a convenient SSH config entry

On your local machine, create or edit ~/.ssh/config with an entry like this. Replace the host name and user with your own values.

Host tarisu-server
  HostName server.example.com
  User deploy
  IdentityFile ~/.ssh/tarisu_recovery
  IdentitiesOnly yes

Then connect with:

ssh tarisu-server

On Windows 10/11, the user config file is usually C:\\Users\\Name\\.ssh\\config. Keep it readable only by your account. IdentitiesOnly yes prevents SSH from offering every key in your agent.

Step 7: Confirm sshd settings and reload only if needed

Adding or removing lines in authorized_keys does not require an sshd restart. If you changed /etc/ssh/sshd_config, test the configuration before reloading it. On Ubuntu and Debian:

sudo sshd -t
sudo systemctl restart ssh

On AlmaLinux 9 and Rocky Linux 9, the service is usually called sshd:

sudo sshd -t
sudo systemctl restart sshd

Do not set PasswordAuthentication no until key login has worked from a second terminal and you still have console access. If sshd -t reports an error, fix the configuration before restarting.

Step 8: Remove the old key if it was lost or compromised

If you suspect the key was stolen, remove the matching public key from every server that trusted it. Find the line by its comment:

grep -n old-key-comment /root/.ssh/authorized_keys

For a normal user, replace /root with that user's home path. Back up the file, then delete the matching line:

sudo cp -a /root/.ssh/authorized_keys /root/.ssh/authorized_keys.before-remove
sudo sed -i '/old-key-comment/d' /root/.ssh/authorized_keys

Check with grep -n old-key-comment /root/.ssh/authorized_keys; it should return nothing. If the old key was used by an automated service, create a new key for that service first and update its configuration before removing the old line.

Step 9: Clean up and document the recovery

  • Store the new private key and passphrase in your password manager.
  • Label the key with the server and purpose, for example tarisu-web-01-recovery.
  • Confirm that sudo or administrative commands still work in the new session.
  • Remove temporary backups after verifying access, but keep the pre-change sshd_config backup if you edited it.
  • Review who else has console access; console access can bypass SSH key controls.

Troubleshooting common errors

Permission denied (publickey)

Run the client command with verbose output:

ssh -vvv -i ~/.ssh/tarisu_recovery -o IdentitiesOnly=yes user@server

Check the server log while you retry. On Ubuntu and Debian:

sudo journalctl -u ssh -n 50 --no-pager

On AlmaLinux and Rocky Linux:

sudo journalctl -u sshd -n 50 --no-pager

Common causes are a missing public key, the wrong username, a private key the client did not read, or permissions that sshd rejects.

Bad ownership or modes

OpenSSH is strict. On the server, for the user you are testing, check and fix permissions:

ls -ld ~/.ssh
ls -l ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

If you are logged in as root, use /root/.ssh and make sure the files are owned by root. For a normal user, the directory and file should be owned by that user, not by root.

SELinux blocks the key on AlmaLinux or Rocky Linux

On systems with SELinux enforcing, restore the SSH file context:

sudo restorecon -R -v ~/.ssh

For root, run sudo restorecon -R -v /root/.ssh. Check sudo ausearch -m avc -ts recent if you suspect a denial, and avoid disabling SELinux as a troubleshooting step.

Windows client cannot find the key

Windows 10 and 11 use the built-in OpenSSH client. If the path is wrong, the client may fall back to another key or prompt for a password. Confirm the file exists:

Get-ChildItem $HOME\\.ssh\\tarisu_recovery

Make sure only your Windows account has read access to the private key. Windows OpenSSH can refuse keys with permissions that are too open. If you use a third-party SSH client, check that it is reading the same key file.

The server accepts only one key

If the server has several authorized keys, use -v and IdentitiesOnly=yes to force the recovery key. On the server, confirm the public key line is present and not commented out:

grep -n recovery /root/.ssh/authorized_keys

If the line is present but login still fails, check the sshd log for the exact reason. A wrong username or permissions problem often looks like a key problem.

sshd will not restart after a config change

Run sudo sshd -t and fix the reported line. If you accidentally stopped sshd and have no console, you will need provider console or rescue access to start it again. Keep the console session open until the new key is verified.

Security notes

An SSH private key is a credential, not a convenience file. Protect it with a passphrase and store it only where you can revoke it quickly. Use a separate key for each device or automation role so that losing one does not require trusting every server with the same key. Keep the number of keys in authorized_keys small and remove keys that are no longer needed.

Console and rescue access are powerful recovery tools. Treat them like root access: limit who can use them, enable multi-factor authentication where your provider offers it, and review access after any security incident. If a key is compromised, remove the public key from every server that trusted it, rotate any secrets the key could reach, and check authentication logs for unexpected sessions.

Backups are part of recovery. A backup of authorized_keys and sshd_config can save you from a typo, but it can also contain sensitive access data. Store backups with the same care as the live files, and delete temporary copies once the new key has been verified.

FAQ

Can I recover the lost private key from the server?

No. The server stores only the public key. If the private key file is gone, generate a new key pair and install the new public key through console or rescue access.

Do I need to reinstall the operating system?

Usually not. Reinstalling is destructive and should be a last resort. If you have console or rescue access, installing a new public key is faster and preserves your data.

Does adding a new public key require restarting sshd?

No. OpenSSH reads authorized_keys for each login attempt. A restart or reload is needed only when you change sshd_config or another service-level setting.

Can I generate the new key on Windows 10 or 11?

Yes. The built-in OpenSSH client supports ssh-keygen. Use a strong passphrase, protect the private key file, and test with ssh -i before closing the server console.

What about Windows Server 2019 or 2022?

The same principle applies, but the default location differs. For administrators using OpenSSH Server, the authorized keys file is commonly C:\\ProgramData\\ssh\\administrators_authorized_keys; for standard users it is in the user profile. Confirm the path and permissions before editing.

What if I have no console, VNC, or rescue access?

You need access through your provider or the hardware owner. If the server is a Tarisu VPS or dedicated server, check the console/VNC option in your Tarisu account area or contact Tarisu support for the available recovery path. A new SSH key cannot be installed remotely when the existing key is the only way in.

Related posts