Iptables
IPTables is a built-in Linux firewall tool that controls how network traffic is handled by the kernel. It acts as an internal firewall, filtering packets based on defined rules and chains.
Listing and Understanding Rules
One can view the current firewall configuration with:
iptables -LBy default, most Linux systems have policies set to ACCEPT, meaning traffic is allowed unless a rule explicitly blocks it.
The Three Main Chains
IPTables rules are grouped into chains, each responsible for different traffic flows:
- INPUT β Incoming traffic to the local system.
- FORWARD β Traffic passing through the system (common in routers or gateways).
- OUTPUT β Outgoing traffic from the local system.
When a packet reaches a chain, it moves through the list of rules in order. If a rule matches, it can take an action (e.g. ACCEPT or DROP). If no rule matches, the chainβs default policy is applied.
Creating Rules
Rules can match by source, destination, protocol, and port.
Example: Allow incoming SSH from a specific IP:
iptables -A INPUT -p tcp -s 192.168.0.12 --dport 22 -j ACCEPTHere:
-Aβ Add rule to the bottom of the chain (-Iinserts at the top).-p tcpβ Match TCP protocol.-sβ Source address.--dport 22β Destination port 22 (SSH).-j ACCEPTβ Jump to the ACCEPT target (allow traffic).
Rule order matters. If a packet matches an earlier rule, later rules are skipped.
Deleting Rules
One can delete a rule by its position number:
iptables -D OUTPUT 5This removes rule number 5 from the OUTPUT chain.