iptables -V #看版本
iptables -h #看帮助
iptables -nL #数字列表显示
iptables默认加载的内核模块
lsmod|egrep "nat|filter" #centos5
lsmod|egrep "nat|filter|ipt" #centos6
通过modprobe把模块加到内核里,执行如下命令:
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state
执行完,再 lsmod|egrep "nat|filter" 就会有很多模块
iptables 命令行增加规则后,不用重启就会生效,可以测一下
iptables命令
-n 数字
-L 列表
-F 清空规则
-X 清空用户自定义链
-Z 清空链的记数器(大Z)
-A 添加规则到链的结尾
-I 添加规则到链第一条
-t 指定表
-p 协议(all tcp udp icmp)默认all
-p 大写P 改变默认规则
--dport 目地端口
--sport 源端口
-j 行为/动作 (ACCEPT DROP REJECT)
--line-number 显示序号
-D #删除规则
-s 指定源地址
-d 目的地址
! 非
-i 从网卡进入 input
-o 从网卡出去 output
-m multiport 多端口
--icmp-type ICMP的类型 any是ICMP全禁 8是禁ping
-m state --state 匹配网络状态
-m limit --limit n/{second/minute/hour} 限制指定时间 包的允许通过数量及并发数
禁止SSH访问
iptables -t filter -A INPUT -p tcp --dport 52113 -j DROP #默认filter,可以不加-t
iptables -nL --line-number #每条规则前会加序号
iptables -F #第一种清空方法,所有规则都清除
iptables -D INPUT 1 #指定链指定序号删除掉
iptables -I INPUT 2 -p tcp --dport 1000 -j DROP #数字2是说这条规则插到第二行,原先的变成第三行
禁止网段连入
iptables -A INPUT -s 10.0.0.0/24 -j DROP #基于客户端源地址
iptables -A INPUT ! -s 10.0.0.0/24 -j DROP #不是这个网段的都DROP ‘!’非 在s前还是后不同的系统版不一样
iptables -A INPUT -p tcp --dport 52113 -i eth0 ! -s 10.0.0.0/24 -j DROP
封掉3306端口
iptables -A INPUT -p tcp --dport 3306 -j DROP
iptables -I INPUT -s 10.0.0.16 -j DROP #范围大
iptables -I INPUT -p tcp -s 10.0.0.16 --dport 80 -j DROP #范围小 是这个源地址且访问80口会封
匹配端口范围
iptables -I INPUT -p tcp --dport 50000:60000 -j DROP
iptables -I INPUT -p tcp -m multiport --dport 21,22,23,24 -j ACCEPT
匹配ICMP类型
iptables -A INPUT -p icmp --icmp-type 8 -j DROP #8是ping
iptables -A INPUT -p icmp --icmp-type 8 -s 10.0.0.0/24 -j DROP
iptables -A INPUT -p icmp --icmp-type any -j DROP #any是整个ICMP全匹配
匹配网络状态
-m state --state
NEW已经或将启动新的连接
ESTABLISHED已建立的连接
RELATED正在启动的新连接
INVALID 非法或无法识别的
FTP服务是特殊的,需要匹配状态连接。
允许关联的状态包通过
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
限制指定时间 包的允许通过数量及并发数
-m limit --limit n/{second/minute/hour}
保存
/etc/init.d/iptables save
iptables-save> /etc/sysconfig/iptables #两种方式