这些天想给vps加个防火墙iptables,经验证下面的规则有效可行,只开放了SSH、FTP、WEB。
需要的朋友,可以直接把下面的代码保存后运行,比如保存成ip.sh,然后运行sh ./ip.sh;如果是远程千万别手工一条条运行,会把自己给断开的;推荐在screen下运行。
- #!/bin/sh
- #清除全部规则
- iptables -F
- iptables -X
- #设置默认规则
- iptables -P INPUT DROP
- iptables -P OUTPUT ACCEPT
- iptables -P FORWARD DROP
- #允许SSH
- iptables -A INPUT -p tcp --dport 22 -j ACCEPT
- #允许WEB
- iptables -A INPUT -p tcp --dport 80 -j ACCEPT
- #允许FTP
- iptables -A INPUT -p tcp --dport 21 -j ACCEPT
- iptables -A INPUT -p tcp --dport 20 -j ACCEPT
- iptables -A INPUT -p tcp --dport 30000:50000 -j ACCEPT
- #允许kloxo
- iptables -A INPUT -p tcp --dport 7778 -j ACCEPT
- #iptables -A INPUT -p icmp -j ACCEPT
- iptables -A INPUT -i lo -p all -j ACCEPT
- iptables -A OUTPUT -o lo -p all -j ACCEPT
- iptables -A FORWARD -p TCP ! --syn -m state --state NEW -j DROP
- iptables -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT
- iptables -A FORWARD -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
- iptables -A INPUT -m state --state INVALID -j DROP
- iptables -A OUTPUT -m state --state INVALID -j DROP
- iptables -A FORWARD -m state --state INVALID -j DROP
- iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- iptables -A INPUT -i lo -j ACCEPT
- iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
- /etc/rc.d/init.d/iptables save
- service iptables restart
该脚本为个人自用,并未开放太多服务,如果需要开放其它服务,请自行增加或更改端口。