Basic iptables rules
17 Apr 2013 in TIL
This post is a quick one for me, as I'm sure I'll need these rules again at some point. I'll probably need to mix and match the rules, but these ones should cover most bases.
Allow input from all as the default policy
bashiptables -P INPUT ACCEPT
Erase old iptables rules
bashiptables -F
Accept all connections to a local device
bashiptables -A INPUT -i lo -j ACCEPT
Allow all established connections to transmit
bashiptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
Allow SSH from all hosts
bashiptables -A INPUT -p tcp --dport 22 -j ACCEPT
Allow HTTP from all hosts
bashiptables -A INPUT -p tcp --dport 80 -j ACCEPT
Allow Redis from only known hosts
bashiptables -A INPUT -s 192.168.1.1 -p tcp --dport 6379 -j ACCEPT
Allow mySQL from only known hosts
bashiptables -A INPUT -s 192.168.1.1 -p tcp --dport 3306 -j ACCEPT
Drop everything else
bashiptables -P INPUT DROPiptables -P FORWARD DROP
We trust the machine to send out valid traffic
bashiptables -P OUTPUT ACCEPT
Output the current rules
bashiptables -n -L -v --line-numbers
And save
bashiptables-save > /etc/iptables.rules
And setup for restore on reboot
bashif [[ "`cat /etc/network/interfaces`" != *pre-up* ]]thenecho "pre-up iptables-restore < /etc/iptables.rules" >> /etc/network/interfacesfi