iptables Rule Generator

Generate iptables rules from CSV

Rule Definitions

Format

source, port, protocol, action

Use * for any. Protocols: tcp, udp, icmp, all

iptables Commands

Enter rules to generate iptables commands

The iptables Rule Generator creates Linux firewall rules from structured CSV input. Enter your rules as rows with chain, source, destination, protocol, port, and action columns, and get correctly formatted iptables commands with proper flag ordering -- no need to memorize the verbose and error-prone iptables syntax.

Understanding iptables

If you administer Linux servers, iptables is likely the first firewall tool you encounter. It has been the standard Linux packet filtering interface since the 2.4 kernel (released in 2001), and despite being succeeded by nftables on newer distributions, iptables remains the most widely deployed Linux firewall in production. Most documentation, tutorials, and automation tools still target iptables syntax, and the iptables-nft compatibility layer ensures that iptables commands work even on systems that use the nftables backend.

The challenge with iptables is its command-line syntax. Every rule is a single command with flags that must appear in a specific order, and getting one flag wrong can silently create a rule that does not match what you intended. A missing -p tcp before --dport 443 produces an error. A -j ACCEPT when you meant -j DROP creates a security hole. And when you are building a complete firewall configuration with 30 or 40 rules, hand-writing every command is tedious and error-prone.

This generator solves the problem by accepting your rules in a simple CSV format and producing correct iptables commands. You can design your firewall in a spreadsheet, paste the CSV, and get a complete script that you can review, version-control, and deploy. The generator handles flag ordering, multiport syntax, connection tracking, interface specification, and automatic IPv4/IPv6 detection -- all the details that trip up even experienced administrators when writing rules by hand.

How to Generate iptables Rules from CSV

  1. Prepare your rule data in CSV format with columns: chain, source, destination, protocol, port(s), and action.
  2. Paste or upload the CSV into the generator.
  3. The generator parses each row and produces a complete iptables command with correct flags.
  4. Review the output commands -- IPv4 rules use iptables and IPv6 rules use ip6tables automatically.
  5. Copy the commands for direct execution or export as a shell script.

CSV Input Format and Generated Commands

CSV Input

chain,source,destination,protocol,port,action
INPUT,203.0.113.0/24,,tcp,22,DROP
INPUT,10.0.0.0/8,,tcp,443,ACCEPT
OUTPUT,,198.51.100.10,tcp,3306,ACCEPT
INPUT,,,tcp,80,ACCEPT
FORWARD,192.168.1.0/24,10.0.0.0/8,tcp,8080,ACCEPT
INPUT,,,icmp,,DROP

Generated Output

iptables -A INPUT -s 203.0.113.0/24 -p tcp --dport 22 -j DROP
iptables -A INPUT -s 10.0.0.0/8 -p tcp --dport 443 -j ACCEPT
iptables -A OUTPUT -d 198.51.100.10 -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A FORWARD -s 192.168.1.0/24 -d 10.0.0.0/8 -p tcp --dport 8080 -j ACCEPT
iptables -A INPUT -p icmp -j DROP

Advanced Features

CSV Fieldiptables FlagExample
Port range--dport8000:8080 generates --dport 8000:8080
Multiple ports-m multiport --dports80,443,8080 generates -m multiport --dports 80,443,8080
Source port--sportSeparate column for source port specification
Connection tracking-m state --stateESTABLISHED,RELATED for stateful rules
Interface-i / -oeth0 generates -i eth0 for INPUT chain
Logging-j LOG --log-prefix"SSH-BLOCK: " generates -j LOG --log-prefix "SSH-BLOCK: "

When to Use the iptables Rule Generator

  • Server hardening -- Build a complete firewall ruleset from a security requirements spreadsheet during initial server setup.
  • Incident response -- Quickly generate block rules for attacker IPs collected during an active incident.
  • Platform migration -- Convert firewall rules from Windows Firewall, AWS security groups, or other platforms to iptables syntax.
  • Automation scripts -- Generate firewall setup scripts for Ansible, Puppet, or shell-based provisioning workflows.
  • Learning iptables -- Study the relationship between CSV specifications and generated commands to understand iptables syntax.

