Connecting to Your Server After Losing SSH Keys: A Beginner-Friendly Guide

Zainul KhanZainul Khan
3 min read

Have you ever been locked out of your own server? Don't worry, it happens to the best of us! This guide will help you regain access using SSH keys, even if you've lost your original keys. I will also cover how to use these keys with apps like FileZilla (a FTP) and PuTTY (a SSH client).

What You'll Learn

  1. Creating a new set of SSH keys

  2. Adding your new key to your server

  3. Using your new key with FileZilla and PuTTY

  4. Solving common problems

Step 1: Making Your New SSH Keys

  • Open your computer's command line:

    • On Windows: Search for "Git Bash" and open it

    • On Mac or Linux: Open the "Terminal" app

  • Type this command and press Enter:

      ssh-keygen -t rsa -b 4096
    
  • When asked where to save the key, just press Enter to use the default location.

  • You'll be asked for a passphrase. You can leave this blank for now by pressing Enter twice.

Great job! You now have two new files:

  • id_rsa: Your private key (keep this secret!)

  • id_rsa.pub: Your public key (this one's okay to share)

Step 2: Telling Your Server About Your New Key

Now we need to let your server know about your new key:

  1. Log into your server using the DigitalOcean console (or whatever method you still have access to).

  2. Once you're in, type this command and press Enter:

     nano ~/.ssh/authorized_keys
    
  3. You'll see a text editor. Go to the end of any text already there.

  4. Now, open the id_rsa.pub file on your computer (remember, that's your public key). Copy all of its contents.

  5. Go back to your server and paste the copied text at the end of the file. Make sure it's on a new line!

  6. Exit from the editor.

You've now added your new key to your server!

Step 3: Using Your New Key with FileZilla and PuTTY

For FileZilla:

  1. Open FileZilla

  2. Go to File > Site Manager

  3. Click "New Site" and give it a name

  4. Fill in these details:

    • Host: Your server's IP address

    • Port: 22

    • Protocol: SFTP - SSH File Transfer Protocol

    • Logon Type: Key file

    • User: Your username on the server (often "root")

    • Key File: Click Browse and find your id_rsa file

For PuTTY:

PuTTY needs a special key format. Here's how to get it:

  1. Download and open PuTTYgen

  2. Click "Load" and find your id_rsa file

  3. Click "Save private key" and save it as a .ppk file

  4. In PuTTY, go to Connection > SSH > Auth

  5. Browse and select your new .ppk file

Step 4: Troubleshooting

If things aren't working:

  1. Double-check that you're using the correct username and server address.

  2. Make sure you selected SFTP (not FTP) in FileZilla.

  3. If you're on Mac or Linux, try this command to fix permissions:

     chmod 600 ~/.ssh/id_rsa
    
  4. Look for error messages in FileZilla's log (at the top of the window) for clues.

You Did It!

Congratulations! You should now be able to connect to your server using your new SSH key. Remember to keep your private key safe and never share it with anyone.

If you're still having trouble, don't hesitate to ask for help from your hosting provider or a tech-savvy friend. Good luck and happy server managing!

0
Subscribe to my newsletter

Read articles from Zainul Khan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Zainul Khan
Zainul Khan