UFW and Fail2ban on a Home Server: Step-by-Step Firewall & Ban Setup

Configure UFW and Fail2ban on Ubuntu 24.04 and Debian 12, avoid the Docker iptables bypass trap, and prevent locking yourself out over SSH or Tailscale.

Configure UFW and Fail2ban on Ubuntu or Debian

Set up a default-deny UFW firewall, close the Docker port bypass, and enable Fail2ban with systemd logging.

Estimated time: PT20M

  1. Set UFW default policies

    Deny all unsolicited incoming connections and allow outgoing traffic before opening required LAN ports.

  2. Allow SSH and essential homelab ports

    Scope SSH to your home subnet or Tailscale interface and enable UFW safely.

  3. Bind internal Docker containers to localhost

    Publish internal container ports as 127.0.0.1:PORT:PORT so Docker does not bypass UFW on your LAN.

  4. Configure Fail2ban with backend = systemd

    Create /etc/fail2ban/jail.local with ignoreip for LAN/Tailscale and verify active jails with fail2ban-client.

Hardening SSH keys stops password guessing, but your server still needs a bouncer deciding which network ports can talk at all—and temporarily blocking IPs that keep hammering exposed endpoints. On Ubuntu and Debian, UFW (Uncomplicated Firewall) handles port filtering cleanly while Fail2ban watches failed authentication attempts and adds temporary bans automatically. Together they consume around 25–40 MB of RAM and practically zero CPU.

This guide walks through configuring both cleanly on Ubuntu 24.04 LTS or Debian 12—including the notorious Docker + UFW bypass and the auth.log systemd fix that trip up almost everyone the first time. If you have not locked down SSH yet, start with our Ubuntu & Debian server hardening guide first.

Step 1: Set UFW Default Policies and LAN Rules

UFW is preinstalled on Ubuntu Server and available with one command on Debian:

Terminal window
sudo apt update && sudo apt install -y ufw

Do not run ufw enable yet. Always define your defaults and allow SSH before turning the firewall on, or you will sever your current SSH terminal on the spot.

First, set default policies: deny everything inbound that you did not ask for, allow all outbound traffic from the server:

Terminal window
sudo ufw default deny incoming
sudo ufw default allow outgoing

Next, allow SSH—ideally scoped strictly to your home subnet (192.168.1.0/24; verify yours in our subnet calculator) instead of anywhere:

Terminal window
sudo ufw allow from 192.168.1.0/24 to any port 22 proto tcp comment "SSH from home LAN"

If you use Tailscale for remote access, also allow traffic entering through the tailscale0 tunnel interface so you can still SSH in when travelling:

Terminal window
sudo ufw allow in on tailscale0 to any port 22 proto tcp comment "SSH over Tailscale"

Now open only the services other devices on your LAN actually talk to directly—for example HTTP/HTTPS (80/443) for Nginx Proxy Manager, or Jellyfin (8096) and SMB (445) scoped to your home network:

Terminal window
sudo ufw allow from 192.168.1.0/24 to any port 80,443 proto tcp comment "Reverse proxy LAN"
sudo ufw allow from 192.168.1.0/24 to any port 8096 proto tcp comment "Jellyfin LAN"
sudo ufw allow from 192.168.1.0/24 to any port 445 proto tcp comment "Samba SMB LAN"

Before enabling, double-check you did not restrict yourself out of your current IP (for instance if your laptop is on a 192.168.10.x VLAN instead of 192.168.1.x). Then activate UFW and check numbered rules:

Terminal window
sudo ufw enable
sudo ufw status numbered

Expected output looks clean and readable:

Status: active
To Action From
-- ------ ----
[ 1] 22/tcp ALLOW IN 192.168.1.0/24 # SSH from home LAN
[ 2] 22/tcp on tailscale0 ALLOW IN Anywhere # SSH over Tailscale
[ 3] 80,443/tcp ALLOW IN 192.168.1.0/24 # Reverse proxy LAN

Step 2: Watch Out for the Docker + UFW Bypass

This is the single biggest firewall misconception in self-hosting: standard UFW rules do not block Docker-published ports.

When you write ports: - "5432:5432" in a docker-compose.yml file, Docker edits iptables directly in the DOCKER chain, forwarding external LAN/WAN packets straight to the container before UFW’s INPUT rules even inspect them. A database or Portainer dashboard you thought UFW was hiding stays wide open on 0.0.0.0.

Fix it with two clean habits (check which ports each app exposes in our default port lookup):

  1. Never publish database ports at all unless external tools on another host genuinely connect to them. Internal containers talk over a shared Docker Compose network without any ports: mapping on the host.
  2. Bind admin UIs and internal proxied apps to 127.0.0.1:
services:
postgres:
image: postgres:16-alpine
# No ports: block at all — reachable only inside the Docker network
uptime-kuma:
image: louislam/uptime-kuma:1
ports:
- "127.0.0.1:3001:3001" # Reachable only from localhost / reverse proxy

