How to Use SSH Keys Instead of Passwords on Your VPS

How to Use SSH Keys Instead of Passwords on Your VPS

If you still log in to your VPS with a password, bots are already trying to guess it. Every server with SSH open to the internet gets hit by automated login attempts, sometimes thousands per day. Learning how to use SSH keys instead of passwords removes that attack surface almost entirely, because a private key cannot be guessed the way a password can. This guide walks you through generating a key pair, copying the public key to your server, testing the login, and turning password login off for good. It takes about ten minutes.

Why SSH Keys Beat Passwords on a VPS

A password is a short secret that a human has to remember, so most people pick something short or reuse one from another site. A brute-force bot can try common passwords thousands of times per hour, and it costs the attacker nothing to keep going. An SSH key pair works differently. Your computer holds a private key, and the server holds the matching public key. When you connect, the server sends a challenge that only the private key can answer. The private key for an Ed25519 key is 256 bits long, which means guessing it is not a realistic attack at all.

Keys are also more convenient once they are set up. You log in without typing a password, and scripts and tools like rsync, scp, and Ansible can connect without a human sitting at the keyboard. One key pair can unlock every server you manage, and you can revoke access by deleting one line from the server.

What You Need Before You Start

You need three things: a computer with a terminal, an SSH client installed, and a user account on the VPS with sudo access. On Linux and macOS, the terminal and SSH client are already installed. On Windows 10 and 11, you can use Windows Terminal or PowerShell, which include the OpenSSH client. If you have never logged in to your server before, read our guide on how to connect to your VPS over SSH for the first time first. It is also worth setting up a normal user account before you start, so follow the steps to create a sudo user and disable root login if you are still logging in as root. Keys work for the root account too, but a separate user account with sudo is safer day to day.

Set Up SSH Keys Instead of Passwords in Four Steps

Switching to ssh key authentication takes four steps: generate a key pair on your own computer, copy the public key to the server, test the new login, and fix the file permissions if the server refuses the key. Do all four before you turn password login off, or you can lock yourself out.

Step 1: Generate the key pair. On your own computer, not on the VPS, run:

ssh-keygen -t ed25519 -C "[email protected]"

Press Enter to accept the default file location, which is ~/.ssh/id_ed25519. When it asks for a passphrase, type one. A passphrase encrypts the private key on disk, so even if someone steals the file, they still need the passphrase to use it. The command creates two files: id_ed25519, your private key, and id_ed25519.pub, your public key. Never share the private key file. The public key is safe to copy anywhere.

Step 2: Copy the public key to the server. The easiest way is ssh-copy-id, which appends your public key to the server’s authorized_keys file for you:

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@your-server-ip

Replace user with your username and your-server-ip with your server’s IP address or domain. It will ask for your password one last time, then confirm the key was added. If ssh-copy-id is not available on your machine, copy the key by hand instead:

cat ~/.ssh/id_ed25519.pub | ssh user@your-server-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Step 3: Test the key login. Log in again and check that you get in without a password:

ssh -i ~/.ssh/id_ed25519 user@your-server-ip

If the terminal asks for your key passphrase instead of the server password, the key works. If it still asks for the server password, the key was not copied to the right account, or the permissions are wrong. Fix that before you continue.

Step 4: Fix permissions if login fails. SSH refuses keys when the permissions are too open. On the server, run:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Your home directory itself should not be writable by other users either. If you copied the key as root into another user’s folder, check the file owner with ls -l ~/.ssh/authorized_keys and change it with chown user:user ~/.ssh/authorized_keys if needed. After fixing permissions, test the login again. Most “Permission denied (publickey)” errors come from this step, not from a bad key.

Turn Off Password Login in sshd_config

Once key login works, the next step is to disable password authentication so bots cannot keep guessing. Open the SSH server config file on your VPS:

sudo nano /etc/ssh/sshd_config

Find the line PasswordAuthentication yes and change it to:

PasswordAuthentication no

While you are in the file, make sure public key login is on:

PubkeyAuthentication yes

Check the config for mistakes before you restart the service, because a bad config can stop SSH from starting:

sudo sshd -t

If that command prints nothing, the config is valid. Restart SSH to apply the change:

sudo systemctl restart ssh

On some systems the service is called sshd instead of ssh, so use sudo systemctl restart sshd if the first command reports that the unit was not found. Then open a second terminal window and log in with your key while the first session is still open. If the new login works, password login is off and your key is the only way in. If it fails, your old session is still connected and you can fix the config. For the full hardening checklist beyond this one change, see our guide to harden SSH on Ubuntu 24.04, and our guide to disable SSH password authentication for a deeper walkthrough of the same switch. As one more layer, set up Fail2ban to block brute-force attempts so repeated failed logins get banned at the firewall.

Common SSH Key Problems and How to Fix Them

Most key problems have a short fix. If you see “Permission denied (publickey)”, check three things in order: the key is in the right user’s authorized_keys file, the permissions are 700 on ~/.ssh and 600 on authorized_keys, and you are logging in as the right username. If you copied the key to root’s folder but log in as another user, the server will never find it.

If SSH keeps asking for a password even though the key is there, your client may be offering a different key first. Run ssh -v user@your-server-ip and look at the lines starting with “Offering public key” to see which keys were tried. If your key was never offered, check that the file exists with ls ~/.ssh/ on your own computer. If you generated the key on another machine, copy the private key file over, or generate a fresh pair and add its public key to the server.

If you set a passphrase and the terminal asks for it on every login, add the key to the SSH agent so you only type it once per session:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

On macOS, add UseKeychain yes to ~/.ssh/config so the passphrase is stored in the keychain. And if you ever lose the private key, you are not locked out forever: log in through your hosting provider’s web console, remove the old public key line from authorized_keys, and add a new one. Just make sure you test the new key before you close that console session.

What to Do Next After Switching to Keys

Switching to ssh key authentication is the single biggest win for VPS security, and it takes less time than choosing a strong password. Back up your private key to an encrypted USB drive or a password manager that stores files, and keep a copy of the public key somewhere you can reach it when setting up a new server. If other people need access to the same VPS, give each person their own key pair instead of sharing yours, so you can remove one person’s access without touching anyone else. With password login off, your server is quiet: the brute-force noise in the auth log drops to almost nothing, and the only way in is a key you control.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.