Basic IPtables Firewall for Servers
*filter
# Allow all loopback traffic
-A INPUT -i lo -j ACCEPT
-A INPUT -i lo -d 127.0.0.0/8 -j REJECT
# Accept all established inbound connections
-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# Allow all outbound traffic
-A OUTPUT -j ACCEPT
# Allow HTTP and HTTPS connections
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT
# Allow SSH connections
#
# The -dport number should be the same port number you set in sshd_config
-A INPUT -p tcp -m conntrack --ctstate NEW --dport 22 -m recent --set --name SSH
-A INPUT -p tcp -m conntrack --ctstate NEW --dport 22 -m recent --update --seconds 60 --hitcount 4 --rttl --name SSH -j DROP
-A INPUT -p tcp -m conntrack --ctstate NEW --dport 22 -j ACCEPT
# Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# Allow database connections from a specific source IP address
-A INPUT -p tcp --dport 3306 -s 123.456.789.10/24 -j ACCEPT
# Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Reject all other inbound traffic
-A INPUT -j REJECT
-A FORWARD -j REJECT
COMMIT
Written by Dolores Portalatin
Related protips
1 Response
I forgot to mention: conntrack is a connection tracking tool that can see and edit the state of connections to your server. You will need a kernel that is >= 2.6.18.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Security
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#