Add a web UI to fail2ban with Fail2ban UI, Caddy and Keycloak login

Published

fail2ban watches log files and bans IP addresses that keep failing to log in. It is excellent at that job, but everything happens in config files and the command line. Fail2ban UI adds a web interface on top: you can see ban and unban events as they happen, look up the owner of an address, read the logs behind a jail, and ban or unban by hand.

This guide builds the setup in Docker, assuming fail2ban already runs in a container and Caddy already acts as your reverse proxy. The result:

  • the UI runs in its own small stack and talks to fail2ban through a shared socket and config folder
  • fail2ban sends every ban and unban to the UI through a callback action
  • the UI is published only on a private hostname with real HTTPS, using Caddy and the Cloudflare DNS challenge
  • logins go through Keycloak (OpenID Connect)
  • the fail2ban configuration lives in a private git repository

Everything uses placeholders such as f2b.int.example.com. Replace them with your own names.

What you need

  • fail2ban running in Docker, with its jails already working. This guide uses the LinuxServer.io image, which has a clear config layout.
  • Caddy running in Docker on a shared network called proxy (see Run Caddy as a standalone reverse proxy in Docker)
  • A domain on Cloudflare, plus an API token that can edit its DNS, for the certificate
  • A local DNS server where you can add a private hostname (see Run AdGuard Home in Docker)
  • An OIDC provider if you want single sign-on. This guide uses Keycloak.

How the pieces connect

  1. The fail2ban container manages firewall rules and exposes a control socket, fail2ban.sock.
  2. The UI container mounts that socket and fail2ban's config folder, so it can read jails and change settings.
  3. A custom action in each jail posts ban and unban events to the UI's callback address, authenticated with a shared secret.
  4. Caddy publishes the UI on an internal hostname and handles HTTPS.

1. A tidy fail2ban layout

The UI edits the same configuration folder that fail2ban uses, so give fail2ban one clean folder on the host:

/opt/fail2ban/
├── compose.yml
├── .env
├── build/               optional local image (see below)
├── run/                 shared folder for fail2ban.sock
└── fail2ban/            fail2ban configuration
    ├── jail.d/
    ├── filter.d/
    ├── action.d/
    └── db/

A few choices worth making:

  • A small local image. The LinuxServer image ships sample jails and filters. I build a tiny image on top of it that removes those samples, so only my own jails exist and the UI never lists jails I did not write. Leave the default actions in place.
  • Mount the Caddy logs read-only into the fail2ban container, for example /opt/caddy/caddy/logs to /var/log/caddy.
  • Set the time zone so log timestamps and bans line up.

The fail2ban service needs two extra mounts compared with a plain setup: the shared socket folder, and its config at the path the UI expects.

services:
  fail2ban:
    build: ./build                # or: image: lscr.io/linuxserver/fail2ban:latest
    container_name: fail2ban
    restart: unless-stopped
    network_mode: host            # lets fail2ban manage the host firewall
    cap_add:
      - NET_ADMIN
      - NET_RAW
    environment:
      - TZ=Etc/UTC                # use your own time zone
    volumes:
      - ./fail2ban:/config/fail2ban
      - ./run:/var/run/fail2ban
      - /opt/caddy/caddy/logs:/var/log/caddy:ro

Start it and confirm your jails still work with docker exec fail2ban fail2ban-client status.

2. The UI stack

Create a separate folder so the UI can be updated or removed on its own:

sudo mkdir -p /opt/fail2ban-ui/fail2ban-ui
cd /opt/fail2ban-ui

Secrets go in /opt/fail2ban-ui/.env. Generate each value instead of inventing one:

CALLBACK_SECRET=<output of: openssl rand -hex 32>
OIDC_SESSION_SECRET=<output of: openssl rand -base64 32>
OIDC_CLIENT_SECRET=<added later, from Keycloak>
sudo chmod 600 .env

Now compose.yml:

