Skip to content

Blocking outgoing traffic for a separate service via nftables

When a single service must be cut off from outbound network traffic without affecting the host or other workloads, nftables provides a precise, kernel‑level mechanism. Below is a minimal, production‑ready recipe to block outbound on port 5432 while preserving DNS and update functionality.

Why Block Outgoing Traffic

In hardened or multi‑tenant environments, limiting a service’s egress surface reduces the impact of compromise. If PostgreSQL (port 5432) should only accept connections and never initiate them, blocking outbound traffic from that process is a defense‑in‑depth step. nftables lets you target rules by port, user, or process ID, making it more precise than broad iptables chains and avoiding the legacy conflicts that appear when mixing frameworks.

Preparation: Checking Current nft Rules

Before adding a blocking rule, verify the existing ruleset to avoid duplicates or lockouts.

nft list ruleset

Check the inet filter table and output chain. Note the current priority and any existing rules. If the table does not exist, create it first:

nft add table inet filter
nft add chain inet filter output { type filter hook output priority 0 ; }

Blocking Port 5432

The core rule blocks any outbound packet leaving the host on port 5432. Apply it in the output chain:

nft add rule inet filter output from port 5432 drop

This rule matches any process sending traffic to port 5432 from the outbound direction. Because it sits in the output chain, it only affects locally generated packets, not forwarded traffic.

Warning

Blocking port 5432 drop will also affect locally initiated connections from other services. Always pair it with explicit allow rules for required subsystems (DNS, package managers, container runtimes).

Adding Allow Rules for DNS and Updates

A blocking rule without corresponding allows breaks name resolution and package updates. Add these after the drop rule:

nft add rule inet filter output ip saddr 127.0.0.1/32 iif lo accept
nft add rule inet filter output from port 53 accept
nft add rule inet filter output from port 80 accept
nft add rule inet filter output from port 443 accept

The first line permits loopback traffic, which is essential for internal DNS resolvers and many update mechanisms. The next three lines allow unencrypted and encrypted HTTP traffic, typically needed for apt, yum, or package registry access.

Tip

If the service runs under a dedicated user or group, narrow the rules further:

nft add rule inet filter output from user postgres ip saddr 10.0.0.0/8 accept
nft add rule inet filter output from port 5432 drop

Adjust postgres and the subnet to match your environment.

Note

DNS often uses random high ports above 32768. If from port 53 isn’t sufficient, capture the actual resolver port with from sport > 32768 accept or configure the service to use a static outbound port range.

Verification via nft list ruleset

After adding rules, confirm the configuration:

nft list ruleset

Output should show the new drop rule followed by the accept rules, in the correct order (order matters in nftables chains, first‑match wins). Test the service:

  • Attempt a database connection from another host; it should fail or timeout.
  • From the protected host, run nslookup example.com or apt update; both must succeed.
  • Use nft monitor output for real‑time rule‑hit verification.

If traffic behaves unexpectedly, reorder rules: nftables processes first‑match, so the accept rules for DNS/updates must precede the drop on 5432 if you want those exceptions to work, or restructure the chain priority.