How to Configure Fail2Ban Safely on an Ubuntu Server Using firewalld and Docker


Setting up Fail2Ban is a great way to automatically block brute-force bots and malicious web scanners. However, copying a generic jail.local template off the internet often causes fail2ban.service to crash on startup—or worse, conflict with firewalld and lock you out of your own VPN.
This guide explains the four most common Fail2Ban pitfalls on modern Ubuntu servers (such as Oracle Cloud Ubuntu images) and provides a copy-pasteable configuration that works safely out of the box.

Why Generic Fail2Ban Configs Break
Before installing, watch out for these four traps:
  1. Missing Log Files Crash Fail2Ban: If you enable [nginx-http-auth] or [nginx-badbots] when Nginx isn't installed on the host (or runs inside Docker without mounted logs), Fail2Ban refuses to start with:
    ERROR: Failed during configuration: Have not found any log file for nginx-http-auth jail
  2. Backend Mismatch (systemd vs. auto): Modern Ubuntu uses systemd journald for SSH logs (backend = systemd), while web servers like Nginx write to plain text files (/var/log/nginx/*.log). Setting backend = systemd globally without setting backend = auto inside file-based jails prevents Fail2Ban from reading web server logs.
  3. firewalld Conflicts: If your server runs firewalld instead of UFW, Fail2Ban should use banaction = firewallcmd-rich-rules so bans integrate cleanly with your active firewall zones and Docker rules.
  4. Banning Your Own VPN Subnet: If you connect via WireGuard (10.8.0.0/24) or Tailscale, always add your VPN subnet to ignoreip so a few mistyped commands never lock you out.

Step 1: Install Fail2Ban and Systemd Python Bindings
On Ubuntu 22.04 and 24.04+, install both fail2ban and python3-systemd so Fail2Ban can read journald logs directly without needing /var/log/auth.log:
bash
sudo apt-get update
sudo apt-get install -y fail2ban python3-systemd
Use code with caution.

Step 2: Create a Safe, firewalld-Aware jail.local
Never edit /etc/fail2ban/jail.conf directly, as package updates overwrite it. Instead, create /etc/fail2ban/jail.local.
The script below configures SSH protection with firewalld integration, whitelists localhost and your WireGuard subnet (10.8.0.0/24), and only enables Nginx jails if the log files actually exist:
bash
sudo tee /etc/fail2ban/jail.local > /dev/null << 'EOF'
[DEFAULT]
# Ban offending IPs for 1 hour (3600 seconds)
bantime = 3600
findtime = 600
maxretry = 5

# Read SSH logs from systemd journal
backend = systemd

# Integrate natively with firewalld
banaction = firewallcmd-rich-rules
banaction_allports = firewallcmd-rich-rules

# Never ban localhost or your WireGuard VPN subnet
ignoreip = 127.0.0.1/8 ::1 10.8.0.0/24

[sshd]
enabled = true
port = ssh
filter = sshd
maxretry = 3
EOF

# Conditionally add Nginx jails ONLY if Nginx log files exist on the host
if [ -f /var/log/nginx/error.log ] && [ -f /var/log/nginx/access.log ]; then
sudo tee -a /etc/fail2ban/jail.local > /dev/null << 'EOF'

[nginx-http-auth]
enabled = true
filter = nginx-http-auth
port = http,https
backend = auto
logpath = /var/log/nginx/error.log

[nginx-limit-req]
enabled = true
filter = nginx-limit-req
port = http,https
backend = auto
logpath = /var/log/nginx/error.log
maxretry = 10

[nginx-badbots]
enabled = true
filter = nginx-badbots
port = http,https
backend = auto
logpath = /var/log/nginx/access.log
maxretry = 2
EOF
fi
Use code with caution.

Step 3: Start and Verify Fail2Ban
Enable the service at boot and verify that all active jails loaded cleanly:
bash
sudo systemctl enable --now fail2ban
sudo fail2ban-client status
Use code with caution.
Expected output:
text
Status
|- Number of jail:      1
`- Jail list:           sshd
Use code with caution.
(If Nginx logs were present on the host, Jail list will also include nginx-http-auth, nginx-limit-req, and nginx-badbots).

Step 4: What If Your Web Server Runs in Docker (or Has Logs Disabled)?
If port 80 or 443 is handled by a Docker container (like Nginx Proxy Manager or Dockerized Nginx), Fail2Ban on the host cannot see inside the container unless you share the log folder:
  1. Check what is listening on port 443:
    bash
    sudo ss -tulnp | grep :443
    
    Use code with caution.
  2. Mount the container's logs to /var/log/nginx:
    In your docker-compose.yml, bind the container log directory to the host:
    yaml
    volumes:
      - /var/log/nginx:/var/log/nginx
    
    Use code with caution.
    (Note: Ensure your Nginx config writes actual files to /var/log/nginx/access.log and error.log rather than symlinking to /dev/stdout, because Fail2Ban needs real files on disk to tail).
  3. Important Warning About Docker & firewalld Bans:
    By default, firewallcmd-rich-rules blocks traffic in firewalld's INPUT chain, whereas Docker routes container traffic through the FORWARD (DOCKER-USER) chain. To ensure Fail2Ban blocks attackers from reaching Docker containers too, set:
    ini
    banaction = firewallcmd-rich-rules[chain="DOCKER-USER"]
    
    Use code with caution.
    (Or if you use Cloudflare Proxy, use Fail2Ban's cloudflare API ban action instead of local firewall bans so you don't accidentally block Cloudflare's edge servers).

Useful Management Commands
  • Check banned IPs for SSH:
    bash
    sudo fail2ban-client status sshd
    
    Use code with caution.
  • Manually unban an IP:
    bash
    sudo fail2ban-client set sshd unbanip <IP_ADDRESS>
    
    Use code with caution.
  • Test configuration syntax before restarting:
    bash
    sudo fail2ban-client -t
    
    Use code with caution.
Would you like to check what is currently listening on port 443 (sudo ss -tulnp | grep :443) on your VM so we can add the exact web jail for it?

Comments

Popular posts from this blog

HOW TO INSTALL DOCKER IN UBUNTU Ubuntu 22.04 LTS

Fixing Git Portable SSL Certificate Errors on Windows

Installing VM Ware Workstation required tools from build vmmon and vmnet