Skip to content

Instantly share code, notes, and snippets.

@fornit1917
Last active April 6, 2018 19:09
Show Gist options
  • Save fornit1917/507f4e30f71e35b4b398d3827263b6f7 to your computer and use it in GitHub Desktop.
Save fornit1917/507f4e30f71e35b4b398d3827263b6f7 to your computer and use it in GitHub Desktop.
Close ports by iptables
#!/bin/bash
#
# Flush iptables
#
iptables -F
#
# Allow SSH, HTTP, HTTPS
#
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#
# Set default policies for INPUT, FORWARD and OUTPUT chains
#
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
#
# Set access for localhost
#
iptables -A INPUT -i lo -j ACCEPT
#
# Accept packets belonging to established and related connections
#
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#
# Save settings
#
/sbin/service iptables save
#
# List rules
#
iptables -L -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment