Iptables Rule Generator
Create iptables/ip6tables rules with ports, CIDR, state and logging options, then copy the generated command and restore snippet.
Iptables Rule Generator
Build copy-ready iptables/ip6tables rules with safe defaults.
About Iptables Rule Generator
Iptables Rule Generator for Linux Firewall Rules
Iptables is a powerful packet filtering system used on many Linux servers, routers, and container hosts. It can feel intimidating because the syntax is compact and order-sensitive, and small mistakes can lead to rules that never match—or rules that match too broadly. This Iptables Rule Generator helps you build correct, copy‑ready commands for common firewall tasks without memorizing every flag, even if you rarely touch iptables.
Use the sidebar to choose your table, chain, protocol, addresses, ports, connection states, and target. The tool then outputs a ready-to-run iptables (IPv4) or ip6tables (IPv6) command, plus an optional iptables-restore snippet you can paste into configuration files and automation workflows.
How Iptables Rule Generator Works
The generator turns your form selections into a single firewall rule by following the same mental model experienced administrators use: identify where the packet is evaluated (table + chain), define what the packet looks like (match conditions), and decide what happens next (jump target). The output is intentionally “plain iptables” so you can audit it easily, teach from it, and adapt it for scripts.
To keep the command readable, match options are emitted in a practical order: interfaces and IP ranges first, then protocol and port details, then connection tracking state, then optional extras like comments or logging controls. This ordering is not strictly required by iptables, but it makes rules easier to compare and maintain when your ruleset grows.
Step-by-Step
- 1) Pick scope: choose IPv4 (iptables), IPv6 (ip6tables), or generate both versions for dual-stack servers.
- 2) Select table and chain: start with the standard filter table for allow/deny rules, or choose nat for DNAT/SNAT, mangle for advanced packet tweaks, raw for connection tracking exceptions, and security where applicable.
- 3) Choose how to add the rule: append with -A to place the rule at the end of the chain, or insert with -I (optionally at a specific position) when rule order matters.
- 4) Add match conditions: define protocol, source/destination CIDRs, interfaces, ports, and connection states (NEW, ESTABLISHED, RELATED, INVALID) using conntrack.
- 5) Choose a target: output a rule that jumps to ACCEPT, DROP, REJECT, or LOG. When LOG is selected, you can add a prefix, level, and optional rate limit.
- 6) Copy and apply: copy the command for a one-off change, or copy the restore block for version-controlled firewall files and repeatable deployments.
If you are working on a remote server, apply rules with care. A common safe approach is to keep an active SSH session open, test the new rule in a second session, and have a rollback plan (for example, a scheduled task that reverts rules if you lose connectivity). The generator makes rule creation faster, but operational safety still matters.
Key Features
Dual-stack output (iptables + ip6tables)
Many production hosts run both IPv4 and IPv6. Generating only IPv4 rules can leave services exposed on IPv6 without you noticing, especially when your application binds to ::. Generate one rule for IPv4, one for IPv6, or both at the same time so your policy stays consistent across stacks.
For IPv6, the tool automatically uses the appropriate ICMP protocol form and helps you keep common patterns aligned between iptables and ip6tables, which is useful when documenting firewall baselines.
Table and chain selection that matches real workflows
Filtering decisions typically live in the filter table (INPUT/OUTPUT/FORWARD). NAT decisions typically live in the nat table (PREROUTING/POSTROUTING/OUTPUT). The generator keeps the most common chains close at hand, making it easier to place a rule where it actually takes effect.
This matters because an otherwise “correct” match can silently do nothing if it is attached to the wrong chain. Seeing the table and chain explicitly in the generated output is a quick sanity check.
Protocol-aware port handling
TCP and UDP ports only make sense when the protocol is TCP or UDP. The generator structures the command so port matches sit alongside the protocol match, including the proper module match where needed, which prevents subtle “rule never matches” mistakes.
You can enter a single port (for example 22) or a simple range (for example 1024:65535) when you need to cover ephemeral port ranges for client traffic.
Conntrack state matching with predictable ordering
Stateful filtering is the norm: allow established connections while tightly controlling new sessions. Select common states and the tool builds a correct -m conntrack --ctstate segment in a predictable order (NEW, ESTABLISHED, RELATED, INVALID).
This helps you craft policies like “allow ESTABLISHED/RELATED, then allow NEW to SSH, then drop the rest,” which is easier to reason about than purely stateless rules.
Logging controls designed for production
LOG rules are invaluable during troubleshooting, but unbounded logging can flood disks and obscure real signals. Add a limit (for example 6/min) and a burst to keep logs useful and manageable, and add a readable prefix so you can filter messages quickly in journald or syslog.
Because the output is explicit, you can also split rules into “LOG then DROP” patterns by generating two adjacent rules and inserting them at the right position in your chain.
Use Cases
- Allow SSH from a trusted network: generate a TCP 22 rule scoped to your office CIDR and inbound interface, with a clear comment for future audits.
- Permit a web service: open TCP 80/443 for a public site, or restrict it to a reverse proxy and keep the application server private.
- Block a noisy source range: drop traffic from an abusive subnet while leaving other sources untouched, reducing resource usage from scans.
- Log and investigate unexpected packets: create a LOG rule with a readable prefix and optional rate limiting to identify misrouted traffic or misconfigured clients.
- Harden a dual-stack host: generate matching IPv4 and IPv6 rules so services are not accidentally exposed over IPv6 when you intended an IPv4-only opening.
- Create reproducible rules for automation: generate an iptables-restore block suitable for configuration management tools and deployment pipelines.
- Write documentation and runbooks: produce a clean, canonical command that you can paste into internal docs, change requests, or incident response playbooks.
Because the output is plain text commands, you can paste it into a terminal, a runbook, a CI job, or a firewall configuration file. It is also useful for learning: by changing one option at a time, you can see exactly how iptables expresses intent—what chain, what match modules, and what target.
If your environment is migrating toward nftables, this tool still has value: iptables concepts (chains, matches, stateful filtering) map closely to nftables rulesets. Using a generator to get the fundamentals right reduces the time spent debugging low-level syntax.
Optimization Tips
Start with a default-deny baseline and open only what you need
For servers, a common pattern is to set default policies to DROP (or a restrictive baseline) and then explicitly allow required inbound services. Keep the rule set minimal, review it whenever you add a new listener, and remove old openings so “temporary” rules do not become permanent attack surface.
Place broad, high-hit rules early and specific rules later
Rules are evaluated in order. Putting an ESTABLISHED/RELATED allow rule near the top reduces processing for return traffic and makes chains faster under load. After that, add specific NEW rules for each inbound service you want to expose, and finish with a clear drop/reject or policy decision.
Use comments, consistent labels, and safe testing habits
When every rule has a comment or a label, you can audit changes quickly with iptables -S and understand why a rule exists months later. On remote machines, always test changes in a way that prevents lockouts: keep an existing session open, consider using a timed rollback, and validate with real traffic rather than assumptions.
FAQ
Why Choose This Tool?
Writing iptables rules from memory is error-prone: a missing module match, an incorrect chain, or a misordered option can turn a good idea into a rule that never triggers. Iptables Rule Generator reduces that friction by presenting the important knobs in one place and emitting a command you can copy immediately. Because the output is explicit and compact, it is easy to review in code reviews, tickets, and peer approvals.
Whether you are documenting a change, teaching a teammate, or building a hardened baseline for a fleet, the generator keeps your intent visible. Start with a single focused rule, verify it with real traffic, and then repeat the pattern until you have a complete policy. When you are ready to operationalize, copy the restore snippet into version control and evolve it alongside your infrastructure.