VPS

How to Secure Your VPS: SSH Keys, Firewall, Fail2ban and More

8 min read

How to Secure Your VPS: SSH Keys, Firewall, Fail2ban and More

The Short Answer

A fresh VPS is a target within minutes of going online: automated bots scan the entire internet for servers with weak passwords and open ports. Securing yours takes about 30 minutes and five steps: log in with SSH keys instead of passwords, disable password login entirely, turn on a firewall, install fail2ban to block brute-force attacks, and enable automatic security updates. Do these before you install WordPress or anything else — a compromised server is far harder to clean than to protect.

This guide assumes Ubuntu 24.04 LTS, the default on most providers. The commands work on Debian with minor changes, and the principles apply to every Linux VPS.

Before You Start: What Attackers Actually Do

Understanding the threat makes the steps feel less like ritual. Within an hour of a new VPS going online, automated scanners will:

  • Try to log in as root with common passwords, thousands of times a day (brute force).
  • Probe common ports — 22 (SSH), 80/443 (web), 3306 (MySQL), 6379 (Redis) — looking for open services.
  • Exploit known vulnerabilities in outdated software, especially WordPress plugins and old PHP versions.

None of this is personal: bots do not care who you are. They want a server to mine cryptocurrency, send spam, or host phishing pages. Every step below closes one of these doors.

Step 1: Create a Non-Root User

Never run your server as root day to day. Create a regular user with sudo access, and use it for everything:

adduser deploy
usermod -aG sudo deploy

Choose a strong password for this user — you will type it whenever you run sudo. From now on, log in as deploy (or whatever name you chose) instead of root.

Step 2: Set Up SSH Keys (and Stop Using Passwords)

Passwords can be guessed; SSH keys effectively cannot. An SSH key pair is two files: a private key that stays on your computer, and a public key that goes on the server. Only someone holding the private key can log in.

On your own computer, generate a key if you do not have one:

ssh-keygen -t ed25519 -C "[email protected]"

Accept the default location (~/.ssh/id_ed25519) and set a passphrase — it protects the key if your laptop is stolen. Then copy the public key to the server:

ssh-copy-id deploy@YOUR_SERVER_IP

(Replace deploy with your username and YOUR_SERVER_IP with the server’s address.) Now test the key login in a new terminal window — keep your current session open as a safety net:

ssh deploy@YOUR_SERVER_IP

If you get in without typing the server password, keys work. Only then move to the next step.

Step 3: Disable Password Login and Root Login

This is the single most effective hardening step. With password login disabled, brute-force attacks become pointless: there is no password to guess.

On the server, edit the SSH configuration:

sudo nano /etc/ssh/sshd_config

Find (or add) these lines and set them exactly as shown:

PasswordAuthentication no
PermitRootLogin no
PubkeyAuthentication yes

Save the file, then check the configuration and restart SSH:

sudo sshd -t
sudo systemctl restart ssh

Test logging in again from a new terminal window before you close your existing session. If anything goes wrong, your open session lets you fix it. If you are locked out entirely, every provider offers a web console (VNC) in its control panel — slower, but it always works.

Never skip the test login. Disabling password authentication before confirming your SSH key works is the most common way beginners lock themselves out of a new server. Two terminal windows open, one for changes and one for testing — every time.

Step 4: Enable the Firewall (UFW)

Ubuntu ships with UFW (Uncomplicated Firewall), which is exactly what the name promises. The rule is simple: deny everything, then allow only the ports you need.

sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

This allows SSH (port 22) and web traffic (ports 80 and 443) and blocks everything else — including database ports, which should never be reachable from the internet. Check the status any time with sudo ufw status.

If you run other services, open only their ports, and only if they must be public. A MySQL or Redis port open to the world is one of the most common serious misconfigurations on a VPS.

Should you change the SSH port? Moving SSH from port 22 to a non-standard port (say, 2222) cuts the noise in your logs dramatically, because most bots only scan port 22. It is not real security — a determined attacker will find the port — but it reduces log spam and automated attempts. If you do it, update both sshd_config (Port 2222) and the firewall (sudo ufw allow 2222/tcp) before restarting SSH.

Step 5: Install Fail2ban

Fail2ban watches your logs and temporarily bans IP addresses that fail to log in too many times. It is your second line of defence: even if an attacker finds your SSH port, they get a handful of attempts before being blocked.

sudo apt update
sudo apt install fail2ban
sudo systemctl enable --now fail2ban

