linux防火墙firewalld

目录

一、firewalld的理论

1、firewalld的概述

2、iptables和firewall的区别

3、firewalld的区域

4、九个区域的作用

5、firewall数据的处理流程

二、配置firewalld防火墙

1、运行配置和永久配置

2、配置方法

3、案例演示

4、配置管理


一、firewalld的理论

1、firewalld的概述

firewalld防火墙是centos7默认的防火墙管理工具,取代了之前的iptables防火墙,也是工作在网络层,属于包过滤包防火墙。

firewalld和iptables一样都是防火墙管理工具(属于用户态),来定义防火墙的各种规则功能,内部结构都指向netfilter网络过滤子系统(属于内核态)来实现包过滤防火墙功能。

firewalld提供了支持网络区域所定义的网络连接以及接口安全等级的动态防火墙管理工具。它支持ipv4和ipv6设置以及以太网桥,并且拥有两种配置:运行时配置与永久配置。

2、iptables和firewalld的区别

iptables主要是基于接口,来设置规则,从而判断网络的安全性。firewalld是基于区域,根据不同区域来设置不同的规则,从而保证网络的安全,与硬件防火墙设置类似。

iptables在 /etc/sysconfig/iptables存储配置。

firewalld将配置存储在/etc/firewalld (优先加载)和/usr/lib/firewalld(默认的配置文件中)中的各种XML文件里。

使用iptables每一个单独更改意味着清除所有旧有的规则和从/etc/sysconfig/iptables里读取所有的新规则。使用firewalld却不会创建任何新的规则,仅仅运行规则中的不同之处。因此firewalld可以在运行时间内,改变设置而不丢失现行连接。

iptables防火墙类型为静态防火墙

firewalld防火墙类型为动态防火墙

3、firewalld的区域

firewalld防火墙为了简化管理,将所有网络流量分为多个区域。然后根据数据包的源ip地址或传入的网络接口等条件将流量传入相应区域。每个区域都定义自己打开或者关闭的端口和服务列表。

4、九个区域的作用

(1)trusted(信任区域)

允许所有的传入流量

(2)public (公共区域)

允许与ssh和或dhcp6-client预定义服务匹配的传入流量,其余均拒绝。是新添加网络接口的默认区域。

(3)external (外部区域)

允许与ssh预定义服务匹配的传入流量,其余均拒绝。默认将通过此区域转发的IPv4传出流量进行地址伪装,可用于路由服务器启用了伪装功能的外部网络。

(4)home (家庭区域)

允许与ssh、mdns、samba-client或dhcpv6-client预定义服务匹配的传入流量,其余均拒绝。

(5)internal (内部区域)

默认值与home区域相同

(6)work  (工作区域)

允许与ssh、dhcpv6-client预定义服务匹配的传入流量,其余均失败

(7)dmz   (隔离区域也被称为非军事区域)

允许与ssh预定义服务匹配的传入流量,其余均失败

(8)block (限制区域)

拒绝所有传入流量

(9)drop  (丢弃区域)

丢弃所有传入流量,并且不产生包含ICMP的错误详细

3、firewalld数据的处理流程

1.若源地址关联到特定的区域(即源地址或接口绑定的区域有冲突),则执行该区域所制定的规则。
2.若源地址未关联到特定的区域(即源地址或接口绑定的区域没有冲突),则使用传入网络接口的区域并执行该区域所制定的规则。
3.若网络接口也未关联到特定的区域(即源地址或接口都没有绑定特定的某个区域),则使用默认区域并执行该区域所制定的规则。
 

二、配置firewalld

1、运行配置与永久配置

(1)运行配置

实时生效,并持续至firewalld重新启动或重新加载配置

不中断现有连接

不能修改服务配置

(2)永久配置

不立即生效,除非firewalld重新启动或重新加载配置

中断现有连接

可以修改服务配置

2、配置方法

Firewall-config图形工具

Firewall-cmd命令行工具

/etc/firewalld/中的配置文件

Firewalld会优先使用/etc/firewall/中的配置,如果不存在配置文件,则使用/usr/lib/firewalld/的配置

/etc/firewalld:用户自定义配置文件,需要时可通过从/usr/lib/firewalld/中拷贝。

usr/lib/firewalld:默认配置文件,不建议修改,若恢复至默认配置,可直接删除/etc/firewalld/中的配置。

3、案例演示

(1)首先启动firewalld

[root@localhost ~]# systemctl start firewalld   #开启防火墙
[root@localhost ~]# systemctl status firewalld  #查看防火墙状态
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 二 2022-04-26 18:31:02 CST; 1min 50s ago
     Docs: man:firewalld(1)

(2) 显示默认区域

[root@localhost ~]# firewall-cmd --get-default-zone 
public

(3) 设置默认区域为internal (内部区域)

[root@localhost ~]# firewall-cmd --set-default-zone=internal  #设置默认区域为内部区域
success
[root@localhost ~]# firewall-cmd --get-default-zone           #查看默认配置区域       
internal

 (4)显示当前正在使用的区域及其对应的网卡接口

[root@localhost ~]# firewall-cmd --get-active-zones  #显示当前正在使用的区域及其对应的网卡接口
internal
  interfaces: ens33 ens37

 (5)显示指定接口ens33绑定的区域

[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens37   #查看指定网卡的默认区域
internal

 (6)为指定接口ens37绑定区域dmz

[root@localhost ~]# firewall-cmd --zone=dmz --add-interface=ens37
The interface is under control of NetworkManager, setting zone to 'dmz'.
success

 (7) 为指定的区域更改绑定的网络接口

[root@localhost ~]# firewall-cmd --zone=internal --change-interface=ens37
The interface is under control of NetworkManager, setting zone to 'internal'.
success

4、配置管理

1、所有指定区域的所有规则

[root@localhost ~]# firewall-cmd --zone=internal --list-all
internal (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33 ens37
  sources: 
  services: ssh mdns samba-client dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

 2、显示internal区域内允许访问的所有服务

[root@localhost ~]# firewall-cmd --zone=internal --list-services 
ssh mdns samba-client dhcpv6-client

 3、删除dhcpv6-client服务

[root@localhost ~]# firewall-cmd --zone=internal --remove-service=dhcpv6-client 
success

 4、添加dhcpv6-client服务

[root@localhost ~]# firewall-cmd --zone=internal --add-service=dhcpv6-client 
success

 5、允许TCP的22端口到internal区域

[root@localhost ~]# firewall-cmd --zone=internal --add-port=22/tcp
success
[root@localhost ~]# firewall-cmd --list-all --zone=internal 
internal (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33 ens37
  sources: 
  services: ssh mdns samba-client dhcpv6-client
  ports: 22/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值