If Nginx Proxy Manager runs in network_mode: host or proxies via Docker network name (http://uptime-kuma:3001), nothing outside your server can hit port 3001 directly, regardless of Docker’s iptables behavior.

Step 3: Install and Configure Fail2ban (With the systemd Backend)

If you ever forward SSH (22), WireGuard/Vaultwarden (443), or run a VPS alongside your home server, automated scanners will probe authentication endpoints within minutes. Fail2ban reads log streams and bans repeat offenders dynamically.

Install it:

Terminal window
sudo apt install -y fail2ban

Never edit /etc/fail2ban/jail.conf directly—package updates overwrite it. Always create /etc/fail2ban/jail.local, which overrides only the settings you specify:

Terminal window
sudo nano /etc/fail2ban/jail.local

Paste this clean baseline configuration:

[DEFAULT]
backend = systemd
ignoreip = 127.0.0.1/8 ::1 192.168.1.0/24 100.64.0.0/10
bantime = 1h
findtime = 10m
maxretry = 5
banaction = ufw
[sshd]
enabled = true
port = ssh
maxretry = 4
bantime = 2h

What each setting does:

  • backend = systemd tells Fail2ban to read authentication failures straight from journald rather than looking for /var/log/auth.log (missing by default on Debian 12 and Ubuntu 24.04 minimal installs).
  • ignoreip prevents Fail2ban from ever banning localhost, your home LAN (192.168.1.0/24), or Tailscale’s CGNAT block (100.64.0.0/10). Adjust 192.168.1.0/24 if your router uses 192.168.0.0/24 or 10.0.0.0/24.
  • bantime = 1h, findtime = 10m, maxretry = 5: five failed attempts inside 10 minutes trigger a 1-hour ban.
  • banaction = ufw makes Fail2ban enforce bans cleanly via UFW deny rules instead of raw duplicate chains, so sudo ufw status shows active bans alongside your rules.
  • [sshd] enables protection for OpenSSH with a slightly tighter 4-try limit and 2-hour cooldown.

Enable and start Fail2ban, then check its status:

Terminal window
sudo systemctl enable --now fail2ban
sudo fail2ban-client status
sudo fail2ban-client status sshd

When active, fail2ban-client status sshd shows Currently failed, Total failed, and a live Banned IP list.

Step 4: Know When to Graduate to CrowdSec

Fail2ban is ideal for protecting host daemons like sshd, Postfix, or bare-metal services reading systemd-journald. Once you expose multiple Docker web apps through a reverse proxy, managing dozens of custom Fail2ban regex filters gets tedious—that is where our CrowdSec + Nginx Proxy Manager guide takes over with shared threat intelligence blocklists.

Troubleshooting UFW and Fail2ban

1. fail2ban.service fails with No file(s) found for glob /var/log/auth.log

You forgot backend = systemd under [DEFAULT] (or [sshd]) in /etc/fail2ban/jail.local. Add backend = systemd, then run sudo systemctl restart fail2ban. Verify logs cleanly via sudo journalctl -u fail2ban -n 30.

2. You accidentally banned your own remote IP

Log in via local keyboard/console, another Tailscale node, or your LAN workstation (if whitelisted) and run:

Terminal window
sudo fail2ban-client set sshd unbanip 203.0.113.45

Then add that static IP or subnet to ignoreip in /etc/fail2ban/jail.local and reload (sudo systemctl reload fail2ban).

3. ufw status shows ports are open to Anywhere after adding a rule

UFW evaluates rules top-to-bottom. If you previously ran sudo ufw allow 22/tcp (creating an Anywhere rule at [1]) and later added allow from 192.168.1.0/24, the broader rule still matches first. Run sudo ufw status numbered and delete the wide rule by index:

Terminal window
sudo ufw delete 1

4. UFW breaks container-to-internet DNS or bridge routing

Ubuntu/Debian UFW works smoothly with Docker bridge networks for outbound container traffic as long as DEFAULT_FORWARD_POLICY="DROP" isn’t blocking internal container hops you explicitly added, or you bind internal apps to 127.0.0.1. Verify container connectivity after enabling UFW with docker exec <container> ping -c 2 1.1.1.1.

Frequently Asked Questions

Do I need UFW if my home router already blocks incoming traffic?

Yes. Your router protects the WAN perimeter, not your internal LAN. A compromised IoT gadget, guest phone, or misconfigured router port forward can still reach your server directly unless a host firewall like UFW filters local ports too.

Why does Docker bypass my UFW rules by default?

Docker inserts NAT and FORWARD rules directly into the iptables DOCKER chain ahead of UFW's INPUT chain. When you publish -p 8080:8080 without a bind IP, Docker opens 0.0.0.0:8080 to the whole network even if UFW denies incoming traffic on port 8080.

Why does Fail2ban fail with 'No file(s) found for glob /var/log/auth.log'?

Ubuntu 24.04 and Debian 12 minimal installs use systemd-journald instead of writing traditional text logs to /var/log/auth.log. Adding backend = systemd inside /etc/fail2ban/jail.local fixes the error immediately.

Will Fail2ban lock me out over Tailscale?

Only if you leave Tailscale's CGNAT subnet out of ignoreip and type wrong credentials repeatedly (for example from a misconfigured script). Adding 192.168.1.0/24 and 100.64.0.0/10 to ignoreip keeps your LAN and tailnet safe.

Portrait of Guillermo

Written by Guillermo

IT Student & Home Server Enthusiast

Building private clouds to break free from Big Tech. I document everything I learn about home servers and self-hosting on MyHomeServerGuide.

More about the author