文章转载自:http://www.itwhy.org/2011/08-16/912.html

这是一个非常简单的 iptables 脚本,你只需要把要开放的端口写入 “$MPort” 变量中就行了,记住最多不能超过16个端口哦。这个脚本虽然简单,但能实现我们的基本要求,而且有时候简单的脚本作用更大,因为繁杂而臃肿不但难于理解,还会拖慢机器处理速度,呵呵。

相关解释
modprobe nf_conntrack_ftp ports=21
modprobe nf_nat_ftp
开放FTP端口,需要载入这两个模块。如果你修改了FTP默认连接端口,也需要修改此处:“modprobe nf_conntrack_ftp ports=21”

iptables -A INPUT -s 127.0.0.1/32 -j ACCEPT
本地回路,这个必需的。

iptables -A INPUT -p tcp -m state –state related,established -j ACCEPT
匹配已有连接和与已有连接相关的,FTP需要这个。

iptables -A INPUT -p icmp –icmp-type 8 -m limit –limit 1/s –limit-burst 3 -j ACCEPT
限制 ping 检查速度。

iptables.sh

#!/bin/bash
#author: InBi
#date: 2011-08-16
#website: http://www.itwhy.org/2011/07-28/912.html
##################################################
MPort="80,21,2200,8080"
##################################################
modprobe nf_conntrack_ftp ports=21
modprobe nf_nat_ftp
/sbin/iptables -t filter -Z;iptables -t filter -F;iptables -t filter -X
/sbin/iptables -P INPUT DROP;iptables -P OUTPUT ACCEPT;iptables -P FORWARD DROP

/sbin/iptables -A INPUT -p tcp -m state --state related,established -j ACCEPT
/sbin/iptables -A INPUT -p tcp ! --syn -m state --state new -j DROP
/sbin/iptables -A INPUT -s 127.0.0.1/32 -j ACCEPT
/sbin/iptables -A INPUT -p udp --sport 53 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -m multiport --dports $MPort -j ACCEPT
/sbin/iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 1/s --limit-burst 3 -j ACCEPT

把以上内容保存至iptables.sh(文件名自取),然后加上执行权限,加入开机启动就OK了。

更多学习iptables的资料请阅读:iptables 命令指南