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.
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:
Blocking Port 5432
The core rule blocks any outbound packet leaving the host on port 5432. Apply it in the output chain:
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.
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:
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.
If the service runs under a dedicated user or group, narrow the rules further:
Adjust postgres and the subnet to match your environment.
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:
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.comorapt update; both must succeed. - Use
nft monitor outputfor 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.