Using SSH keys is one of the most secure and efficient ways to connect to a VPS. Instead of entering a password every time, you can authenticate using a private/public key pair. In this guide, we’ll walk you through uploading your SSH public key to an existing VPS to enable secure, passwordless login.

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 your SSH public key to a VPS simplifies access and enhances security. Whether you’re managing a single server or dozens, key-based authentication saves time and minimizes risks. Just don’t forget to keep your private key safe and disable password login only after ensuring everything works smoothly.