--- type: Guide purpose: Load the nftables host firewall without breaking Docker's rules. answers: - "How is the host firewall configured?" --- # 08 — Firewall (nftables) nftables host firewall. fail2ban and Gitea hardening follow in [09-fail2ban.md](09-fail2ban.md). ## Prerequisites - Gitea running and accessible (from [07-gitea-setup.md](07-gitea-setup.md)) - nftables installed (from [01-arch-install.md](01-arch-install.md)) ## 1. nftables firewall Create `/etc/nftables.conf`: ```bash sudo vim /etc/nftables.conf ``` Replace contents with: ```nft #!/usr/sbin/nft -f # Do NOT put `flush ruleset` here. Arch's iptables is the nf_tables backend # (`iptables --version` reports `nf_tables`), so Docker's own DNAT and forward # rules live in this same ruleset. Flushing everything deletes them, and the # published ports 80/443/2222 stop answering until `systemctl restart docker`. # Replace only the table this file owns: table inet filter delete table inet filter table inet filter { chain input { type filter hook input priority 0; policy drop; # established/related connections ct state established,related accept # loopback iif lo accept # ICMP (ping) ip protocol icmp accept ip6 nexthdr icmpv6 accept # admin SSH tcp dport 2234 accept # HTTP and HTTPS (Caddy) tcp dport { 80, 443 } accept # Gitea SSH tcp dport 2222 accept } chain forward { type filter hook forward priority 0; policy accept; } chain output { type filter hook output priority 0; policy accept; } } ``` The bare `table inet filter` line before the `delete` is not redundant — it creates the table if it does not exist, so `delete` cannot fail on a first run or after a reboot. Without it the file errors out half-applied. What this chain does and does not cover: `input` filters traffic addressed to **the host**, which is admin SSH on 2234. The container ports (80, 443, 2222) arrive as DNAT and traverse `forward`, not `input` — Docker's own rules govern those. So loosening or tightening `input` will not close a published container port, and a mistake here shows up as *SSH lockout*, not as Gitea going down. Apply and enable. This is the point where the firewall goes on for the first time — [01-arch-install.md](01-arch-install.md) deliberately left it disabled so the port change in [03](03-ssh-hardening.md) could not lock you out. **Set a dead-man switch first.** One typo in `input` and 2234 is gone with no console on a headless box. This schedules an unconditional revert in 5 minutes, which you cancel by hand once you have proven you are still in: ```bash sudo systemd-run --on-active=300 --unit=nft-panic \ /usr/sbin/nft delete table inet filter sudo nft -f /etc/nftables.conf ``` Now, **from a second terminal on your local machine** — a new connection, not the one already open, since an established session keeps working via `ct state established` even when the rule that allowed it is gone: ```bash ssh -p 2234 -o BatchMode=yes admin@192.168.1.50 'echo STILL_IN' ``` If that prints `STILL_IN`, cancel the revert and make the ruleset permanent: ```bash sudo systemctl stop nft-panic.timer sudo systemctl enable --now nftables sudo nft list table inet filter ``` **`nft -f` alone does not survive a reboot.** It writes to the running kernel only. Without `enable`, the next boot comes up with no ruleset at all — and because everything still works, nothing tells you. If port-forwards exist by then, the box is open. Do not skip this pair. **`systemctl is-active nftables` reports `inactive` even when the firewall is loaded, and that is correct.** The unit is `Type=oneshot` with `RemainAfterExit=no`: it runs `nft -f`, exits 0, and the rules stay in the kernel with no process behind them. Judge this unit by `nft list table inet filter`, never by `is-active`. `is-enabled` is what tells you it will come back after a reboot. If it does **not**, change nothing and wait out the timer — the table deletes itself and the box returns to open. Then fix `/etc/nftables.conf` and retry. Recovery without the timer, at the physical console: `sudo nft delete table inet filter`. Note that `systemctl stop nftables` does **not** remove rules already loaded into the kernel, and `nft flush ruleset` would take Docker's rules with it. **External reachability** (80, 443, 2222 open; anything else closed) can only be tested once the port-forwards exist. Those are [06-docker-deploy.md](06-docker-deploy.md) §1, and this ruleset is exactly what gates them — create them after the checklist at the bottom of this page passes, not before. ## 2. Verify nftables survives reboot This step is not a formality — it is the only thing that catches a ruleset that was loaded by hand but never enabled. ```bash sudo reboot # wait ~40s, then from the dev machine: ssh -p 2234 admin@192.168.1.50 'systemctl is-enabled nftables' sudo nft list table inet filter # rules must still be present ``` The same reboot doubles as the autostart test for [06](06-docker-deploy.md) §7. Confirm in one pass: ```bash systemctl is-active docker ddclient fail2ban cd ~/mithlond && docker compose ps # both containers Up ``` A useful negative signal during boot: port 2222 answering *Connection refused* rather than timing out means the host is up and the firewall is permissive but Docker has not started yet. Timeouts mean dropped packets; refusals mean nothing is listening. They point at completely different faults. ## Checklist - [ ] `/etc/nftables.conf` replaces only `table inet filter`, no `flush ruleset` - [ ] nftables rules active, SSH on 2234 proven from a *new* connection - [ ] `nft-panic` timer cancelled after that proof - [ ] nftables survives reboot - [ ] Docker still publishes 80/443/2222 after the ruleset loads ## Next step Continue with [09-fail2ban.md](09-fail2ban.md).