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:
- 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 - Backend Mismatch (
systemdvs.auto): Modern Ubuntu usessystemdjournald for SSH logs (backend = systemd), while web servers like Nginx write to plain text files (/var/log/nginx/*.log). Settingbackend = systemdglobally without settingbackend = autoinside file-based jails prevents Fail2Ban from reading web server logs. firewalldConflicts: If your server runsfirewalldinstead of UFW, Fail2Ban should usebanaction = firewallcmd-rich-rulesso bans integrate cleanly with your active firewall zones and Docker rules.- Banning Your Own VPN Subnet: If you connect via WireGuard (
10.8.0.0/24) or Tailscale, always add your VPN subnet toignoreipso 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.localNever 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:- Check what is listening on port
443:bashsudo ss -tulnp | grep :443Use code with caution. - Mount the container's logs to
/var/log/nginx:
In yourdocker-compose.yml, bind the container log directory to the host:(Note: Ensure your Nginx config writes actual files toyamlvolumes: - /var/log/nginx:/var/log/nginxUse code with caution./var/log/nginx/access.loganderror.lograther than symlinking to/dev/stdout, because Fail2Ban needs real files on disk to tail). - Important Warning About Docker &
firewalldBans:
By default,firewallcmd-rich-rulesblocks traffic infirewalld'sINPUTchain, whereas Docker routes container traffic through theFORWARD(DOCKER-USER) chain. To ensure Fail2Ban blocks attackers from reaching Docker containers too, set:(Or if you use Cloudflare Proxy, use Fail2Ban'sinibanaction = firewallcmd-rich-rules[chain="DOCKER-USER"]Use code with caution.cloudflareAPI 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 sshdUse 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 -tUse 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
Post a Comment