You can see iptables as a rule set. In iptables we talk about chains of rules. These chains of rules together build up your firewall. The chains are named:
- PREROUTING - Rules in this chain will be applied before the packet gets routed.
- INPUT - This packet will be locally delivered.
- FORWARD - This chain applies after a packet is routed wen not delivered locally.
- OUTPUT - Every connection going from this machine to other computers in the network or the Internet.
- POSTROUTING - These rules apply after the packet is routed.
This is what a rule might look like:
iptables -A FORWARD -d 192.168.0.120 -p tcp --dport 80 -j ACCEPT
In this case this rule belongs to the FORWARD chain. This rule forwards port 80 on this machine to IP address 192.168.0.120. We can use this rule to create a rule that will drop the connection to port 80.
iptables -A INPUT -p tcp --dport 80 -j DROP
This rule belongs to the chain INPUT. It will DROP all connections to port 80. If you want to see if this worked just run:
sudo iptables -L
This will show you a list per chain of current rules in use. Keep in mind that restriction is your best protection. Only open incoming ports if you have to. These rules are not preserved at boot. If you would reboot your computer now, all of the rules would be gone.