Securing a new VPS: SSH keys, firewall and fail2ban without locking yourself out
A practical way to secure a VPS on Ubuntu: a dedicated user with SSH keys, no root login, ufw with rate limiting and fail2ban. It also covers the day fail2ban banned my own IP and how I got back in.

A fresh VPS starts receiving login attempts from bots within minutes of going online. You need to secure the VPS quickly, but the steps that keep attackers out can also keep you out. This is the order I follow on Ubuntu 22.04, and the mistake that taught me to respect that order.
My own site runs on a Hostinger VPS with Node.js under PM2, MySQL in Docker and Nginx in front. Everything below comes from setting that server up. The commands are standard Ubuntu, so they work on any provider.
Before you touch anything: keep a way back in
Every lockout story starts the same way. Someone changes SSH settings, closes their only session, and finds out the new settings don't work.
Two habits prevent most of it:
- Keep your current session open while you change SSH or firewall settings. Test every change from a second terminal window. Close the first session only after the second one works.
- Know your out-of-band access. Most VPS providers give you a console that doesn't depend on your SSH connection. On Hostinger, hPanel has a browser terminal for the VPS. Find it now, before you need it. I'll come back to why.
Step 1: a dedicated user with SSH key setup on Ubuntu
Don't work as root day to day. Create a normal user with sudo rights:
adduser your-user
usermod -aG sudo your-user
On your own computer, generate a key pair if you don't have one. Ed25519 is short, fast and widely supported:
ssh-keygen -t ed25519 -C "you@example.com"
Copy the public key to the server for the new user:
ssh-copy-id your-user@203.0.113.10
You can also add public keys from the SSH keys section in hPanel. I used it during setup. Either way, the private key never leaves your machine. Only the .pub file goes to the server.
Now open a new terminal and confirm you can log in with the key:
ssh your-user@203.0.113.10
sudo whoami # should print: root
If both lines work, you're ready for the next step. If not, fix it now while root access still works.
Step 2: disable root login and passwords
On Ubuntu 22.04, the main SSH config reads extra files from /etc/ssh/sshd_config.d/, and the first value it finds for a setting wins. Some cloud images ship a file there that turns password login back on. That's why editing only sshd_config sometimes "doesn't work."
The clean fix is a drop-in file whose name sorts first:
sudo nano /etc/ssh/sshd_config.d/00-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
Check the syntax, then restart. On Ubuntu the service is called ssh:
sudo sshd -t && sudo systemctl restart ssh
Confirm what SSH is actually using:
sudo sshd -T | grep -E "permitrootlogin|passwordauthentication"
You should see permitrootlogin no and passwordauthentication no. Then test again from a new window, without closing the old one.
Step 3: ufw firewall with rate limiting
The rule is simple: deny everything incoming, then allow only what you serve. For a typical web server that means SSH, HTTP and HTTPS.
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit OpenSSH
sudo ufw allow "Nginx Full"
sudo ufw enable
sudo ufw status verbose
The order matters. Allow SSH before you run ufw enable, or you cut your own connection.
limit is better than allow for SSH. ufw temporarily blocks an address that opens too many connections in a short window. That slows brute-force bots before they reach fail2ban. The "Nginx Full" profile opens ports 80 and 443.
Two things people miss:
- Docker can bypass ufw. Docker writes its own firewall rules, so a port published with
-p 3306:3306can be reachable from the internet even if ufw says it's blocked. Bind database ports to localhost instead:-p 127.0.0.1:3306:3306. That's how I run MySQL. - hPanel has its own firewall section for the VPS. If you use it as well, both layers must allow SSH. A rule missing in either one blocks you.
Step 4: fail2ban, and the day it banned me
fail2ban reads the logs, spots repeated failed logins and bans the source address for a while.
sudo apt update && sudo apt install fail2ban -y
sudo nano /etc/fail2ban/jail.local
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
ignoreip = 127.0.0.1/8 ::1
[sshd]
enabled = true
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd
Never edit jail.conf directly. Package updates can overwrite it, while jail.local stays yours.
Here's the real lesson. Right after setting all this up, I was switching my connection to the new user. I kept connecting with the wrong username and key combination, and my SSH agent offered several keys on each attempt. Every rejected attempt counted as a failure. A few tries later, my connection to the server just hung. fail2ban had done exactly what I asked and banned my own IP.
SSH from my machine was useless at that point. The hPanel browser terminal still worked, so I opened it and ran:
sudo fail2ban-client status sshd
sudo fail2ban-client set sshd unbanip 203.0.113.10
My IP was in the banned list. One command removed it and I was back in.
Then I fixed the cause, not just the symptom. I gave the server its own entry in ~/.ssh/config on my computer:
Host my-vps
HostName 203.0.113.10
User your-user
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
IdentitiesOnly yes stops the agent from trying every key it holds, so one wrong key no longer means five failures. Now I just type ssh my-vps.
If you have a static office IP, you can add it to ignoreip. Most home and mobile connections change their IP, though, so don't rely on that alone.
A secure VPS checklist you can reuse
Here's the short version I keep for new servers:
- Find the provider's console or browser terminal before changing anything.
- Create a sudo user and log in with an SSH key.
- Disable root login and password login with a drop-in file, then test from a new window.
- Turn on ufw: deny incoming,
limitSSH, allow only the ports you serve. - Bind Docker database ports to
127.0.0.1. - Install fail2ban with a
jail.localfile. - Use
~/.ssh/configwithIdentitiesOnly yes. - Turn on automatic security updates:
sudo apt install unattended-upgrades. - Set up backups and test a restore at least once.
A VPS gives you full control, which also means you own updates, backups and security. If that sounds like more than you want, my complete Hostinger guide explains when shared hosting is enough. If you do want a VPS, Hostinger's VPS plans are what I use. Check current prices yourself, and note that renewal prices are usually higher than the first term.
Once the server is locked down, the next steps are deploying your Node.js app with PM2 and Nginx and adding free HTTPS with Let's Encrypt.
FAQ
Should I change the SSH port to secure my VPS?
It reduces log noise from bots, but it's not real security. Keys, no root login and fail2ban do the actual work. If you change the port, update ufw first, before restarting SSH.
How do I unban my IP in fail2ban?
Get in through your provider's console or browser terminal, then run sudo fail2ban-client set sshd unbanip YOUR_IP. Check the list first with sudo fail2ban-client status sshd.
What if I lose my SSH private key?
Use the browser terminal in your hosting panel to add a new public key to ~/.ssh/authorized_keys for your user. Keep a backup of your private key somewhere safe and encrypted.
Is ufw enough without fail2ban?
ufw rate limiting slows fast bots, but it doesn't read failed logins. fail2ban catches slower, repeated attempts. They do different jobs, so use both.
Setting up a server for your platform and want a second pair of eyes? Talk to the editor at ahmed-adil.com.
Some links in this article are affiliate links; if you buy through them, Ahmed may earn a commission at no extra cost to you.