services:
  fail2ban-ui:
    image: swissmakers/fail2ban-ui:latest
    container_name: fail2ban-ui
    restart: unless-stopped
    env_file: .env
    environment:
      - PORT=3080
      - BIND_ADDRESS=0.0.0.0
      - CALLBACK_URL=http://127.0.0.1:3080
      - UPDATE_CHECK=false
      - DISABLE_EXTERNAL_IP_LOOKUP=true
    ports:
      - "127.0.0.1:3080:3080"     # reachable by the host-networked fail2ban only
    volumes:
      - ./fail2ban-ui:/config                  # database, settings, keys
      - /opt/fail2ban/fail2ban:/etc/fail2ban   # same config fail2ban uses
      - /opt/fail2ban/run:/var/run/fail2ban    # the control socket
      - /opt/caddy/caddy/logs:/var/log/caddy:ro
    networks:
      - proxy

networks:
  proxy:
    external: true

What these settings do:

  • CALLBACK_URL is the address fail2ban uses to report events. Because the fail2ban container uses host networking, it cannot resolve Docker names, so the UI publishes its port on the loopback address only. If your fail2ban uses normal Docker networking instead, put it on the proxy network and use http://fail2ban-ui:3080.
  • CALLBACK_SECRET is a shared token that the callback must present. If you leave it unset, the UI generates one on first start, but setting it yourself makes the setup repeatable.
  • UPDATE_CHECK=false and DISABLE_EXTERNAL_IP_LOOKUP=true stop the UI from contacting outside services, which suits a privacy-minded setup.
  • The log mount is read-only, so the UI can test log paths but cannot change logs.

Start it:

docker compose up -d
docker logs -f fail2ban-ui

If the UI starts but cannot reach fail2ban, the cause is almost always file permissions on the socket folder or config folder. Make sure the user the UI runs as can read and write both. Running the UI as a normal, non-root user is the safer choice, so adjust folder ownership to match rather than running it as root.

3. Publish it privately with Caddy

The UI can change firewall rules, so it should never be on the public internet. Give it an internal-only hostname that has no public DNS record:

  1. In your local DNS server, add a rewrite so f2b.int.example.com resolves to the private address of the machine running Caddy.
  2. Create no public record for that name.

Caddy cannot use the normal HTTP challenge for a name that is not public, so it needs the DNS challenge, which proves ownership by creating a temporary record in Cloudflare. That needs a Caddy build with the Cloudflare DNS plugin. Replace the stock image with a small local build:

FROM caddy:2-builder AS builder
RUN xcaddy build --with github.com/caddy-dns/cloudflare

FROM caddy:2
COPY --from=builder /usr/bin/caddy /usr/bin/caddy

In the Caddy compose file, use build: . instead of image: caddy:2. Put the token in Caddy's .env:

CF_API_TOKEN=your_cloudflare_token

Create the token from the Edit zone DNS template, scoped to your one zone. Then add the site file sites/f2b.caddy:

f2b.int.example.com {
    @internal remote_ip private_ranges
    handle @internal {
        tls {
            dns cloudflare {env.CF_API_TOKEN}
        }
        reverse_proxy fail2ban-ui:3080
    }
    handle {
        respond "Forbidden" 403
    }
}

Rebuild and reload Caddy. Open https://f2b.int.example.com from inside your network, and you should see the UI with a valid certificate.

4. Connect fail2ban to the UI

Events only show up once fail2ban reports them. In the UI's settings, open the fail2ban server entry and make sure it points at the local socket, then enable the callback action. The UI provides an action definition that posts ban and unban events to the callback address with your secret. In my setup it is named ui-custom-action, but use whatever name your version shows.

Then add that action to each jail. A jail that already works might look like this:

[web-login]
enabled  = true
filter   = web-login
logpath  = /var/log/caddy/web.log
maxretry = 5
findtime = 10m
bantime  = -1
ignoreip = 127.0.0.1/8 ::1 192.168.1.0/24
action   = %(action_)s
           ui-custom-action

bantime = -1 makes bans permanent until you unban by hand. The ignoreip line keeps your own network from being banned. The extra line under action is the only change.

