Skip to content

Instantly share code, notes, and snippets.

@utamas
Created January 1, 2016 17:39
Show Gist options
  • Save utamas/254b83421be038f65136 to your computer and use it in GitHub Desktop.
Save utamas/254b83421be038f65136 to your computer and use it in GitHub Desktop.
Sets up ubuntu firewall, dropping packages on ports that are not whitelisted.
setupFirewall() {
# Flushing firewall rules.
sudo iptables -F
# Enabling outgoing packets.
sudo iptables -P OUTPUT ACCEPT
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -A INPUT --in-interface lo -j ACCEPT
local ports=(22 80 443)
for port in "${ports[@]}"; do
sudo iptables -A INPUT -p tcp --dport $port -j ACCEPT
done
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables-save > /etc/iptables.conf
echo "post-up iptables-restore < /etc/iptables.conf" | sudo tee -a /etc/network/interfaces
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment