iptables options
iptables provides a wide range of options to configure firewall rules and control network traffic on a Linux system. Here is an overview of some common options used with

iptables options:
General Syntax:
iptables [options] [table] [command] [chain] [match] [target/jump]
Options:
- -A, –append:
- Appends a rule to the end of the specified chain.
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
- -I, –insert:
- Inserts a rule at the specified position in the chain.
iptables -I INPUT 2 -p tcp --dport 80 -j ACCEPT
- -D, –delete:
- Deletes a rule from the specified chain based on its rule number.
iptables -D INPUT 3
- -L, –list:
- Lists the rules in the specified chain.
iptables -L INPUT
- -F, –flush:
- Flushes all rules from the specified chain.
iptables -F INPUT
- -P, –policy:
- Sets the default policy for a chain (ACCEPT, DROP, or REJECT).
iptables -P INPUT DROP
- -N, –new-chain:
- Creates a new user-defined chain.
iptables -N MYCHAIN
- -X, –delete-chain:
- Deletes the specified user-defined chain.
iptables -X MYCHAIN
- -E, –rename-chain:
- Renames a user-defined chain.
iptables -E OLDCHAIN NEWCHAIN
- -Z, –zero:
- Zeroes the packet and byte counters in all chains.
iptables -Z
- -S, –list-rules:
- Lists all rules in a human-readable format.
iptables -S
- -v, –verbose:
- Provides detailed output, including packet and byte counters.
iptables -L -v
- -h, –help:
- Displays help information and usage instructions.
iptables --help
Match Extensions and Targets:
- -m, –match:
- Specifies a match extension to use with a rule.
- Example: iptables -A INPUT -p tcp –dport 80 -m tcp –tcp-flags SYN,ACK,FIN,RST ACK -j ACCEPT
- -j, –jump:
- Specifies the target to jump to if a packet matches the rule.
- Example: iptables -A INPUT -p tcp –dport 22 -j ACCEPT
These are just a subset of the available options. The full list of options, matches, and targets can be found in the iptables man page (man iptables). It’s important to carefully plan and test firewall rules, as incorrect configurations can lead to unintended consequences.
iptables options in more detail
Rule Specification iptables options:
- -p, –protocol [protocol]:
- Specifies the protocol of the rule, such as TCP, UDP, ICMP, etc.
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
- -s, –source [address]:
- Specifies the source address or network.
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT
- -d, –destination [address]:
- Specifies the destination address or network.
iptables -A OUTPUT -d 8.8.8.8 -j ACCEPT
Port and Service iptables options:
- –sport, –source-port [port]:
- Specifies the source port for the rule.
iptables -A INPUT --sport 1024:65535 -j ACCEPT
- –dport, –destination-port [port]:
- Specifies the destination port for the rule.
iptables -A INPUT --dport 22 -j ACCEPT
Connection State iptables options:
- -m, –match [match]:
- Specifies a match extension module, such as state for connection tracking.
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
Target iptables options:
- -j, –jump [target]:
- Specifies the target to jump to if the packet matches the rule.
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
- -g, –goto [chain]:
- Causes packets to jump to the specified chain and continue processing in that chain.
iptables -A INPUT -p tcp --dport 80 -g LOG_DROP
Logging iptables options:
- –log-prefix [prefix]:
- Prefixes log messages with the specified string.
iptables -A INPUT -p tcp --dport 22 -j LOG --log-prefix "SSH Attempt: "
- –log-level [level]:
- Sets the logging level (numeric or symbolic).
iptables -A INPUT -p tcp --dport 22 -j LOG --log-level 4
Miscellaneous iptables options:
- -i, –in-interface [interface]:
- Specifies the input interface.
iptables -A INPUT -i eth0 -j ACCEPT
- -o, –out-interface [interface]:
- Specifies the output interface.
iptables -A OUTPUT -o eth0 -j ACCEPT
- -c, –set-counters [packets] [bytes]:
- Initializes packet and byte counters.
iptables -A INPUT -p tcp --dport 80 -c 0 0 -j ACCEPT
These are just a selection of the many options available with iptables. The tool provides extensive flexibility, allowing administrators to craft precise firewall rules to suit their network security requirements. For more comprehensive information and additional options, refer to the iptables manual (man iptables).
Useful Links
https://sanchitgurukul.com/basic-networking
https://sanchitgurukul.com/network-security
