DEV Community

Cover image for How to Secure Linux VPS with Fail2ban & SSH Key Authentication: Stop Brute-Force Attacks
Faysal Molla
Faysal Molla

Posted on Originally published at faysal.cloud

How to Secure Linux VPS with Fail2ban & SSH Key Authentication: Stop Brute-Force Attacks

The Threat Landscape for New VPS Deployments

Every public IPv4 address on the internet is continuously scanned by automated botnets within minutes of being provisioned on Contabo or Hostinger. If you check your auth log with grep 'Failed password' /var/log/auth.log, you will likely see thousands of failed SSH login attempts from IP addresses worldwide trying generic usernames like root, admin, test, and ubuntu.

Leaving root password authentication enabled on default port 22 is an unacceptable security vulnerability. In this guide, we implement a three-tiered security shield: Ed25519 SSH Key Authentication, custom port assignment, and Fail2ban automated intrusion prevention.

Step 1: Generate an Ed25519 SSH Key Pair

On your local computer (Mac, Linux, or Windows PowerShell), generate an ultra-secure Ed25519 SSH key:

ssh-keygen -t ed25519 -C "admin@yourdomain.com"
Enter fullscreen mode Exit fullscreen mode

Copy your public key to your VPS server:

ssh-copy-id -i ~/.ssh/id_ed25519.pub root@YOUR_SERVER_IP
Enter fullscreen mode Exit fullscreen mode

Step 2: Hardening the SSH Daemon Configuration

SSH into your VPS and edit the main OpenSSH daemon configuration file:

sudo nano /etc/ssh/sshd_config.d/99-hardened.conf
Enter fullscreen mode Exit fullscreen mode

Paste the following security directives (choosing a custom high port such as 2244):

# Change default port to evade automated port scanners
Port 2244

# Disable root login and enforce cryptographic keys only
PermitRootLogin prohibit-password
PasswordAuthentication no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

# Restrict authentication attempts
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2
X11Forwarding no
Enter fullscreen mode Exit fullscreen mode

Critical Rule: Update Firewall Before Restarting SSH!

If you change your SSH port to 2244 without updating UFW firewall rules, you will lock yourself out of your server! Run the following commands before restarting SSH:

sudo ufw allow 2244/tcp
sudo ufw enable
sudo sshd -t
sudo systemctl restart ssh
Enter fullscreen mode Exit fullscreen mode

Do not close your current terminal window! Open a new local terminal and test connecting with your key over the new port:

ssh -p 2244 -i ~/.ssh/id_ed25519 root@YOUR_SERVER_IP
Enter fullscreen mode Exit fullscreen mode

Step 3: Installing & Configuring Fail2ban

Fail2ban monitors system log files and dynamically modifies your netfilter/iptables firewall to drop IP addresses that display malicious login behavior. Install Fail2ban:

sudo apt update && sudo apt install -y fail2ban
Enter fullscreen mode Exit fullscreen mode

Create a local jail override file:

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
Enter fullscreen mode Exit fullscreen mode

Configure the [sshd] section with your custom port and aggressive ban rules:

[sshd]
enabled = true
port = 2244
mode = aggressive
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
findtime = 600
bantime = 86400
Enter fullscreen mode Exit fullscreen mode

This configuration dictates that if an IP fails authentication 3 times within 10 minutes (600s), Fail2ban blocks all network packets from that IP for a full 24 hours (86,400s).

Step 4: Activating & Monitoring Fail2ban

Enable and start the Fail2ban service:

sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd
Enter fullscreen mode Exit fullscreen mode

You can view the real-time list of currently banned malicious IP addresses:

sudo fail2ban-client status sshd
# Output displays:
# Currently failed: 0
# Total failed: 14
# Currently banned: 2
# Banned IP list: 185.220.101.42 194.26.29.112
Enter fullscreen mode Exit fullscreen mode

Summary

By enforcing Ed25519 SSH keys, disabling password authentication, moving off port 22, and deploying Fail2ban, your Linux VPS on Hostinger or Contabo transforms from an easy target into a fortified fortress.


Originally published at faysal.cloud.

Top comments (0)