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.
On this page
- Step 1: Set UFW Default Policies and LAN Rules
- Step 2: Watch Out for the Docker + UFW Bypass
- Step 3: Install and Configure Fail2ban (With the systemd Backend)
- Step 4: Know When to Graduate to CrowdSec
- Troubleshooting UFW and Fail2ban
- 1. fail2ban.service fails with No file(s) found for glob /var/log/auth.log
- 2. You accidentally banned your own remote IP
- 3. ufw status shows ports are open to Anywhere after adding a rule
- 4. UFW breaks container-to-internet DNS or bridge routing
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
Set UFW default policies
Deny all unsolicited incoming connections and allow outgoing traffic before opening required LAN ports.
Allow SSH and essential homelab ports
Scope SSH to your home subnet or Tailscale interface and enable UFW safely.
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.
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:
sudo apt update && sudo apt install -y ufwDo 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:
sudo ufw default deny incomingsudo ufw default allow outgoingNext, allow SSH—ideally scoped strictly to your home subnet (192.168.1.0/24; verify yours in our subnet calculator) instead of anywhere:
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:
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:
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:
sudo ufw enablesudo ufw status numberedExpected 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 LANStep 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):
- 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. - 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 proxyIf 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:
sudo apt install -y fail2banNever edit /etc/fail2ban/jail.conf directly—package updates overwrite it. Always create /etc/fail2ban/jail.local, which overrides only the settings you specify:
sudo nano /etc/fail2ban/jail.localPaste this clean baseline configuration:
[DEFAULT]backend = systemdignoreip = 127.0.0.1/8 ::1 192.168.1.0/24 100.64.0.0/10bantime = 1hfindtime = 10mmaxretry = 5banaction = ufw
[sshd]enabled = trueport = sshmaxretry = 4bantime = 2hWhat each setting does:
backend = systemdtells Fail2ban to read authentication failures straight fromjournaldrather than looking for/var/log/auth.log(missing by default on Debian 12 and Ubuntu 24.04 minimal installs).ignoreipprevents Fail2ban from ever banning localhost, your home LAN (192.168.1.0/24), or Tailscale’s CGNAT block (100.64.0.0/10). Adjust192.168.1.0/24if your router uses192.168.0.0/24or10.0.0.0/24.bantime = 1h,findtime = 10m,maxretry = 5: five failed attempts inside 10 minutes trigger a 1-hour ban.banaction = ufwmakes Fail2ban enforce bans cleanly via UFW deny rules instead of raw duplicate chains, sosudo ufw statusshows 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:
sudo systemctl enable --now fail2bansudo fail2ban-client statussudo fail2ban-client status sshdWhen 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:
sudo fail2ban-client set sshd unbanip 203.0.113.45Then 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:
sudo ufw delete 14. 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.