
SSH Keys Explained: How to Set Up SSH Key Authentication on VPS
What Is SSH? Secure Your Server with SSH Keys (2026 Guide)
If you rent a VPS, the very first thing standing between your server and thousands of hacking bots is SSH. Every day, automated scanners probe millions of servers looking for weak SSH passwords – and a compromised server can be turned into a spam bot or crypto miner within hours.
This guide explains what SSH is, why password logins are dangerous, and how to lock down your server with SSH key authentication in about 10 minutes.
You can see how to Install CloudPanel on Ubuntu 24.04 LTS in 5 Minutes (Beginner Guide)
What Is SSH?
SSH (Secure Shell) is an encrypted protocol that lets you remotely access and manage a server through the command line. When you type ssh root@your-server-ip, SSH creates an encrypted tunnel between your computer and the server – nobody in between can read your traffic.
SSH has two login methods:
- Password authentication – you type a password every time
- Key-based authentication – your computer proves its identity with a cryptographic key pair
Passwords are the weak link. Bots can brute-force them, phishing can steal them, and humans reuse them. SSH keys, by contrast, are practically uncrackable – a 4096-bit key would take longer than the age of the universe to guess.
How SSH Keys Work
An SSH key pair has two parts:
- Private key – stays on your computer, never shared (like a house key)
- Public key – placed on the server (like the lock on the door)
When you connect, the server challenges your computer to prove it holds the matching private key. No password travels over the network, so there’s nothing to intercept or brute-force.
Step 1: Generate an SSH Key Pair
On your local computer (Linux, macOS, or Windows PowerShell), run:
ssh-keygen -t ed25519 -C "[email protected]"Press Enter to accept the default location (~/.ssh/id_ed25519), and set a passphrase for extra protection. This creates two files: id_ed25519 (private) and id_ed25519.pub (public).
Step 2: Copy the Public Key to Your Server
The easiest way:
ssh-copy-id root@your-server-ipEnter your password one last time. Your public key is now stored in the server’s ~/.ssh/authorized_keys file.
(On Windows without ssh-copy-id, manually paste the contents of id_ed25519.pub into that file.)
Step 3: Test Key Login
Open a new terminal and connect:
ssh root@your-server-ipIf you log in without a server password prompt, your key works. Do not close your existing session yet – keep it open as a backup in case something breaks.
Step 4: Disable Password Login
Now harden the SSH configuration on the server:
nano /etc/ssh/sshd_config
Find and set these lines:
PasswordAuthentication no
PermitRootLogin prohibit-password
PubkeyAuthentication yes
Save, then restart SSH:
systemctl restart sshdFrom now on, only computers holding your private key can log in. Brute-force bots will knock on a door that no longer exists.
Step 5: Extra Hardening (Recommended)
- Change the default port 22 to something random (e.g., 49200) – cuts bot traffic by 90%+
- Install Fail2Ban – auto-bans IPs after failed attempts
- Create a sudo user and disable root login entirely for daily work
- Keep private keys safe – never email them or store them in cloud notes
Common Mistakes to Avoid
- Disabling password login before testing your key > instant lockout
- Sharing one key across your whole team > create a key per person
- Using ancient RSA 1024-bit keys > use ed25519 or RSA 4096
- Forgetting the passphrase > store it in a password manager
Finally
SSH keys are the single highest-impact security upgrade for any VPS – free, fast, and nearly unbreakable. Combine key authentication with a custom port and Fail2Ban, and your server drops off the radar of 99% of automated attacks. Ten minutes of setup buys you years of peace of mind.







