iptables常用命令

Linux知识
0 766

iptables常用命令

常用操作命令:

-A 或 -append 在所选链尾加入一条或多条规则

-D 或 -delete 在所选链尾部删除一条或者多条规则

-R 或 -replace 在所选链中替换一条匹配规则

-I 或 -insert 以给出的规则号在所选链中插入一条或者多条规则. 如果规则号为1,即在链头部.

-L 或 -list 列出指定链中的所有规则,如果没有指定链,将列出链中的所有规则.

-F 或 -flush 清除指定链和表中的所由规则, 假如不指定链,那么所有链都将被清空.

-N 或 -new-chain 以指定名创建一条新的用户自定义链,不能与已有链名相同.

-X 或 -delete-chain 删除指定的用户定义帘,必需保证链中的规则都不在使用时才能删除,若没有指定链,则删除所有用户链.

-P 或 -policy 为永久帘指定默认规则(内置链策略),用户定义帘没有缺省规则,缺省规则也使规则链中的最后一条规则,用-L显示时它在第一行显示.

-C 或 -check 检查给定的包是否与指定链的规则相匹配.

-Z 或 -zero 将指定帘中所由的规则包字节(BYTE)计数器清零.

-h 显示帮助信息.

范例:

[root@localhost script]# iptables -helpiptables v1.4.7

Usage: iptables -[AD] chain rule-specification [options]iptables -I chain [rulenum] rule-specification [options]iptables -R chain rulenum rule-specification [options]iptables -D chain rulenum [options]iptables -[LS] [chain [rulenum]] [options]iptables -[FZ] [chain] [options]iptables -[NX] chainiptables -E old-chain-name new-chain-nameiptables -P chain target [options]iptables -h (print this help information)

QQ图片20131218191912

附上一段iptables初始化的脚本: 

#!/bin/sh

#The author by jack

echo "Setting firewall . . . . start"

#--------RULESET INIT----------#
iptables -F
iptables -X
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
#------------------------------#
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -i eth0 -p tcp ! --syn -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
#------------------------------#
#zabbix
iptables -A INPUT -p tcp --destination-port 10050 -j ACCEPT
iptables -A INPUT -p udp --destination-port 10051 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 10050 -j ACCEPT
iptables -A OUTPUT -p udp --destination-port 10051 -j ACCEPT
#for web
iptables -A INPUT -p tcp --destination-port 21 -j ACCEPT
iptables -A INPUT -p tcp --destination-port 80 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 80 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 21 -j ACCEPT
#for mysql
iptables -A INPUT -p tcp --destination-port 3306 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 3306 -j ACCEPT
#for mail
iptables -A INPUT -p tcp --destination-port 25 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 25 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 110 -j ACCEPT
#for ssh
iptables -A INPUT -p tcp -s any/0 --destination-port 22 -j ACCEPT
iptables -N icmp_allowed
iptables -A icmp_allowed -p ICMP --icmp-type 11 -j ACCEPT
iptables -A icmp_allowed -p ICMP --icmp-type 8 -j ACCEPT
iptables -A icmp_allowed -p ICMP -j DROP
iptables -A OUTPUT -p icmp -j ACCEPT
iptables -A INPUT -p icmp -j ACCEPT
iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT
iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT
/etc/init.d/iptables save