ipfence home page local-first intrusion prevention for Linux

Manual pages

Everyday tasks

Recipes. Commands that change something need sudo.

See what is happening

Shell commands

sudo ipfence status                      # the whole picture
sudo ipfence status --json               # for scripts
sudo journalctl -u ipfence -f            # decisions as they are made
sudo journalctl -u ipfence | grep 'event=ban'

A decision in the log:

Plain text

level=INFO msg=decision event=ban address=203.0.113.50 service=ssh reason="5 SSH authentication failures within 10m (latest: failed-password)"

event is ban, or observe while enforcement is off.

Block an address yourself

You must choose how long, on purpose:

Terminal session

$ sudo ipfence ban 198.51.100.200 --permanent
banned 198.51.100.200 with no expiry

$ sudo ipfence ban 198.51.100.0/24 --for 7d --reason "abusive network"
banned 198.51.100.0/24 until 2026-12-06T02:43:16Z
  note: a network ban can block unrelated devices and users

Durations: 30s, 15m, 6h, 7d. Banning something already banned replaces its expiry.

Refusals you might meet

Terminal session

$ sudo ipfence ban 127.0.0.1 --for 1h
ipfence: refused: 127.0.0.1 includes loopback addresses

$ sudo ipfence ban 203.0.113.1 --for 1h
ipfence: refused: 203.0.113.1 is an address of this host

$ sudo ipfence ban 10.0.2.2 --for 1h
ipfence: 10.0.2.2 includes 10.0.2.2, your current SSH client; use --force if you mean it

$ sudo ipfence ban 198.51.100.7 --for 1h
ipfence: refused: 198.51.100.7 is covered by the allowlist entry 198.51.100.7; remove that entry first (allow --remove, or the config file)

Remove a ban

Terminal session

$ sudo ipfence unban 203.0.113.50
unbanned 203.0.113.50 (this does not allowlist it)

If the address is only blocked because it falls inside a range:

Terminal session

$ sudo ipfence unban 198.51.100.7
ipfence: not banned: 198.51.100.7 has no ban of its own; it is covered by 198.51.100.0/24 (unban that, or allow this address)

An address can have its own ban and sit inside a banned range. Unbanning it removes only its own, and says so: note: still blocked by the network ban 198.51.100.0/24.

To also forget its repeat-offense count: --reset-history.

Make sure an address is never blocked

Terminal session

$ sudo ipfence allow 203.0.113.50 --note "false alarm"
allowlisted 203.0.113.50 on this host (all services) until removed
  removed the ban on 203.0.113.50

$ sudo ipfence allow 198.51.100.7 --note "partner office"
allowlisted 198.51.100.7 on this host (all services) until removed
  note: the ban 198.51.100.0/24 stays, with 198.51.100.7 exempt from it

$ sudo ipfence allow --remove 203.0.113.50
removed the allowlist entry 203.0.113.50

An allow entry covers all services on this machine and survives reboots. You can instead list addresses in the settings file:

Settings file (TOML)

[allow]
addresses = ["203.0.113.5", "2001:db8:abcd::/64"]

I am locked out

Get in another way (provider console, physical keyboard, or any address that still works), then use one of these, gentlest first:

Shell commands

sudo ipfence unban <your address>
sudo ipfence allow <your address>
sudo ipfence disable --yes
sudo systemctl stop ipfence && sudo nft delete table inet ipfence

All four work even if the daemon is not running. A first automatic ban also lifts by itself after 15 minutes.

If you cannot find which range is blocking you, sudo ipfence status lists every active ban.

Change settings

Edit /etc/ipfence/config.toml, then restart (there is no live reload):

Shell commands

sudo systemctl restart ipfence
sudo journalctl -u ipfence -n 5          # a mistake is reported here

Restarting keeps all bans in place. A typo is an error, not ignored. Writing [alow] instead of [allow] gives:

Plain text

ipfence: config: unknown setting(s): alow, alow.addresses

To check a file before restarting, run any read-only command with it:

Shell commands

sudo ipfence status --config /etc/ipfence/config.toml

Be stricter or more lenient

Settings file (TOML)

[ssh]
threshold = 3          # ban after 3 failures
window = "30m"         # counted over 30 minutes

[policy]
durations = ["1h", "1d", "30d"]
forget_after = "90d"

Ban persistent offenders forever (opt in)

Settings file (TOML)

[policy]
permanent_after = 5

From the fifth ban onward, an address is banned with no expiry, only if all the evidence was high confidence (non-existent accounts, forbidden accounts, or probes, not just a wrong password for a real user).

Protect Nginx or Caddy

See Getting started, step 8 and Nginx, Caddy and proxies.

Run behind a CDN or load balancer

ipfence cannot block visitors for you there. It can tell you who is probing:

Settings file (TOML)

[proxy]
trusted = ["10.0.0.2"]        # your proxy's address as this server sees it

[nginx]
enabled = true
clients = "proxied"

SSH protection is unaffected if SSH is reached directly.

Get a notification

ipfence can run a program of yours after a decision. Create it, owned by root and not writable by others:

Shell commands

sudo tee /usr/local/bin/ipfence-notify >/dev/null <<'EOF'
#!/bin/sh
# Receives one JSON object on standard input.
cat >>/var/log/ipfence-notify.log
EOF
sudo chmod 755 /usr/local/bin/ipfence-notify

Settings file (TOML)

[hook]
command = ["/usr/local/bin/ipfence-notify"]
fields = ["event", "address", "service", "reason", "expires"]

The service sandbox also applies to the hook: it can only write where the sandbox allows (not /var/log by default) and cannot use the network. To permit writing, add a drop-in with sudo systemctl edit ipfence:

Configuration (INI)

[Service]
ReadWritePaths=/var/log

For network access see Installation. A hook that uses the network sends data off your machine.

Check why a line was or was not counted

Shell commands

sudo ipfence test "paste the log line here"
sudo ipfence test --service nginx "$(sudo tail -n1 /var/log/nginx/access.log)"
sudo journalctl -u ssh -n1 -o json | sudo ipfence test -

The last form also applies the journal sender checks. On RHEL-family systems the unit is sshd.

Turn blocking off temporarily

Shell commands

sudo ipfence disable --yes      # removes the rules, keeps the records
sudo ipfence enable             # restores bans that have not expired

Upgrade

Shell commands

sudo install -m 0755 ipfence-linux-amd64 /usr/local/bin/ipfence
sudo systemctl restart ipfence

Back up and restore

State is one directory. Stop first so the files are consistent:

Shell commands

sudo systemctl stop ipfence
sudo tar -C /var/lib -czf ipfence-state.tgz ipfence
sudo cp /etc/ipfence/config.toml config.toml.bak
sudo systemctl start ipfence

Restoring the same directory keeps the same identity, so the firewall table is still recognized. Restoring onto another machine moves the history with it.

Uninstall

Shell commands

sudo ipfence disable --yes
sudo systemctl disable --now ipfence
sudo rm /etc/systemd/system/ipfence.service /usr/local/bin/ipfence
sudo systemctl daemon-reload
sudo rm -rf /etc/ipfence /var/lib/ipfence /run/ipfence