适合Web服务器的iptables规则
    #! /bin/sh
    # /etc/iptables.bak
     
    # Let's save typing & confusion with variables
    IPTABLES=/sbin/iptables
     
    # Flush active rules and custom tables
    $IPTABLES --flush
    $IPTABLES --delete-chain
     
    # set the defaults so that by-default incoming packets are dropped, unless explicitly allowed;
    # for a desktop workstation, we'll let lots of (unpredictable) outgoing packets go freely.
    $IPTABLES -P INPUT DROP
    $IPTABLES -P FORWARD DROP
    $IPTABLES -P OUTPUT ACCEPT
     
    # INBOUND POLICY
    # ==============
    # of course, accepting loopback is a good idea
    $IPTABLES -A INPUT -i lo -j ACCEPT
     
    # we will permit ping, but rate-limit type 8 to prevent DoS-attack
    $IPTABLES -A INPUT -p icmp --icmp-type 0 -j ACCEPT
    $IPTABLES -A INPUT -p icmp --icmp-type 3 -j ACCEPT
    $IPTABLES -A INPUT -p icmp --icmp-type 11 -j ACCEPT
    $IPTABLES -A INPUT -p icmp --icmp-type 8 -m limit --limit 1/second -j ACCEPT
     
    #   (Applies to packets entering our network interface from the network,
    #   and addressed to this host.)
     
    $IPTABLES -A INPUT -m state --state INVALID -j DROP
    $IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
     
    # ftp incoming
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 20 -j ACCEPT
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 21 -j ACCEPT
     
    # ssh incoming, including non-standard port (if needed)
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
    #$IPTABLES -A INPUT -p tcp -m state --state NEW --dport 222 -j ACCEPT
     
    # web serving, let's allow it!
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT
     
    # secure web serving, let's allow it!
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 443 -j ACCEPT
     
    # amanda tape-backups; we reach out and tape things from this machine
    $IPTABLES -A INPUT -p udp -m state --state NEW -m udp --dport 10080 -j ACCEPT
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 10082 -j ACCEPT
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 10083 -j ACCEPT
     
    # nagios (5666); monitor time (123), allow snmp (161)
    $IPTABLES -A INPUT -p tcp -m state --state NEW --dport 5666 -j ACCEPT
    $IPTABLES -A INPUT -p udp -m udp --dport 123 -j ACCEPT
    $IPTABLES -A INPUT -p udp -m udp --dport 161 -j ACCEPT
     
     
    # OUTBOUND POLICY
    # ===============
    # of course, accepting loopback is a good idea
    $IPTABLES -A OUTPUT -o lo -j ACCEPT
     
    #   (Applies to packets sent to the network interface from local processes)
     
    $IPTABLES -A OUTPUT -j ACCEPT

根据自己的服务器的具体环境作适当修改,然后把上面的代码保存到/etc/iptables.bak。
运行脚本:
sh /etc/iptables.bak
查看规则:
iptables -L
保存规则:
service iptables save


IPTABLES 开 FTP 问题的解决

modprobe ip_nat_ftp
modprobe ip_conntrack_ftp
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

先试前边的几条留住最后一条,假如不行,在把最后一条加上~
另:

iptables配置FTP的主动和被动模式

FTP协议有两种工作方式:PORT方式和PASV方式,中文意思为主动式和被动式。

Port模式:ftp server:tcp 21 <------client:dynamic ftp server:tcp 20 ------>client:dynamic

Pasv模式:ftp server:tcp 21 <----client:dynamic ftp server:tcp dynamic <----client:dynamic

主动与被动是针对于ftp服务器来说的,ftp服务器主动连接客户端,成为主动。 ftp服务器被动滴等待客户端连接称为被动


PORT(主动)方式的连接过程是:客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路。当需要传送数据时,客户 端在命令链路上用PORT命令告诉服务器:“我打开了XXXX端口,你过来连接我”。于是服务器从20端口向客户端的XXXX端口发送连接请求,建立一条 数据链路来传送数据。


PASV(被动)方式的连接过程是:客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路。当需要传送数据时,服务 器在命令链路上用PASV命令告诉客户端:“我打开了XXXX端口,你过来连接我”。于是客户端向服务器的XXXX端口发送连接请求,建立一条数据链路来 传送数据。

#allow all ftp incoming connections
iptables -A INPUT -p tcp --dport 21 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 21 -m state --state NEW,ESTABLISHED -j ACCEPT

# Enable active ftp transfers
iptables -A INPUT -p tcp --dport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 20 -m state --state ESTABLISHED -j ACCEPT

# Enable passive ftp transfers
iptables -A INPUT -p tcp --sport 1024:65535 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 1024:65535 --dport 1024:65535 -m state --state ESTABLISHED,RELATED -j ACCEPT

Pages:12