iptables基本管理

iptables基本管理

问题

本案例要求熟悉iptables工具的基本管理,分别练习以下几方面的操作:
查看当前生效的防火墙规则列表
追加、插入新的防火墙规则,修改现有的防火墙规则
删除、清空指定的防火墙规则

方案

采用两台RHEL6虚拟机,在其中svr5上配置iptables防火墙规则,pc205作为测试用的客户机,如图所示。
在这里插入图片描述
步骤
实现此案例需要按照如下步骤进行。
步骤一:查看当前生效的防火墙规则列表
1)列出filter表的规则
filter表是iptables缺省操作的表,因此 -t filter 通常被省略。
filter表默认包括INPUT、OUTPUT、FORWARD这三个规则链。

[root@svr5 ~]# iptables -L  								//查看所有规则链
Chain INPUT (policy ACCEPT)
target     prot opt source               destination 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

[root@svr5 ~]# iptables -L INPUT  							//只看INPUT链
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

2)列出nat表的规则
nat表默认包括PREROUTING、POSTROUTING、OUTPUT这三个规则链。

[root@svr5 ~]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination 

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

3)列出raw表的规则
raw表默认包括PREROUTING、OUTPUT这两个规则链。

[root@svr5 ~]# iptables -t raw -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

4)列出mangle表的规则
mangle表默认包括所有的五种规则链。

[root@svr5 ~]# iptables -t mangle -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination 

Chain INPUT (policy ACCEPT)
target     prot opt source               destination 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination

步骤二:追加、插入新的防火墙规则
1)-A,在末尾追加一条新的防火墙规则
允许访问本机的所有TCP数据包:

[root@svr5 ~]# iptables -A INPUT -p tcp -j  ACCEPT   		//添加一条规则
[root@svr5 ~]# iptables -L INPUT  							//查看结果
Chain INPUT (policy ACCEPT)
target     prot opt source               destination 
ACCEPT     tcp  --  anywhere             anywhere

在查看规则时,可以结合-n选项以数字形式显示地址、端口等信息:

[root@svr5 ~]# iptables -nL INPUT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination  
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0

还可以结合–line-numberrs来显示规则的行号(顺序号):

[root@svr5 ~]# iptables -nL INPUT --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination   
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0  		//当前链的第1条规则

2)-I,在开头或指定位置插入一条新的防火墙规则
在INPUT链的开头插入规则,允许所有的UDP协议数据包:

[root@svr5 ~]# iptables -I INPUT -p udp -j ACCEPT
[root@svr5 ~]# iptables -nL INPUT --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination 
1    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0    	//新增规则作为第一条
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0  		//原第一条变为第二条

插入INPUT链的第2条规则,允许所有的ICMP协议数据包:
[root@svr5 ~]# iptables -I INPUT 2 -p icmp -j ACCEPT

[root@svr5 ~]# iptables -nL INPUT --line-numbers 
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination  
1    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0  
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0  		//新增的第2条规则
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0

步骤三:删除、清空指定的防火墙规则
1)-D,删除指定的规则
删除INPUT链的第3条规则:

[root@svr5 ~]# iptables -D INPUT 3  						//删除第3条规则
[root@svr5 ~]# iptables -nL INPUT --line-numbers  			//确认删除结果
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination 
1    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0 
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0

删除指定具体内容的规则:

[root@svr5 ~]# iptables -D INPUT -p udp -j ACCEPT  			//删除UDP放行规则
[root@svr5 ~]# iptables -nL INPUT --line-numbers 			//确认删除结果
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination 
1    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
2)-F,清空规则

清空filter表的所有规则:

[root@svr5 ~]# iptables -F

[root@svr5 ~]# iptables -nL --line-numbers 
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination 

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

清空nat表的所有规则:

[root@svr5 ~]# iptables -t nat -F
[root@svr5 ~]# iptables -t mangle -F
[root@svr5 ~]# iptables -t raw -F
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值