Introduction
If you just bought a VPS and are starting to self-host, this is one of the most important security improvements you can make.
By switching to SSH key authentication and disabling password login, your server becomes nearly immune to brute-force attacks — even if someone discovers your password or you're still using the default port 22.
Step 1: Generate an SSH Key Pair on Your Local Machine
Linux & macOS
Open a terminal and run:
ssh-keygen -t rsa -b 4096or (newer recommended format):
Bash
ssh-keygen -t ed25519Press Enter to accept the default file location and leave the passphrase empty (just hit Enter twice).
Your keys will be saved as:
- Private key: ~/.ssh/id_rsa or ~/.ssh/id_ed25519
- Public key: ~/.ssh/id_rsa.pub or ~/.ssh/id_ed25519.pub
Never share the private key!
Windows (PowerShell)
Open PowerShell and run:
ssh-keygen.exe -t ed25519(or -t rsa -b 4096 if ed25519 is not supported)
Press Enter through the prompts (no passphrase). Keys will be created in C:\Users\YourUser\.ssh\
Step 2: Copy Your Public Key to the VPS
Linux & macOS (Easiest Method)
ssh-copy-id user@your-vps-ipReplace user and your-vps-ip with your actual username and server IP.
Windows
- Paste your public key (it’s one long line starting with ssh-ed25519 or ssh-rsa) → Save with Ctrl+O → Enter → Ctrl+X
- Test it: Open a new terminal/PowerShell and try logging in. It should work without asking for a password.
Edit the file:Bash
nano ~/.ssh/authorized_keysCreate the .ssh folder and authorized_keys file (if they don't exist):Bash
mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keysLog into your VPS normally (with password):PowerShell
ssh user@your-vps-ipCopy your public key to clipboard:PowerShell
Get-Content $HOME\.ssh\id_ed25519.pub | Set-Clipboard(or id_rsa.pub if you used RSA)
Step 3: Disable Password Authentication
Now that key login works, disable password login entirely.
Log into your VPS (using your key) and edit the SSH config:
sudo nano /etc/ssh/sshd_configFind and change (or add) these lines:
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
PubkeyAuthentication yesSave and exit.
Restart the SSH service:
sudo systemctl restart sshd(or sudo service ssh restart on older systems)
Final test: Try logging in from a new terminal. It should only work with your private key — password attempts will be rejected instantly.
Done!
Your VPS is now protected against:
- Brute-force attacks
- Credential stuffing
- Weak or leaked passwords
Even if an attacker knows your username and password, they cannot log in without your private key file.
Pro tip: Back up your private key securely and consider adding a passphrase later using ssh-keygen -p.
If you enjoyed this guide, feel free to share it or subscribe to The Self Hosting Art. Thank you for reading! 😊