How to Lock Down Your VPS Login: Fail2ban, SSH Keys, and Port Knocking Compared
Every VPS with SSH open to the internet gets probed. Bots scan port 22 all day, trying common usernames and weak passwords against any server that answers. If you want to lock down your VPS login, you do not need an expensive security product. Three tools cover most of the risk: SSH keys, Fail2ban, and port knocking. Each one works in a different way, and each one has a weak spot the others can cover. This guide compares them side by side, with the exact commands to set each one up, so you can pick the combination that fits your server.
If you just bought a server, start with the first things to do after buying a VPS before you open it up to real traffic. Login hardening belongs near the top of that list, right after updates and a firewall.
How to Lock Down Your VPS Login With SSH Keys
SSH key authentication replaces passwords with a key pair. You keep a private key on your own computer, and the server holds the matching public key. Nobody can log in without the private key, so password guessing stops working entirely. This is the single biggest upgrade you can make to your login security, and it costs nothing.
Generate a key on your own machine:
ssh-keygen -t ed25519 -C "[email protected]"
Copy the public key to the server:
ssh-copy-id user@your-server-ip
Then turn off password login in /etc/ssh/sshd_config:
PasswordAuthentication no
PubkeyAuthentication yes
PermitRootLogin no
Restart SSH with sudo systemctl restart ssh, and test the new login in a second terminal window before you close your current session. If something is wrong, the open session lets you fix it. A guide to disable password login and switch to key-only access walks through the same steps in more detail.
The weak spot is key management. If someone copies your private key, they have your access. Protect it with a passphrase, keep it off shared machines, and store a backup copy somewhere safe. For a team, give each person their own key instead of sharing one. That way you can remove one person’s access without changing keys for everyone else.
Fail2ban: Ban the Bots That Keep Trying
Fail2ban watches your logs and blocks IP addresses that fail too many logins. A bot that tries fifty passwords in a minute gets banned for ten minutes or longer, and the log noise drops fast. Fail2ban ssh protection does not change how you log in. It changes how much abuse your server has to absorb.
Install it on Ubuntu or Debian:
sudo apt install fail2ban
sudo systemctl enable --now fail2ban
Create /etc/fail2ban/jail.local with a rule for SSH:
[sshd]
enabled = true
port = ssh
maxretry = 5
findtime = 10m
bantime = 1h
This bans an IP for one hour after five failed logins in ten minutes. Check the bans with sudo fail2ban-client status sshd. A full Fail2ban setup guide covers the same install plus jails for WordPress and mail services.
Fail2ban has two limits you should know. First, it only reacts after failed attempts, so it never stops the first few guesses. Second, attackers with many IP addresses can spread attempts across them and stay under the limit. That is why Fail2ban works best as a second layer, not the only one. Pair it with SSH keys and the bots have nothing to guess and nowhere to keep trying from.
Port Knocking: Hide the Door Entirely
Port knocking keeps your SSH port closed until a client taps a secret sequence of ports first. The firewall watches for knocks on, say, ports 7000, 8000, and 9000 in order. When it sees the right sequence from your IP, it opens port 22 for a short window. Everyone else sees a closed port and moves on. A port knocking setup adds a layer of obscurity on top of real authentication.
Install knockd and add a rule like this to /etc/knockd.conf:
[openSSH]
sequence = 7000,8000,9000
seq_timeout = 15
command = /sbin/iptables -I INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
tcpflags = syn
From your own machine, knock with a client such as knock and then connect normally. If you want the full walkthrough, this guide shows how to change the default SSH port and set up port knocking on the same server.
The trade-off is clear. Port knocking hides SSH from casual scans, but it adds friction every time you connect, and it breaks tools that expect SSH to answer right away. Automated backups, deploy scripts, and monitoring checks need the port open or need to knock first. It also relies on secrecy: once the sequence leaks, the hiding stops working. Treat it as a way to cut noise, not as a replacement for keys.
Fail2ban vs SSH Keys vs Port Knocking: Side by Side
The table below sums up the three methods. None of them is the full answer on its own. The right choice depends on whether you value ease of use, strong authentication, or invisibility most.
| Method | How it works | Protection level | Setup effort | Main drawback |
|---|---|---|---|---|
| SSH keys | Key pair replaces passwords; server checks the private key | High: stops password guessing completely | Easy: one command to generate, one file to edit | A stolen private key gives full access |
| Fail2ban | Bans IPs after repeated failed logins | Medium: slows bots, cuts log noise | Easy: install, add a small config file | Only reacts after failures; distributed attacks slip past |
| Port knocking | SSH port stays closed until a secret port sequence opens it | Medium: hides SSH from casual scans | Moderate: needs knockd and firewall rules | Slows every login; breaks tools that expect SSH open |
Read the table as a guide to layering, not a ranking. SSH keys fix the authentication problem. Fail2ban shrinks the attack surface in your logs. Port knocking shrinks it further by hiding the port. Together they cover the gaps each one leaves open.
A Layered Setup That Works in Practice
For most small VPS setups, this order gives the best return for the least work. First, switch to SSH keys and turn off password login. That one change removes the attack bots are built for. Second, install Fail2ban with the SSH jail above, so the bots that keep trying get banned and your logs stay readable. Third, change the default SSH port from 22 to something less obvious. It will not stop a targeted scan, but it cuts most of the background noise. Port knocking is optional after that. Add it only if you connect by hand and want the port hidden too.
Keep a checklist for the rest of the server as well. A guide to harden SSH on Ubuntu 24.04 covers ten config changes that pair well with the steps above, from login grace time to allowed users. Run through it once, then test each change from a second session before you log out.
Test your setup the way an attacker would. Try to log in with a password and confirm the server refuses. Try five wrong passwords from another IP and confirm Fail2ban bans it. Try to connect without knocking and confirm the port stays closed. If each test gives the answer you expect, the layers are doing their job.
To keep your VPS login safe for good, do not rely on one trick. Use SSH keys so passwords stop mattering, Fail2ban so repeat attackers get cut off, and port knocking or a moved port so the door is harder to find. Set them up once, test them, and check the Fail2ban log now and then to see what the bots are still trying.