Every new VPS ships with one password login, and that password is the first thing brute-force bots attack — often within minutes of the server coming online. SSH keys fix this properly. You generate a key pair once on your own computer: a private key that never leaves your machine, and a public key you copy to the server. The server then checks that your private key matches, no password needed.
The result is logins that are both safer and faster: you type ssh user@your-server and you are in, with no password prompt, and bots hammering port 22 with password lists get nowhere. This guide shows the exact commands for Windows, macOS, and Linux — including how to turn off password login entirely once your key works.
What you need before you start
- A VPS running Linux with SSH access (root or a sudo user) and its IP address
- A terminal on your computer: Terminal on macOS, any terminal on Linux, or Windows Terminal / PowerShell on Windows 10+ (OpenSSH is built in since Windows 10 version 1803)
- About ten minutes
Step 1: Generate your key pair
Generate the key on your own computer, not on the server. An Ed25519 key is the modern default: short, fast, and strong. On macOS and Linux, open your terminal. On Windows, open PowerShell (not the old Command Prompt) — Windows Terminal works fine too.
macOS and Linux
Run this, replacing the comment with your email or any label you want:
ssh-keygen -t ed25519 -C "[email protected]"
You will be asked where to save the key. Press Enter to accept the default (~/.ssh/id_ed25519). You will then be asked for a passphrase. Set one — it encrypts the private key so a stolen laptop does not mean a stolen server. Then add the key to your ssh-agent so you only type the passphrase once per session:
eval "$(ssh-agent -s)"ssh-add ~/.ssh/id_ed25519
Windows (PowerShell)
Windows 10 and 11 ship OpenSSH, so the same command works in PowerShell:
ssh-keygen -t ed25519 -C "[email protected]"
Accept the default save location (C:UsersYou.sshid_ed25519), set a passphrase, then start the agent and add the key:
Start-Service ssh-agent; Set-Service ssh-agent -StartupType Automaticssh-add $env:USERPROFILE.sshid_ed25519
If ssh-keygen is not recognized, your Windows is older than build 1803 — install OpenSSH Client from Settings → Apps → Optional features, then retry. PuTTY users can keep using PuTTYgen to make the pair, but switching to the built-in OpenSSH in Windows Terminal is less friction long term.
Step 2: Copy the public key to your VPS
Only the public key (id_ed25519.pub) goes to the server. Never email or copy the private key anywhere.
Linux and macOS (easiest)
The ssh-copy-id utility does this in one command. It logs in with your password one last time, creates ~/.ssh/authorized_keys on the server, and sets the right permissions:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@YOUR_SERVER_IP
Manual copy (works everywhere, including Windows PowerShell)
Print your public key:
cat ~/.ssh/id_ed25519.pub
On Windows PowerShell the equivalent is type $env:USERPROFILE.sshid_ed25519.pub. Copy the whole line, log in to your VPS, and append it to authorized_keys:
mkdir -p ~/.ssh && chmod 700 ~/.sshecho "PASTE_YOUR_PUBLIC_KEY_LINE_HERE" >> ~/.ssh/authorized_keyschmod 600 ~/.ssh/authorized_keys
Permissions matter. If authorized_keys is group- or world-writable, the SSH daemon ignores it silently — the most common reason “the key just doesn’t work.”
Step 3: Test the key login
Open a new terminal and connect. Use -i if your key has a non-default name:
ssh -i ~/.ssh/id_ed25519 user@YOUR_SERVER_IP
You should land straight at a shell prompt (or be asked only for your key passphrase). Do not close your existing session until this new login succeeds — keep one working connection open as a safety line while you change SSH settings.
Step 4: Disable password login (do this once the key works)
Password logins are what bots attack. With the key proven, turn them off server-side. Edit the SSH daemon config:
sudo nano /etc/ssh/sshd_config
Set these three lines (add them if they are commented out or missing):
PasswordAuthentication noChallengeResponseAuthentication noPermitRootLogin prohibit-password
PermitRootLogin prohibit-password means root can only log in with a key — root password attempts fail even if someone guesses it. Save, then restart SSH and test from a new connection before closing your safety session:
sudo systemctl restart sshd
Still getting a password prompt? Restart did not reload — run sudo sshd -t first to catch syntax errors in the config.
Troubleshooting: the usual suspects
| Symptom | Likely cause and fix |
|---|---|
| “Permission denied (publickey)” | Key never landed in authorized_keys, or permissions are wrong. Re-run the chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys commands on the server. |
| Still asked for a password after copying | You may have pasted the key with a line break, or pasted into the wrong user’s home directory. Check the key is one unbroken line for the user you log in as. |
| Works locally, fails from a second device | Keys live on the device that generated them. Generate a new pair on the second device and add its public key to authorized_keys too — do not copy the private key between machines. |
| Locked out of the server | Use your provider’s web console (VNC/serial console) to get in without SSH, restore a password login temporarily, and re-add your key. Almost every VPS panel offers this. |
Practical takeaways
- Generate keys on your own computer with
ssh-keygen -t ed25519and a passphrase; macOS and Linux use Terminal, Windows uses PowerShell with built-in OpenSSH. - Copy only the public key:
ssh-copy-idon macOS/Linux, manual paste on Windows PowerShell. - Set
~/.sshto 700 andauthorized_keysto 600, or the server will silently ignore your key. - Once key login works, set
PasswordAuthentication noandPermitRootLogin prohibit-password, restart sshd, and test from a fresh session. - Keep one spare access path — a provider web console, or a second key on another device — before you disable passwords.
SSH keys are a one-time setup that pays off every single day: faster logins, no passwords to type or leak, and a server that shrugs off the constant password guessing hitting port 22. Set it up now, on every new VPS, before you install anything else. Brand new to servers? Our walkthrough on connecting to your VPS via SSH for the first time covers the client side step by step. And if the VPS itself is new territory, start with what a VPS is and how it works.