Skip to content

Instantly share code, notes, and snippets.

@mauriciomdea
Created August 17, 2012 15:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauriciomdea/3379747 to your computer and use it in GitHub Desktop.
Save mauriciomdea/3379747 to your computer and use it in GitHub Desktop.
Basic Firewall configuration for new VPS
#!/bin/bash
# Accept already established connections (so it doesnt drop your current SSH session)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Accept SSH connections from anywhere
iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT
# (Optional) Accept HTTP connections from anywhere
iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT
# Block all other connections!
iptables -A INPUT -j DROP
# Unblock loopback interface
iptables -I INPUT 4 -i lo -j ACCEPT
# Enable logging of denied connections
iptables -I INPUT 5 -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Print current iptables rules
iptables -L -v
# Save current rules
iptables-save > /etc/iptables.up.rules
# (Manual step) Loading rules after boot:
# vi /etc/network/interfaces
# auto eth0
# iface eth0 inet dhcp
# pre-up iptables-restore < /etc/iptables.up.rules
# post-down iptables-restore < /etc/iptables.down.rules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment