第七章 Iptables与Firewalld防火墙

文章目录

第七章 Iptables与Firewalld防火墙

一、Iptables
1、策略与规则链
(1)、防火墙策略规则的设置

一种是“通”即放行,另一种是“堵”即阻止。 当防火墙的默认策略为拒绝时(堵),就要设置允许规则通,否则谁都进不来。
如果防火墙的默认策略为允许时,就要设置拒绝规则,否则谁都能进来,防火墙也就失去了防范的作用。

(2)、数据包处理位置

iptables服务把用于处理或过滤流量的策略条目称之为规则,多条规则可以组成一个规则链,而规则链则依据数据包处理位置的不同进行分类,具体如下:

在进行路由选择前处理数据包(PREROUTING)
处理流入的数据包(INPUT)
处理流出的数据包(OUTPUT)
处理转发的数据包(FORWARD)
在进行路由选择后处理数据包(POSTROUTING)
2、基本的命令参数
(1)、iptables中常用的参数以及作用
参数 作用
-P 设置默认策略
-F 清空规则链
-L 查看规则链
-A 在规则链的末尾加入新规则
-l num 在规则链的头部加入新规则
-D num 删除某一条规则
-s 匹配来源地址IP/MASK,加感叹号表示出这个IP外
-d 匹配目标地址
-i 网卡名称 匹配从这块网卡流入的数据
-o 网卡名称 匹配从这块网卡流出的数据
-p 匹配协议
–dport num 匹配目标端口号
–sport num 匹配来源端口号
(2)、查看已有的防火墙规则链
[root@centos ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
LIBVIRT_INP  all  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
LIBVIRT_FWX  all  --  anywhere             anywhere            
LIBVIRT_FWI  all  --  anywhere             anywhere            
LIBVIRT_FWO  all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
LIBVIRT_OUT  all  --  anywhere             anywhere            

Chain LIBVIRT_INP (1 references)
target     prot opt source               destination         
ACCEPT     udp  --  anywhere             anywhere             udp dpt:domain
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:domain
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootps
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:bootps

Chain LIBVIRT_OUT (1 references)
target     prot opt source               destination         
ACCEPT     udp  --  anywhere             anywhere             udp dpt:domain
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:domain
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootpc
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:bootpc

Chain LIBVIRT_FWO (1 references)
target     prot opt source               destination         
ACCEPT     all  --  192.168.122.0/24     anywhere            
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable

Chain LIBVIRT_FWI (1 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             192.168.122.0/24     ctstate RELATED,ESTABLISHED
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable

Chain LIBVIRT_FWX (1 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere  
(3)、清空已有的防火墙规则链并查看
//清空规则链
[root@centos ~]# iptables -F
//查看规则链
[root@centos ~]# 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         

Chain LIBVIRT_INP (0 references)
target     prot opt source               destination         

Chain LIBVIRT_OUT (0 references)
target     prot opt source               destination         

Chain LIBVIRT_FWO (0 references)
target     prot opt source               destination         

Chain LIBVIRT_FWI (0 references)
target     prot opt source               destination         

Chain LIBVIRT_FWX (0 references)
target     prot opt source               destination  
(4)把INPUT规则链的默认策略设置为拒绝并查看
//更改默认策略
[root@centos ~]# iptables -P INPUT DROP
//查看规则链
[root@centos ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
(5)、向INPUT链中添加允许ICMP流量进入的策略规则
//设置策略规则
[root@centos ~]# iptables -I INPUT -p icmp -j ACCEPT 
(6)、删除INPUT规则链中刚刚加入的那条策略,并把默认策略设置为允许
//删除策略
[root@centos ~]# iptables -D INPUT <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liebe1*1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值