25 Most Frequently Used Linux IPTables Rules Examples
25 Most Frequently Used Linux IPTables Rules Examples
At a first glance, IPTables rules might look cryptic. In this article, Ive given 25 practical IPTables rules that you can copy/paste and use it for your needs. These examples will act as a basic templates for you to tweak these rules to suite your specific requirement. For easy reference, all these 25 iptables rules are in shell script format: iptables-rules
When you make both INPUT, and OUTPUT chains default policy as DROP, for every firewall rule requirement you have, you should define two rules. i.e one for incoming and one for outgoing. In all our examples below, we have two rules for each scenario, as weve set DROP as default policy for both INPUT and OUTPUT chain. If you trust your internal users, you can omit the last line above. i.e Do not DROP all outgoing packets by default. In that case, for every firewall rule requirement you have, you just have to define only one rule. i.e define rule only for incoming, as the outgoing is ACCEPT for all packets.
Note: If you dont know what a chain means, you should first familiarize yourself with the IPTables fundamentals.
This is helpful when you find some strange activities from a specific ip-address in your log files, and you want to temporarily block that ip-address while you do further research. You can also use one of the following variations, which blocks only TCP traffic on eth0 connection for this ip-address.
iptables -A INPUT -i eth0 -s "$BLOCK_THIS_IP" -j DROP iptables -A INPUT -i eth0 -p tcp -s "$BLOCK_THIS_IP" -j DROP
Note: If you like to understand exactly what each and every one of the arguments means, you should read How to Add IPTables Firewall Rules
In the above example, instead of /24, you can also use the full subnet mask. i.e 192.168.100.0/255.255.255.0.
iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT iptables -A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
The following rules allow all incoming secure web traffic. i.e HTTPS traffic to port 443.
iptables -A INPUT -i eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT iptables -A OUTPUT -o eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
Please note that this is slightly different than the incoming rule. i.e We allow both the NEW and ESTABLISHED state on the OUTPUT chain, and only ESTABLISHED state on the INPUT chain. For the incoming rule, it is vice versa.
Note: For outgoing HTTP web traffic, add two additional rules like the above, and change 443 to 80.
Now allow incoming connection to the port 111, and the ports that were used by ypbind.
iptables iptables iptables iptables iptables iptables -A -A -A -A -A -A INPUT INPUT INPUT INPUT INPUT INPUT -p -p -p -p -p -p tcp udp tcp udp tcp udp --dport --dport --dport --dport --dport --dport 111 111 853 853 850 850 -j -j -j -j -j -j ACCEPT ACCEPT ACCEPT ACCEPT ACCEPT ACCEPT
The above will not work when you restart the ypbind, as it will have different port numbers that time. There are two solutions to this: 1) Use static ip-address for your NIS, or 2) Use some clever shell scripting techniques to automatically grab the dynamic port number from the rpcinfo -p command output, and use those in the above iptables rules.
-m limit: This uses the limit iptables extension limit 25/minute: This limits only maximum of 25 connection per minute. Change this value based on your specific requirement limit-burst 100: This value indicates that the limit/minute will be enforced only after the total number of connection have reached the limit-burst level.
If you do the above, you also need to explicitly allow incoming connection on the port 422.
iptables -A INPUT -i eth0 -p tcp --dport 422 -m state --state NEW,ESTABLISHED -j ACCEPT iptables -A OUTPUT -o eth0 -p tcp --sport 422 -m state --state ESTABLISHED -j ACCEPT
Next, make sure all the remaining incoming connections jump to the LOGGING chain as shown below.
iptables -A INPUT -j LOGGING
All of the above 25 iptables rules are in shell script format: iptables-rules Previous articles in the iptables series:
Linux Firewall Tutorial: IPTables Tables, Chains, Rules Fundamentals IPTables Flush: Delete / Remove All Rules On RedHat and CentOS Linux Linux IPTables: How to Add Firewall Rules (With Allow SSH Example) Linux IPTables: Incoming and Outgoing Rule Examples (SSH and HTTP)