The default configuration is sensible: it bans an IP for 10 minutes after 5 failed SSH logins. You can check who is currently banned with:

sudo fail2ban-client status sshd

For a WordPress server, consider adding a jail for the WordPress login page later — but the SSH jail alone stops the vast majority of automated attacks.

Step 6: Enable Automatic Security Updates

Unpatched software is the other great compromise vector. Ubuntu can install security updates automatically:

sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

Answer “Yes” when asked. This installs security patches automatically; full system upgrades (which can change behaviour) still need your decision. Check what it has done with cat /var/log/unattended-upgrades/unattended-upgrades.log.

Also make a habit of updating manually every week or two:

sudo apt update && sudo apt upgrade -y

And reboot occasionally when the system asks — kernel updates only take effect after a restart.

Step 7: Backups — Your Last Line of Defence

Security reduces the chance of a breach; backups limit the damage when one happens anyway. You need two kinds:

  1. Provider snapshots. Most providers offer one-click snapshots or automatic backups (DigitalOcean charges 20% of the Droplet price; Hostinger includes weekly backups free). Snapshots restore the whole server in minutes.
  2. Your own off-site backup. Provider snapshots protect against your mistakes, not against the provider. For WordPress, UpdraftPlus (free) can back up files and database to Google Drive or Dropbox on a schedule. For other apps, a simple rsync to another machine works.

Test a restore at least once. A backup you have never restored is a hope, not a backup.

Going Further: Worth Doing When You Have Time

  • Two-factor authentication for SSH. Google Authenticator’s PAM module adds a time-based code on top of your SSH key. Strong protection, moderate setup effort.
  • Disable unused services. Every running service is a potential entry point. Check what is listening with sudo ss -tulpn and stop what you do not need.
  • Keep WordPress and plugins updated. If you run WordPress, outdated plugins are the most common compromise route — more common than SSH attacks. Enable auto-updates for plugins, or check weekly.
  • Use a separate database user. Your web app’s database user should have access only to its own database — never the MySQL root account.
  • Set up monitoring and alerts. A free uptime monitor (UptimeRobot, Better Stack) tells you within minutes if the server goes down or is hijacked for mining — a sudden CPU spike to 100% is the classic symptom.

Security Checklist (Copy This)

  • [ ] Non-root user with sudo created
  • [ ] SSH key login working
  • [ ] Password authentication disabled (PasswordAuthentication no)
  • [ ] Root login disabled (PermitRootLogin no)
  • [ ] UFW enabled: only 22, 80, 443 open
  • [ ] Fail2ban installed and running
  • [ ] Automatic security updates enabled
  • [ ] Backups configured (snapshot + off-site)
  • [ ] Software updated; reboot if the kernel changed

Frequently Asked Questions

How often do VPS servers actually get attacked?

Constantly. A new server typically sees its first automated login attempts within minutes of coming online, and thousands of attempts per day after that. Almost all of it is bots, not humans — but the bots are patient, and they only need one weak password.

Is changing the SSH port enough on its own?

No. It reduces noise but is not real security — port scans find non-standard ports easily. Treat it as a bonus on top of SSH keys and fail2ban, never as a replacement for them.

Do I need fail2ban if I use SSH keys?

Yes, as defence in depth. Keys stop password guessing, but fail2ban also protects other services and slows down any attacker probing your server. It costs nothing to run and takes two minutes to install.

What should I do if my VPS is hacked?

Take it offline, do not try to clean it while it is running. Restore from a snapshot taken before the breach, or rebuild the server from scratch and restore data from a clean backup. Then find the entry point — usually an outdated plugin or a leaked password — and fix it before the site goes live again. Never trust a compromised server’s own logs alone.

Are provider firewalls a replacement for UFW?

They complement each other. A cloud firewall (offered by DigitalOcean, Vultr, Linode and others) filters traffic before it reaches your server, which is excellent — but a local firewall (UFW) still protects you if the cloud firewall is misconfigured or bypassed. Run both if your provider offers a cloud firewall; UFW alone is fine if it does not.

The Bottom Line

Five steps, about 30 minutes: SSH keys, no password login, a firewall, fail2ban, and automatic security updates. That is the baseline every VPS needs before it hosts anything real — and it stops the overwhelming majority of automated attacks. Add backups, keep your software updated, and check your server weekly, and your VPS will be safer than most servers on the internet.

Leave a Reply

Your email address will not be published. Required fields are marked *