Special Offers: Up to 75% OFF on Hostinger & High-RAM Contabo Cloud VPS available this month.Compare Plans

How to Set Up UFW Firewall & Fail2ban on Ubuntu VPS for Zero-Trust Security

Alex Vance
Alex VanceCloud Infrastructure Architect
October 10, 2026
•
15 min read
•
4.9 / 5.0
Transparency Notice: This article contains affiliate links to hosting providers like Hostinger and Contabo. If you purchase through our links, we may earn a commission at zero extra cost to you. We only recommend products we personally benchmark and test. Read full disclosure.
How to Set Up UFW Firewall & Fail2ban on Ubuntu VPS for Zero-Trust Security

Hostinger vs. Contabo: Full Specifications Breakdown

Data verified from our lab benchmarks • Updated for 2026

Feature / Metric
HostingerEditor's Choice UI
ContaboMax RAM Value
Starting Price$2.99 / month (Web) | $4.99 / mo (VPS)$5.50 / month (Cloud VPS S)
vCPU Cores1 to 8 vCPU (Dedicated resources)4 to 12 vCPU (High Core Count)
RAM Memory4 GB to 32 GB6 GB to 64 GB (Gigantic RAM)
Storage Technology50 GB – 400 GB NVMe SSD50 GB NVMe or 300 GB SSD
Traffic / Bandwidth4 TB to 32 TB32 TB Outgoing + Unlimited In
Control Panel & UIhPanel (Ultra Modern & Easy)cPanel, Plesk, Webmin, or CLI
Customer Support24/7 Live Chat & AI Support (Multi-lingual)Email Ticket Support (German Business Hours)
Best ForBloggers, WordPress Sites & Devs wanting easy UIHeavy Database, Docker, Game Servers & SysAdmins
Money-Back Guarantee30-Day Money-Back Guarantee14-Day EU Statutory Cancellation
Official LinksVisit HostingerVisit Contabo

The Constant Reality of Internet Port Scanners

Within seconds of provisioning a public IPv4 address on any cloud provider, automated botnets and malicious crawlers begin scanning ports 22 (SSH), 80 (HTTP), 3306 (MySQL), and 5432 (PostgreSQL). Leaving these ports open without rate limiting or intrusion prevention guarantees thousands of unauthorized authentication attempts every single day.

In this guide, we combine UFW (Uncomplicated Firewall) with Fail2ban to create an impenetrable, automated defense perimeter.

Phase 1: Establishing Strict UFW Firewall Rules

Default firewall rules should always follow a default-deny architecture where all incoming connections are blocked unless explicitly authorized.

# Reset UFW to pristine state
sudo ufw --force reset

# Set strict defaults
sudo ufw default deny incoming
sudo ufw default allow outgoing

# Authorize your management SSH port (use 22 or your custom port)
sudo ufw allow 22/tcp comment "SSH Management"

# Authorize standard web traffic
sudo ufw allow 80/tcp comment "HTTP Traffic"
sudo ufw allow 443/tcp comment "HTTPS Encrypted Traffic"

# Enable the firewall
sudo ufw enable

# Inspect active status with numbering
sudo ufw status numbered

Phase 2: Installing and Configuring Fail2ban

Fail2ban is an intrusion prevention framework that inspects system log files (such as /var/log/auth.log) for failed login patterns and dynamically updates firewall packet rules to ban offenders.

sudo apt update
sudo apt install -y fail2ban

Never edit /etc/fail2ban/jail.conf directly because system updates will overwrite your changes. Instead, create a local override configuration:

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local

Phase 3: Hardening the [DEFAULT] and [sshd] Jails

In /etc/fail2ban/jail.local, find the [DEFAULT] section and customize the ban parameters:

[DEFAULT]
# Whitelist your local home/office static IP so you never get locked out
ignoreip = 127.0.0.1/8 ::1 YOUR_HOME_STATIC_IP

# Ban time: 1 day (86400 seconds)
bantime = 86400

# Find time: Evaluation window of 10 minutes (600 seconds)
findtime = 600

# Max retries: 3 failed attempts within 10 minutes triggers an immediate ban
maxretry = 3

# Use UFW as the default banning action
banaction = ufw

Scroll down to the [sshd] section and ensure it is activated:

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3

Phase 4: Starting Fail2ban and Monitoring Banned Bots

Enable and start the Fail2ban systemd service:

sudo systemctl enable --now fail2ban
sudo systemctl status fail2ban

Inspect the live status of your SSH jail to view actively banned malicious IPs:

sudo fail2ban-client status sshd

If you or a colleague ever trigger a ban accidentally, unban the IP address with:

sudo fail2ban-client set sshd unbanip 198.51.100.25

Frequently Asked Questions (FAQ)

Can Fail2ban protect Nginx and WordPress login forms?

Yes! Fail2ban includes pre-configured filters for Nginx HTTP authentication and WordPress xmlrpc attacks. You can enable [nginx-http-auth] and [nginx-botsearch] in jail.local to ban bots attacking web forms.

Does Fail2ban consume high CPU resources?

Fail2ban uses inotify to watch log updates asynchronously, consuming less than 0.5% CPU under standard conditions.

Editorial Verdict: Pros & Cons

What We Love (Pros)

  • Automatically detects and permanently bans IP addresses attempting SSH dictionary attacks
  • Zero impact on legitimate user traffic and low CPU overhead
  • Essential foundation for compliance and data integrity

Drawbacks (Cons)

  • Accidentally triggering your own fail2ban filter can lock you out if SSH key is misconfigured
#UFW#Fail2ban#Linux Security#Firewall#Server Hardening
Alex Vance

Written by Alex Vance

Cloud Infrastructure Architect

Alex and the ServerCraft engineering lab run routine benchmarks on over 25 web hosting providers, maintaining strict objectivity and reproducibility.

Recommended Next Reads