How iptables Rule Syntax Works

Command Structure

Every iptables command follows this pattern:

iptables -A <CHAIN> [-s source] [-d dest] -p <protocol> [--dport port] -j <ACTION>
  • -A appends the rule to the specified chain
  • -s and -d specify source and destination addresses in CIDR notation
  • -p sets the protocol (tcp, udp, icmp)
  • --dport sets the destination port (requires -p tcp or -p udp)
  • -j sets the target action

Chain Types

ChainDirectionUse Case
INPUTInbound to this hostProtecting services running on this server
OUTPUTOutbound from this hostControlling what this server can connect to
FORWARDThrough this hostRouting/NAT scenarios, firewall appliances

IPv4 vs. IPv6

When the generator detects an IPv6 address (e.g., 2001:db8::/32), it produces ip6tables commands instead of iptables:

ip6tables -A INPUT -s 2001:db8::/32 -p tcp --dport 443 -j ACCEPT

Practical Advice for iptables Configuration

The most critical thing to understand about iptables is rule ordering. Rules in a chain are evaluated top to bottom, and the first matching rule wins. If you put a broad ACCEPT rule before a specific DROP rule, the DROP never fires. This is especially important for the INPUT chain: always put your specific allow rules first, then your specific deny rules, and finish with a default deny (iptables -A INPUT -j DROP). The generator preserves the order of your CSV rows, so arrange your spreadsheet accordingly.

The second most common mistake is not saving rules for persistence. By default, iptables rules live in kernel memory and disappear on reboot. On Debian/Ubuntu, use iptables-save > /etc/iptables/rules.v4 and install the iptables-persistent package. On RHEL/CentOS, use service iptables save. If you skip this step, your server comes back from a reboot with no firewall -- a critical vulnerability window that has caused real security incidents.

When building a complete firewall configuration, always include stateful rules at the top of your INPUT and OUTPUT chains. A rule like iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT allows return traffic for connections your server initiated, without which outbound connections (package updates, DNS queries, API calls) would break. Without this rule, you would need explicit rules for every return path, which is impractical. Place this rule first in the INPUT chain for best performance, since the majority of packets on an active server are part of established connections.

Frequently Asked Questions

What is the difference between DROP and REJECT? DROP silently discards the packet with no response -- the sender's connection times out. REJECT sends back an ICMP "port unreachable" or TCP RST, letting the sender know immediately. DROP is more common for external-facing rules; REJECT is more helpful on internal networks where quick failure is preferred.

How do I apply the generated rules? Copy the commands and run them directly in a root shell, or save as a script and execute it. Rules take effect immediately but are lost on reboot. Use iptables-save > /etc/iptables/rules.v4 to persist rules across reboots on Debian/Ubuntu, or service iptables save on RHEL/CentOS.

Can the generator create rules for custom chains? Yes. Enter a custom chain name in the chain column (e.g., LOGGING or SSH-RULES). The generator produces the -A CUSTOMCHAIN command, but you must separately create the chain with iptables -N CUSTOMCHAIN before applying the rules.

Should I use iptables or nftables for new deployments? For new deployments on modern distributions (Debian 10+, RHEL 8+, Ubuntu 20.04+), nftables is the recommended choice. It offers cleaner syntax, better performance, and atomic ruleset loading. Use the nftables Rule Generator for those systems. For existing systems that already run iptables, or for compatibility with legacy automation, iptables remains a solid choice.

How do I flush all existing rules before applying new ones? Use iptables -F to flush all rules in all chains, and iptables -X to delete custom chains. Be careful: if you flush rules on a remote server without first setting the default policy to ACCEPT, you will lock yourself out. Always run iptables -P INPUT ACCEPT before flushing.

Can I generate rules with comments? Yes. iptables supports the -m comment --comment "description" syntax to add human-readable comments to rules. If your CSV includes a description column, the generator can embed it as a comment in each rule.

Related Tools

IPparse•Free Network & Subnet Calculator Tools

© 2026 IPparse. All tools run locally in your browser.

100% Privacy-First • No Data Collected