Installation and removal
Requirements
- Linux with nftables (
inettables, interval sets with timeouts). Tested on kernels 5.14, 6.1 and 6.12. - Root to change firewall rules.
- systemd and
journalctlfor SSH detection and the supplied unit. - Go 1.27.4 or newer to build. Nothing else is needed at runtime.
The nft command is not required: ipfence speaks netlink. It is useful for
looking at the result.
Install
Shell commands
scripts/build.sh # dist/ipfence-linux-{amd64,arm64}
sudo install -m 0755 dist/ipfence-linux-amd64 /usr/local/bin/ipfence
sudo install -m 0644 systemd/ipfence.service /etc/systemd/system/ipfence.service
sudo systemctl daemon-reload
sudo ipfence init
sudo systemctl enable --now ipfence
sudo ipfence status
sudo ipfence enable # only when the report looks rightIf the binary lives elsewhere, change ExecStart in a drop-in.
Files
| Path | Contents | Mode |
|---|---|---|
/etc/ipfence/config.toml |
your overrides | created 0600 by init |
/var/lib/ipfence/ipfence.db |
state (plus -wal, -shm) |
directory 0700, file 0600 |
/run/ipfence/control.sock, writer.lock |
runtime | directory 0700 |
The daemon refuses a configuration file that is a symlink, owned by another user or writable by group or others, and state directories that others can read.
The systemd unit
It runs as root because changing nftables over netlink needs CAP_NET_ADMIN.
Everything else is narrowed:
| Setting | Effect |
|---|---|
CapabilityBoundingSet=CAP_NET_ADMIN CAP_DAC_READ_SEARCH |
firewall changes and reading logs owned by other users; no other root powers |
RestrictAddressFamilies=AF_UNIX AF_NETLINK, IPAddressDeny=any |
no IP sockets at all |
ProtectSystem=strict, ProtectHome=yes, PrivateTmp=yes |
read-only filesystem except its state and runtime directories; logs under /home or /tmp are invisible |
NoNewPrivileges, MemoryDenyWriteExecute, SystemCallFilter=@system-service and related |
common hardening |
Trade-offs to know:
-
Without
CAP_SYS_PTRACEthe daemon cannot read other programs' open sockets or executable paths, so its status report omits "listening on".sudo ipfence initruns outside the sandbox and shows them. -
A configured hook inherits the sandbox, including the network block. To let a hook reach the network, add a drop-in (
systemctl edit ipfence):Configuration (INI)
[Service] RestrictAddressFamilies= IPAddressDeny=That also removes the guarantee that the daemon cannot open a connection.
-
Restart=on-failurewithRestartSec=10. After 20 starts within 5 minutes, counting your own restarts, systemd refuses to start it. Rules already loaded stay. Recover withsudo systemctl reset-failed ipfence && sudo systemctl start ipfence. -
There is deliberately no
ExecStop: stopping the unit keeps protection. -
Under SELinux (tested on Rocky 9, enforcing) the daemon runs as
unconfined_service_t; no policy module is shipped.
Upgrade
Shell commands
sudo install -m 0755 dist/ipfence-linux-amd64 /usr/local/bin/ipfence
sudo systemctl restart ipfenceRules stay loaded during the restart. The database migrates forward
automatically. An older binary refuses a newer database and leaves it alone, so
keep a copy of /var/lib/ipfence if you might roll back.
Remove
Shell commands
sudo ipfence disable --yes # 1. remove the nftables table
sudo systemctl disable --now ipfence # 2. stop the service
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 # 3. delete config and historyOrder matters: remove the table while the database still exists, because it holds
the identity that proves the table is yours. If you deleted the database first,
disable --yes still removes a well-formed ipfence table, or use
sudo nft delete table inet ipfence.