iptables Rule Generator
Build bounded iptables or ip6tables commands for a packet path with explicit CIDR and port matches, persistence choices, and production-safety checks.{{ summaryTitle }}
{{ summaryLine }}
{{ values.command_text }}
The chart renderer is unavailable. The same review states remain available in Rule audit.
| Signal | Value | Review note | Copy |
|---|---|---|---|
| {{ row.label }} | {{ row.value }} | {{ row.note }} |
| Step | Generated command or note | Use when | Review note | Copy |
|---|---|---|---|---|
| {{ row.step }} | {{ row.output }} | {{ row.use }} | {{ row.note }} |
A firewall rule is both a packet matcher and a decision placed at a specific point in an ordered chain. The text can be valid while the policy is wrong: an allow rule below an earlier drop may never see traffic, a broad accept can expose a service, and a remote-host mistake can cut off the same administrative session needed for recovery.
The default filter table has three built-in paths. INPUT handles packets addressed to the local host, OUTPUT handles packets created by the host, and FORWARD handles packets routed through it. The correct chain depends on the packet path, not simply on whether the rule feels inbound or outbound from the operator's perspective.
| Packet path | Typical chain | Question to answer first |
|---|---|---|
| Client reaches a service on this host | INPUT |
Which source, interface, protocol, port, and connection state should reach the listener? |
| This host contacts another service | OUTPUT |
Which destination and service should locally created packets reach? |
| This host routes traffic between networks | FORWARD |
Which incoming and outgoing interfaces, addresses, and states define the permitted route? |
Rules are evaluated in chain order. When a packet does not match, evaluation continues to the next rule. Terminal targets such as ACCEPT and DROP decide the packet's fate immediately; RETURN leaves the current chain according to the calling context. LOG records a match but does not stop traversal, so a later rule or the chain policy still decides the packet.
IPv4 and IPv6 use separate command families. A service reachable over both protocols needs equivalent policy on both sides, and each side needs address-family-correct source and destination matches. A dual-stack shortcut can generate two commands only when the address scope is unrestricted; family-specific CIDRs require separate rules so an IPv4 range is never mistaken for an IPv6 range.
The command name does not always reveal the kernel policy owner. On many current systems, iptables may use an nftables-compatible backend; other hosts retain legacy xtables or delegate policy to firewalld, nftables, a distribution service, or an orchestration system. Mixing direct rules with a manager that later reloads its own policy can erase or contradict a valid runtime change.
Runtime changes and persistent policy are separate decisions. An append, insert, or delete changes the active ruleset immediately when executed, while a save or manager handoff controls what returns after reboot or reload. Persist only a rule that has passed remote testing, chain-order review, and a rollback rehearsal.
Firewall generation cannot determine business authorization, service ownership, routing, interface topology, existing chain order, or the safety of a maintenance window. Keep a second administrative session, verify the exact target host, and use host-native policy tooling whenever it owns the ruleset.
How to Use This Tool:
Draft one bounded filter-table rule, then compare the command with the live chain, backend ownership, and persistence plan before running anything.
- Choose a Rule preset as a starting point, then set Address family, Backend profile, distribution, persistence target, and review strictness for the actual host.
- Select the rule operation, built-in chain, target action, and protocol. Use Insert with a verified position when an earlier broad rule would otherwise shadow the new rule.
- Narrow the match with source and destination addresses, port direction and ports, interfaces, ICMP type, and connection state. Use
anyonly when broad scope is intentional. - For TCP or UDP, enter single ports, comma-separated ports, or ranges. Keep every value within 1–65,535 and remember that the multiport matcher allows at most 15 entries, with each range counting as two.
- Add a concise rule comment and choose whether the command needs
sudo. ForREJECT, confirm that the selected reject response matches the protocol and address family. - Stop if production strictness blocks generation. Narrow broad accepts, add service or connection-state scope, choose a durable ownership path, add an audit comment, and pin the host backend as required by the blocking message.
- Run the generated duplicate check first, compare the apply command with the numbered live chain, keep a rollback path, and execute only in an approved change window. Test from the expected network path before saving the rules.
Interpreting Results:
Ready means the local rule model found no remaining review items. Review needed means a command exists but host-specific or policy-sensitive findings remain. A blocked production review produces an input error instead of a command, because the unresolved finding is treated as a deployment stop.
| Output | Meaning | Verification |
|---|---|---|
| Rule command | The selected append, insert, check, or delete operation for one or both address families. | Confirm binary, chain, operation, position, match clauses, comment, and target before execution. |
| Duplicate check | The same match rendered with -C, which tests for an existing rule without changing the chain. |
Use its exit status; a missing rule is expected before a new append or insert. |
| Readiness checks | Local findings for source scope, service scope, interfaces, state tracking, persistence, comment, backend, and address family. | Resolve each review against the live host rather than treating the status count as a security score. |
| Save plan | Duplicate check, apply command, chain listing, and a persistence or manager handoff suggestion. | Test runtime behavior first; confirm package, service, file paths, privileges, and firewall-manager ownership before saving. |
Technical Details:
An iptables rule is assembled from an operation, chain, zero or more match clauses, and a target. Match extensions are evaluated in command order and short-circuit when a condition fails. If every match succeeds, the target decides whether traversal stops, continues, or returns to a calling chain.
The generated command uses the default filter table and only the built-in INPUT, OUTPUT, and FORWARD chains. It does not model NAT, packet marking, raw-table behavior, custom chains, rate limiting, sets, owner matches, or every extension available on a particular kernel and userspace release.
Rule Core:
| Stage | Generated form | Exact boundary |
|---|---|---|
| Binary and operation | iptables or ip6tables, optionally suffixed with -nft or -legacy, followed by -A, -I, -C, or -D. |
Insert position is allowed only from 1 through 999. |
| Packet path | Adds the selected built-in chain, protocol, input/output interfaces, source, and destination. | An output interface is invalid in INPUT; an input interface is invalid in OUTPUT. |
| Port match | Uses --sport or --dport for one entry and the multiport extension for several entries. |
Ports are 1–65,535; ranges are inclusive and count as two of the 15 multiport slots. |
| Protocol detail | Maps ICMP to ipv6-icmp for IPv6 and adds an ICMP type or conntrack-state match when selected. |
Port clauses are emitted only for TCP and UDP. |
| Audit and decision | Adds an optional comment, the jump target, and an optional reject response. | Comments are limited to 256 characters; tcp-reset requires TCP, and ICMP reject forms must match the selected family. |
Validation and Review Core:
Blocking validation protects command syntax and family consistency. Review rules call out operational risk that can still be intentional. Production strictness promotes selected review findings to blockers before any command is returned.
| Condition | Standard review | Production strictness |
|---|---|---|
ACCEPT from any source |
Marks source scope for review. | Blocks generation. |
| TCP or UDP with no port | Warns that the protocol match remains broad. | Blocks generation. |
ACCEPT with any conntrack state |
Marks state scope for review. | Blocks generation. |
| Runtime-only or manager handoff persistence | Requires an ownership decision. | Blocks generation until a production-ready path is selected. |
| Blank audit comment | Requests a concise identifier on shared hosts. | Blocks generation. |
| System-default backend | Requires checking whether the host uses nft-compatible or legacy binaries. | Blocks generation until the backend is pinned. |
Dual-stack mode emits one IPv4 and one IPv6 command but accepts only unrestricted source and destination scope. This constraint prevents one family's CIDR from being copied into the other command. Use separate family-specific drafts when either side needs a narrow address range.
Persistence output is advisory. Commands that redirect iptables-save into a root-owned path may require a root shell or a privilege-aware write method because shell redirection is evaluated separately from sudo. Distribution packages and active managers determine the real save, test, reload, and boot-restore workflow.
Limitations and Responsible Use:
The commands are specimens for review, not authorization to change a firewall. They do not read the live ruleset, select a safe insertion point, test reachability, inspect counters, identify the active policy manager, or guarantee recovery after a remote lockout.
- Keep an out-of-band or second administrative path before changing remote access rules. Confirm the rollback command before the apply command.
- List the target chain with line numbers and counters, check for an existing equivalent rule, and place the rule before any earlier terminal decision that would shadow it.
- Test from another host on the intended network path. A local socket check does not prove that packets cross the expected interface and chain.
- Save only after runtime verification. A persistent bad rule can make the same outage return at boot.
Worked Examples:
Allow HTTPS from one office network
An IPv4 INPUT rule accepts new and established TCP connections to destination port 443 from 203.0.113.0/24. The generated duplicate check uses the same match with -C, while the apply command appends or inserts it according to the selected operation. Before running it, confirm that the example CIDR has been replaced, the HTTPS service is listening, the position precedes a broad drop, and a remote test increments the expected rule counter.
References:
- iptables and ip6tables manual page, Linux man-pages project.
- iptables-extensions manual page, Linux man-pages project.
- How to allow a port with iptables, Simplified Guide.
- How to save iptables rules permanently, Simplified Guide.