Guides ยท Oct 5, 2026
How to Secure SSH on a New VPS: Keys, Config and Fail2ban
Harden SSH on a VPS: ed25519 keys, disabling password and root login, AllowUsers, testing sshd config safely and fail2ban.
SSH is the front door to your server, and on any public IP address it gets knocked on constantly. Within minutes of a new VPS coming online, bots start guessing passwords for root. This guide goes deeper on SSH specifically than our general first 10 minutes checklist, and explains why each setting matters.
Why keys beat passwords
A password can be guessed, reused, leaked or phished. An SSH key pair has a private half that never leaves your computer and a public half that you place on the server. An attacker cannot brute-force a modern key, so once password login is disabled, the endless guessing attempts in your logs become harmless noise.
1. Create an ed25519 key
On your own computer (Windows 10/11, macOS and Linux all include ssh-keygen):
ssh-keygen -t ed25519 -C "laptop-2026"
Set a passphrase when asked. The passphrase encrypts the private key on disk, so a stolen laptop does not mean a stolen server. Your keys end up in ~/.ssh/id_ed25519 (private, never share) and ~/.ssh/id_ed25519.pub (public).
2. Install the public key on the server
ssh-copy-id -i ~/.ssh/id_ed25519.pub USER@YOUR_SERVER_IP
On Windows without ssh-copy-id, print the public key with type %USERPROFILE%\.ssh\id_ed25519.pub, then on the server append it to ~/.ssh/authorized_keys. You can also add the key to your TidalVPS account so that it is installed automatically on new servers and reinstalls.
Check the permissions, because SSH ignores keys in folders that are too open:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
3. Write a hardening drop-in file
On Ubuntu and Debian you should not edit /etc/ssh/sshd_config directly. Create a file in /etc/ssh/sshd_config.d/ instead, which survives package upgrades. Files are read in alphabetical order and the first value found for an option wins, so give yours a low number:
sudo nano /etc/ssh/sshd_config.d/00-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowUsers deploy
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
- PermitRootLogin no: attackers always try the
rootusername first. - PasswordAuthentication no and KbdInteractiveAuthentication no: together these close every password-based path.
- AllowUsers: only the listed accounts can log in at all. Use your own username here.
- MaxAuthTries / LoginGraceTime: shorten the time and attempts an unauthenticated connection gets.
4. Test before you restart (this prevents lockouts)
A typo in the SSH config can lock you out. Validate it first, and keep your existing session open:
sudo sshd -t && echo "config OK"
sudo sshd -T | grep -E "passwordauthentication|permitrootlogin|allowusers"
sudo systemctl reload ssh
On Ubuntu the service is called ssh; on some other distributions it is sshd. Now open a second terminal and log in with your key. Only close the first session after the second one works. If it does not, fix the file from the first session.
Some cloud images ship a file such as 50-cloud-init.conf that sets PasswordAuthentication yes. Because the first value wins, your 00- file takes priority, and sshd -T confirms the final result.
5. Should you change the SSH port?
Moving SSH to another port reduces log noise from bots but is not real security: a port scan finds it in seconds. If you do it, allow the new port in your firewall before restarting, and keep that firewall rule in mind when you troubleshoot. Keys and disabled passwords do the real work. For most people, leaving port 22 and using a firewall plus fail2ban is simpler and less error-prone.
6. Add fail2ban
sudo apt install -y fail2ban
sudo nano /etc/fail2ban/jail.local
[sshd]
enabled = true
maxretry = 4
findtime = 10m
bantime = 1h
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd
Make sure you do not ban yourself: add your own fixed IP to ignoreip under [DEFAULT] if you have one. See the banned list any time with the status command above, and unban with sudo fail2ban-client set sshd unbanip IP.
7. Limit who can reach SSH at the firewall
If you always connect from a known address, restrict SSH to it:
sudo ufw allow from YOUR_HOME_IP to any port 22 proto tcp
If your IP changes, use sudo ufw limit OpenSSH instead, which rate-limits repeated connection attempts. The UFW guide covers both.
8. Optional: require a second factor
For servers that matter, add a time-based one-time code on top of the key:
sudo apt install -y libpam-google-authenticator
google-authenticator
Scan the QR code with an authenticator app and save the emergency codes somewhere safe. Then configure PAM and set AuthenticationMethods publickey,keyboard-interactive in your SSH drop-in file. This one needs care, so keep a console session open and test thoroughly. It is optional for most small servers.
9. Keep an eye on logins
last -a | head
sudo journalctl -u ssh --since "1 hour ago" | grep -i -E "accepted|failed"
Accepted logins from IP addresses you do not recognise are your signal to investigate. Failed attempts from random countries are normal and not a sign that anyone got in.
If you lock yourself out
The web console in your dashboard connects without SSH, so you can log in there and fix the file. If you cannot log in at all, reinstalling the server from the dashboard is the last resort (it erases the disk, so use your backups).
What SSH hardening does not cover
These settings protect the login. They do not stop a volumetric flood that saturates your connection; that is the job of network filtering, which comes with every DDoS protected VPS. Layer both.
Frequently asked questions
Is it safe to disable password login on SSH?
Yes, as long as key login works first. Test in a second terminal before closing your existing session, and remember the dashboard web console as a fallback.
What is the best SSH key type?
ed25519 is the modern default: small, fast and secure. Use RSA with at least 3072 bits only if you need compatibility with very old systems.
Does changing the SSH port improve security?
It reduces log noise but is not a real defence. Keys, no passwords, a firewall and fail2ban matter far more.
Do I need fail2ban if passwords are disabled?
It is not required, but it reduces log spam and extra load, and can protect other services as well.
Related guides
- How to Secure a New Ubuntu VPS (First 10 Minutes)
- UFW Firewall on an Ubuntu VPS: Rules, Rate Limiting and Docker Caveats
- Ubuntu 26.04 VPS Setup and Hardening Checklist
Need a server for this?
Every TidalVPS VPS comes with full root access, unmetered traffic, daily backups and DDoS protection.