IPTables Template

Below is a basic template for setting up IPTables on a Linux box :

Create LOGDROP CHAIN. Add LOG and DROP to chain.

iptables -N LOGDROP
iptables -A LOGDROP -j LOG --log-level 6
iptables -A LOGDROP -j DROP

Create input and output policies

iptables --policy INPUT DROP
iptables --policy OUTPUT DROP

Add inbound rules

iptables -A INPUT  -m state --state ESTABLISHED,RELATED                 -j ACCEPT
iptables -A INPUT -p icmp                 --icmp-type echo-request     -j ACCEPT
iptables -A INPUT -p tcp                 --dport ssh             -j ACCEPT
iptables -A INPUT -j LOGDROP

Add outbound rules

iptables -A OUTPUT  -m state --state ESTABLISHED,RELATED                 -j ACCEPT
iptables -A OUTPUT -p icmp                 --icmp-type echo-request    -j ACCEPT
iptables -A OUTPUT -p tcp                 --dport 443             -j ACCEPT
iptables -A OUTPUT -p tcp                 --dport 80             -j ACCEPT
iptables -A OUTPUT -p udp                 --dport 53             -j ACCEPT
iptables -A OUTPUT -p udp     -d [syslog server ip]    --dport 514             -j ACCEPT
iptables -A OUTPUT -o lo                                -j ACCEPT
iptables -A OUTPUT -j LOGDROP

Save Config

Rick Donato

Want to become a Linux expert?

Here is our hand-picked selection of the best courses you can find online:
Linux Mastery course
Linux Administration Bootcamp
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial