UFW and Fail2Ban Setup
Security

Setting Up the UFW Firewall and Fail2Ban

Create UFW firewall rules on an Ubuntu/Debian server and automatically block brute-force attacks with Fail2Ban.

  • MerkezHost Technical Team
  • 4 min read
  • Level: Starter
  • Updated:

The cornerstone of server security is leaving only the ports you need open and automatically blocking repeated attack attempts. In this guide we'll configure the UFW (Uncomplicated Firewall) firewall on an Ubuntu/Debian server and block brute-force attacks with Fail2Ban.

Part 1: The UFW firewall

Install UFW

UFW usually comes pre-installed on Ubuntu. If not:

sudo apt update && sudo apt install -y ufw

Set the default policies

Denying all incoming connections and allowing outgoing ones is the safest starting point:

sudo ufw default deny incoming
sudo ufw default allow outgoing

Allow the required ports

Important

Always allow the SSH port before enabling UFW, otherwise your connection to the server will be cut. If you have changed the SSH port, enter your own port number.

sudo ufw allow OpenSSH          # or: sudo ufw allow 2222/tcp
sudo ufw allow 80/tcp           # HTTP
sudo ufw allow 443/tcp          # HTTPS

To allow access to a specific service only from your own IP address (for example MySQL):

sudo ufw allow from 203.0.113.10 to any port 3306 proto tcp

Enable UFW

sudo ufw enable
sudo ufw status verbose

Managing rules

sudo ufw status numbered        # List rules with numbers
sudo ufw delete 3               # Delete rule number 3
sudo ufw limit OpenSSH          # Rate-limit SSH (6+ attempts in 30 s are blocked)

Part 2: Fail2Ban

Fail2Ban monitors log files and blocks IP addresses that make many failed login attempts for a set period of time.

Installation

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

Configuration

Instead of editing the main configuration file directly, create a jail.local file so that your settings are preserved during updates:

sudo nano /etc/fail2ban/jail.local
[DEFAULT]
bantime  = 1h
findtime = 10m
maxretry = 5
backend  = systemd
banaction = ufw
ignoreip = 127.0.0.1/8 ::1

[sshd]
enabled = true
port    = ssh
# If you changed the SSH port: port = 2222

With these settings, an IP address that fails to log in 5 times within 10 minutes is blocked for 1 hour. Add your own static IP address to the ignoreip line to avoid accidentally locking yourself out.

sudo systemctl restart fail2ban

Check the status

sudo fail2ban-client status
sudo fail2ban-client status sshd

To unban an IP address that was blocked by mistake:

sudo fail2ban-client set sshd unbanip 203.0.113.10

Extra protection for Nginx

You can also block suspicious bot scans against Nginx. Add this to the jail.local file:

[nginx-botsearch]
enabled  = true
port     = http,https
logpath  = /var/log/nginx/access.log
backend  = auto
Info

Fail2Ban and UFW provide protection at the server level. Volumetric DDoS attacks, however, must be filtered at the network level before they reach your server. MerkezHost's infrastructure handles this with layered Corero + Stormwall DDoS protection.

Conclusion

Your server now exposes only the necessary ports and blocks brute-force attacks automatically. By applying this guide together with our SSH security guide, you can build a strong baseline layer of security.

Frequently Asked Questions

Will my SSH connection drop when I enable UFW?

If you don't allow the SSH port before enabling UFW, your connection will be cut. That's why you should always run ufw allow OpenSSH (or your custom port) first.

Does Fail2Ban block DDoS attacks?

No. Fail2Ban blocks brute-force login attempts. Volumetric DDoS attacks require protection at the network level; on MerkezHost infrastructure, this protection is provided in layers.

  • #ufw
  • #fail2ban
  • #firewall
  • #firewall rules
  • #brute force
Share:
Read More

Related Guides