Forgejo

Appearance

Make haypack comfortable for you.

Theme
Font
Contrast
Text size
Layout

Saved in this browser. No cookies.

Written guide · Networking · 15 min · Intermediate

Working with the firewall

Manage local nftables rules: allow ports, restrict networks, block traffic, test changes and roll them back.

1. Understand the baseline

This guide covers the firewall inside a haypack guest. Run Linux commands as root there. It does not configure router forwarding or the Proxmox firewall.

iac uses table inet iac and chain input for traffic addressed to the guest. The default policy is drop: unmatched incoming traffic is blocked. The baseline already permits established connections, SSH on port 22, loopback, ICMP and DHCP. App ports come from iac.json.

Actionnftables verdictResult
ACCEPT / allowacceptPermits traffic in this chain
BLOCK / silently discarddropDiscards traffic without a reply
REJECT / refuserejectDiscards traffic and sends an error reply

There is no BLOCKED command. Use drop or reject. An accept in one base chain does not override a drop in another chain.

2. Inspect before changing

nft list ruleset
nft -a list chain inet iac input
ss -lntup

Check existing allowances and service listeners. A firewall allowance cannot start a service. Keep your current management session open and have console access available before testing restrictions.

3. Create a local rule file

Local files 90–99 under /etc/nftables.d/ survive iac updates. Do not edit generated 50-*.nft files or the managed baseline.

ls -l /etc/nftables.d/90-local.nft
nano /etc/nftables.d/90-local.nft

Inspect an existing file before editing it. Choose only the examples you need below; they are alternatives, not a complete ruleset to paste together. Replace sample ports and documentation addresses with your deployment values.

4. Common rule recipes

Allow a TCP port

add rule inet iac input tcp dport 8443 counter accept comment "Allow application HTTPS"

Allows IPv4 and IPv6 from any source reaching the guest.

Allow several TCP ports

add rule inet iac input tcp dport { 8080, 8443 } counter accept comment "Allow application ports"

Allow UDP

add rule inet iac input udp dport 51820 counter accept comment "Allow VPN UDP"

TCP and UDP are separate protocols. Opening one does not open the other.

Allow only a source network

add rule inet iac input ip saddr 192.0.2.0/24 tcp dport 8443 counter accept comment "Allow IPv4 management"
add rule inet iac input ip6 saddr 2001:db8:1234::/64 tcp dport 8443 counter accept comment "Allow IPv6 management"

These are separate IPv4 and IPv6 examples. Use the address families you need. This restricts access only if no other broader rule already permits the port. Remove your own unrestricted allowance; adjust generated app access in its managed configuration.

Block a source on a specific port (DROP)

insert rule inet iac input ip saddr 198.51.100.23 tcp dport 8443 counter drop comment "Block source on application port"

insert rule puts the restriction at the beginning of the chain, before existing allowances, including established-connection acceptance. This example can therefore also interrupt an existing matching connection. It covers IPv4 only.

Refuse a TCP port (REJECT)

insert rule inet iac input tcp dport 8080 counter reject with tcp reset comment "Reject application port"

This takes precedence over later allowances in this chain and also covers existing matching connections. Use it only for a port you intend to refuse.

Count and log blocked traffic

insert rule inet iac input ip saddr 198.51.100.23 tcp dport 8443 counter drop comment "Drop selected source"
insert rule inet iac input ip saddr 198.51.100.23 tcp dport 8443 limit rate 5/second log prefix "iac-block: "

The second insert runs first: it logs at a limited rate, then evaluation reaches the unconditional drop. Traffic above the log limit is still dropped. Log entries are available in journalctl -k.

5. Understand rule order

add rule appends; insert rule prepends. A terminating verdict ends evaluation in that chain. A later drop cannot undo an earlier accept in the same chain. Several insertions appear in reverse file order.

An earlier NetBox restriction in its separate chain cannot be bypassed by adding a local accept. See firewall structure and precedence. Avoid a blanket drop at the start of input: it can cut off SSH and essential network services.

6. Validate, apply and test

nft -c -f /etc/nftables.conf && systemctl reload nftables
systemctl is-active nftables
nft -a list chain inet iac input

The syntax check changes nothing; reload runs only on success. Load the complete configuration so the iac table is rebuilt, rather than repeatedly applying a drop-in and duplicating rules. Syntax validation does not prove that access remains available.

Test from both an allowed and a blocked client using new connections, and inspect counters. For example, on Windows:

Test-NetConnection server.example.org -Port 8443

For UDP use the actual application client. For packet inspection in the guest:

tcpdump -ni any 'tcp port 8443'
journalctl -k --since "5 minutes ago"

A timeout can indicate filtering; a refusal can also mean no service is listening. Check the whole connection path.

7. Change or remove a rule

Edit the relevant line in your local file, validate and reload again. To disable this entire example file:

mv -i /etc/nftables.d/90-local.nft /etc/nftables.d/90-local.nft.disabled
nft -c -f /etc/nftables.conf && systemctl reload nftables

This disables every rule in that file. Restore the .nft extension to enable it again. Test with a new connection: the baseline may continue permitting established sessions after an allowance is removed.

Further reading