Iptables Rule Generator

IPTables Rule Generator

Generated Rule:

iptables is used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel. These rules define how incoming and outgoing network packets are handled by the system.

Syntax of an iptables Rule

Understanding the syntax of an iptables rule is crucial for effectively managing network traffic. An iptables rule consists of several components

ComponentDescriptionExample
ActionSpecifies the action to take on a chain-A (append), -I (insert), -D (delete)
ChainThe chain to which the rule is addedINPUT, OUTPUT, FORWARD
ProtocolSpecifies the protocol to match-p tcp, -p udp, -p icmp
SourceMatches packets from a specific source IP address-s 192.168.1.100
DestinationMatches packets to a specific destination IP address-d 192.168.1.200
Source PortMatches packets from a specific source port–sport 80
Destination PortMatches packets to a specific destination port–dport 22
MatchSpecifies additional matching criteria-m state –state NEW, -m tcp –dport 80
TargetSpecifies the action to take on matching packets-j ACCEPT, -j DROP, -j REJECT
InterfaceMatches packets coming in or going out through a specific interface-i eth0 (incoming), -o eth0 (outgoing)

Example of an iptables Rule

Suppose we want to allow incoming SSH connections (which use TCP port 22) from a specific IP address (192.168.1.100) and drop all other incoming SSH connections. Here are the rules to achieve this:

Allow SSH from a specific IP address:

    iptables -A INPUT -p tcp -s 192.168.1.100 --dport 22 -j ACCEPT

    Drop all other incoming SSH connections:

      iptables -A INPUT -p tcp --dport 22 -j DROP

      These rules ensure that only SSH connections from 192.168.1.100 are allowed, while all other SSH connection attempts are dropped.