您的位置:首页 > 其它

防火墙iptables 应用

2013-09-11 12:04 92 查看
iptables基本语法格式
iptables -t表名 命令选项 链名 条件匹配 -j目标动作或跳转

-A 在指定链的末尾添加一条规则

-D 删除指定链中某一条规则,按规则号或内容确定要删除的规则

-I 在指定链中插入一条新规则,若未指定插入位置,默认在链的开头

-R 修改、替换指定链中的一条规则,按规则号或内容确定要替换的规则

-L 列出指定链中所有规则进行查看,未指定链名,列出所有链

-F 清空指定链中所有规则,若未指定链名,则清空表中所有链内容

-N 新建一条用户自定义规则链

-X 删除表中用户自定义规则链

-P 设置指定链的默认策略

-n 使用数字形式显示输出信息,如显示IP非主机名

-v 查看规则列表时显示的详细信息

-V 查看版本信息

-h 查看命令帮助信息

--line-numbers 查看规则列表时,显示规则在链中的顺序号

具体应用
A.删除、清空规则
1.删除filter表INPUT链的第二条规则

iptables -D INPUT 2

2.不指定表名,默认清空filter表

iptables -F

3.清空nat/mangle表所有规则

iptables -t nat -F

iptables -t mangle -F

4.删除filter中使用者自定义的规则链

iptables -X

B.设置规则链的默认策略
1.将filter表中FORWARD规则的默认策略设定为DROP

iptables -t filter -P FORWARD DROP

2.将filter表中OUTPUT规则默认策略设置为ACCEPT

iptables -P OUTPUT ACCEPT

C.查看、替换规则
1.将INPUT链中原来编号为3的规则内容替换为 -j ACCEPT。注意编号3规则需要存在,否则报错

iptables -R INPUT 3 -j ACCEPT

2.查看filter表中INPUT链中的所有规则,同时显示各规则顺序号

iptables -L INPUT --line-numbers

3.-L需要放在最后,以数字形式显示地址和端口号

iptables -nvL

D.具体的规则匹配
1.丢弃从外网接口eth1进入防火墙本机的源地址为私网地址的数据(默认通过)

iptables -A INPUT -i eth1 -s 192.168.0.0/16 -j DROP

iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP

iptables -A INPUT -i eth1 -s 172.16.0.0/12 -j DROP
2.某个网段扫描主机,希望封堵IP地址段,两小时后解封

iptables -I INPUT -s 10.20.30.0/24 -j DROP

iptables -I FORWARD -s 10.20.30.0/24 -j DROP

#at now +2 hours

at>iptables -D INPUT 1

at>iptables -D FORWARD 1

at>EOF
3.拒绝转发来自192.168.1.11主机的数据

iptables -A FORWARD -s 192.168.1.11 -j REJECT
4.允许转发来自192.168.0.0/24网段的数据

iptables -A FORWARD -s 192.168.0.0/24 -j ACCEPT
5.允许或拒绝ipcp 数据包   协议匹配

iptables -I INPUT -p icmp -j REJECT/ACCEPT
6.允许转发除icmp以外所有数据包 !取反

iptables -I INPUT -p !icmp -j ACCEPT
7.允许202.13.0.0/16用SSH远程登陆防火墙主机 端口匹配

iptables -A INPUT -p tcp--dport 22 -s 202.13.0.0/16 -j ACCEPT

iptables -A INPUT -p tcp--dport 22 -j DROP
8.允许本机开放从TCP端口20-1024提供的服务 端口匹配

iptables -A INPUT -p TCP --dport 20:1024  -j ACCEPT

iptables -A OUTPUT -p TCP --sport 20:1024  -j ACCEPT
9.允许转发来自192.168.0.0/24网络的DNS解析 端口匹配

iptables -A FORWARD -p udp -s 192.168.0.0/24 --dport 53 -j ACCEPT

iptables -A FORWARD -p udp -d 192.168.0.0/24 --sport 53 -j ACCEPT
10.拒绝从外网接口eth1直接访问防火墙本机数据包,但是允许相应防火墙TCP请求的数据包进入 TCP标记匹配

iptables -P INPUT DROP

iptables -I INPUT -i eth1 -p tcp--tcp-flags SYN,RST,ACK SYN -j ACCEPT

iptables -I INPUT -i eth1 -p tcp--tcp-flags!--syn -j ACCEPT
11.禁止其它主机ping防火墙主机,但是允许从防火墙上ping其它主机,允许接收ICMP回应数据 ICMP类型匹配

iptables -A INPUT -p icmp--icmp-type Echo-Request -j DROP

iptables -A INPUT -p icmp--icmp-type Echo-Replay -j ACCEPT

iptables -A INPUT -p icpm--icmp-type destination-Unreachable -j ACCEPT

Echo-Request 数字代码为8 ,请求

Echo-Replay 数字代码为0.回显

destination-Unreachable .3  目标不可到达

 

 导出、导入防火墙规则
1.iptables-save  
把当前设置的防火墙规则信息输出到终端  将当前调试好的 iptables规则保存到配置文件,并通过 iptables服务脚本自动加载

# iptables-save > /etc/sysconfig/iptables  或  service iptables save

# service iptables restart

# chkconfig - -level 35 iptables on 

 
2.iptalbes-restore  
从已保存的配置文件中导入 iptables规则

# iptables-retore < /etc/sysconfig/iptables

本文出自 “还不算晕” 博客,请务必保留此出处http://haibusuanyun.blog.51cto.com/2701158/761852
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: