How to Upload an SSH Public Key to Your VPS for Secure Access

Using SSH keys provides a secure, passwordless way to connect to your VPS, streamlining management while enhancing security. This guide simplifies uploading your SSH public key to an existing VPS on AvaHost or similar platforms, with practical examples and tips to ensure a smooth setup.

Introduction

SSH key-based authentication replaces password logins with a private/public key pair, reducing the risk of brute-force attacks. This process is ideal for managing VPS or dedicated servers securely and efficiently.

Prerequisites

Before you begin, ensure the following:

  • You already have access to your VPS via username and password.

  • You have an SSH key pair generated on your local machine.

  • ssh is installed on your local machine (Linux, macOS, or Windows with WSL or PuTTY/Pageant).

Step 1: Check for an Existing SSH Key

On your local machine, run:

ls ~/.ssh

Check if you have a file named id_rsa.pub or id_ed25519.pub. If not, generate a new key pair:

ssh-keygen -t ed25519 -C "your_email@example.com"

Follow the prompts and press Enter to accept the default file location.

Step 2: Upload the Public Key to Your VPS

You can easily upload your key using the ssh-copy-id command:

ssh-copy-id user@your_vps_ip

Replace user with your VPS username (e.g., root or ubuntu), and your_vps_ip with your actual VPS IP address. You’ll be prompted to enter your VPS password one last time. If you don’t have ssh-copy-id, you can manually upload the key:

cat ~/.ssh/id_ed25519.pub | ssh user@your_vps_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Step 3: Test the Connection

Once the key is uploaded, try logging in:

ssh user@your_vps_ip

You should connect without entering a password.

Optional: Disable Password Authentication

To increase security, you can disable password logins completely (after making sure your key-based access works):

  1. Connect to your VPS.

  2. Edit the SSH config file:

sudo nano /etc/ssh/sshd_config
  1. Find and change or add:

PasswordAuthentication no
  1. Restart the SSH service:

sudo systemctl restart ssh

Be cautious — if your key isn’t working properly, you may lock yourself out.

Conclusion

Uploading an SSH public key to your VPS enables secure, passwordless access, simplifying management while boosting security. With AvaHost’s reliable VPS hosting, the steps and examples above—using ssh-copy-id or manual upload—make setup straightforward. Keep your private key safe, test thoroughly, and consider disabling password authentication for maximum protection.