Restart, do not reload. In my setup a fail2ban-client reload did not pick up the new action, and a full container restart did:

docker restart fail2ban

5. Test it end to end

Ban a documentation address, which belongs to no real machine, and watch the UI:

docker exec fail2ban fail2ban-client set web-login banip 203.0.113.99

A ban event should appear in the UI within a second or two. Check the jail's log view and the address lookup too. Then clean up:

docker exec fail2ban fail2ban-client set web-login unbanip 203.0.113.99

If the ban works in fail2ban but nothing shows in the UI, the callback is not arriving. See the troubleshooting section.

6. Add Keycloak login

Without a login, anyone who can reach the hostname controls your firewall rules, so turn on OIDC.

In Keycloak:

  1. Create a client with the OpenID Connect protocol and the client ID fail2ban-ui.
  2. Turn on Client authentication so it is a confidential client.
  3. Set the valid redirect URI to https://f2b.int.example.com/auth/callback.
  4. Copy the client secret from the Credentials tab into OIDC_CLIENT_SECRET in the UI's .env.
  5. Make sure the users who should have access exist and have an email address.

In the UI's compose.yml, add:

    environment:
      - OIDC_ENABLED=true
      - OIDC_PROVIDER=keycloak
      - OIDC_ISSUER_URL=https://auth.example.com/realms/main
      - OIDC_CLIENT_ID=fail2ban-ui
      - OIDC_REDIRECT_URL=https://f2b.int.example.com/auth/callback

The client secret and the session secret come from the .env file you already created. Apply it and test:

docker compose up -d

Open the UI in a private window. You should be sent to Keycloak, and returned to the dashboard after login. The ban and unban callbacks from fail2ban keep working, because they use the shared secret rather than a login.

If a redirect error appears, the redirect URI in Keycloak must match OIDC_REDIRECT_URL exactly, including https and the path.

7. Keep the config in git

Your jails, filters and actions are valuable and easy to break, so version them. In /opt/fail2ban/fail2ban:

git init
printf 'db/\n*.sqlite3\n' > .gitignore
git add .
git commit -m "fail2ban config"

Push it to a private repository only. Keep the following out of git:

  • the database and any .env file
  • tokens or keys
  • anything that reveals your real hostnames or network layout

Commit after each change made through the UI as well, since the UI edits the same files.

Security checklist

  • The UI controls your firewall, so keep it on a private hostname, behind Keycloak login, with no public DNS record.
  • Never publish port 3080 beyond the loopback address.
  • Keep secrets in .env files with chmod 600.
  • Use a scoped Cloudflare token that can edit DNS for one zone only.
  • Put your own network in ignoreip, so a mistake cannot ban you.
  • Update the images regularly, and test jails after each update.

Troubleshooting

  • The UI cannot connect to fail2ban: check that the same socket folder is mounted in both containers, and that the UI's user can read and write it.
  • Jails are missing in the UI: the UI's config mount is pointing at the wrong folder. It must be the same folder fail2ban actually uses.
  • Bans work but no events appear: the action is not in the jail, the callback address is wrong, or the container was reloaded instead of restarted. Check CALLBACK_URL is reachable from the fail2ban container, and that the secrets match.
  • Callbacks are rejected: the secret on the fail2ban side differs from the one in the UI's .env.
  • Certificate errors on the private hostname: the DNS challenge failed. Read docker logs caddy and check that the Cloudflare token can edit DNS for that zone.
  • A 403 from Caddy: you are connecting from a public address, so test from inside your network.
  • The Keycloak redirect fails: the redirect URI or issuer URL does not match what the UI is configured with.
  • The log viewer is empty: the log folder is not mounted at the same path in the UI, or the jail's logpath points somewhere else.

Summary

Fail2ban UI turns fail2ban's text files and commands into a dashboard without changing how it protects you. Share the socket and config folder, add a callback action to each jail, publish the UI on a private hostname through Caddy, and put Keycloak login in front of it. With the config in git, every change is easy to review and roll back.

← All guides