13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

Centos7的Firewalld防火墙基础命令详解

一、Linux防火墙的基础

专注于为中小企业提供成都做网站、网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业德清免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了超过千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

Linux的防火墙体系主要工作在网络层,针对TCP/IP数据包实时过滤和限制,属于典型的包过滤防火墙(或称为网络层防火墙)。Linux系统的防火墙体系基于内核共存:firewalld、iptables、ebtables,默认使用firewalld来管理netfilter子系统。

1、firewalld概述

firewalld的作用是为包过滤机制提供匹配规则(或称为策略),通过各种不同的规则,告诉netfilter对来自指定源,前往指定目的或具有某些协议特征的数据包采取何种处理方式。为了更加方便地组织和管理防火墙,firewalld提供了支持网络区域所定义的网络链接以及接口安全等级的动态防火墙管理工具。支持IPv4、IPv6防火墙设置以及以太网桥,并且拥有两种配置模式:

还支持服务或应用程序直接添加防火墙规则接口。

2、firewalld网络区域

firewalld将所有的网络数据流量划分为多个区域,从而简化防火墙管理。根据数据包的源IP地址或传入网络接口等条件,将数据流量转入相应区域的防火墙规则。

对于进入系统的数据包,首先检查的就是其源地址:

默认区域不是单独的区域,而是指向系统上定义的某个其他区域。默认情况下,默认区域是public,但是也可以更改默认区域。以上匹配规则,按照先后顺序,第一个匹配的规则胜出。在每个区域中都可以配置其要打开或者关闭的一系列服务或端口,firewalld的每个预定义的区域都设置了默认打开的服务。

3、firewalld预定义区域说明

二、firewalld防火墙的配置方法

在Centos 7系统中,可以使用三种方式配置firewalld防火墙:

一般情况下,不建议直接编辑配置文件;

1、firewalld-cmd的基础命令

[root@centos01 ~]# systemctl start firewalld  
[root@centos01 ~]# systemctl enable firewalld
[root@centos01 ~]# systemctl status firewalld 
[root@localhost ~]# firewall-cmd --state 
running
[root@centos01 ~]# systemctl stop firewalld
[root@centos01 ~]# systemctl disable firewalld
[root@centos01 ~]# firewall-cmd --get-zones 
[root@centos01 ~]# firewall-cmd --get-service 
[root@centos01 ~]# firewall-cmd --get-default-zone 
[root@localhost /]# firewall-cmd --reload   
[root@centos01 ~]# firewall-cmd --get-active-zones 

[root@centos01 ~]# firewall-cmd --get-icmptypes 
address-unreachable bad-header communication-prohibited destination-unreachable 
echo-reply echo-request fragmentation-needed host-precedence-violation host-prohibited
host-redirect host-unknown host-unreachable ip-header-bad neighbour-advertisement
neighbour-solicitation network-prohibited network-redirect network-unknown 
network-unreachable no-route packet-too-big parameter-problem port-unreachable
precedence-cutoff protocol-unreachable redirect required-option-missing
router-advertisement router-solicitation source-quench source-route-failed time-exceeded
timestamp-reply timestamp-request tos-host-redirect tos-host-unreachable 
tos-network-redirect tos-network-unreachable ttl-zero-during-reassembly 
ttl-zero-during-transit unknown-header-type unknown-option

firewall-cmd –get-icmptypes命令执行结果中部分阻塞类型的含义如下:

2、firewalld区域管理选项

区域管理示例如下:

[root@centos01 ~]# firewall-cmd --get-default-zone 
[root@centos01 ~]# firewall-cmd --list-all
[root@centos01 ~]# firewall-cmd --get-zone-of-interface=ens32 
internal
[root@centos01 ~]# firewall-cmd --zone=internal --change-interface=ens32 
 The interface is under control of NetworkManager, setting zone to 'internal'.
success
[root@centos01 ~]# firewall-cmd --zone=internal --list-interface 
ens32
[root@centos01 ~]# firewall-cmd --get-active-zones  
internal
 interfaces: ens32

3、firewalld服务管理

为了方便管理,firewalld预先定义了很多服务,存放在/usr/lib/firewalld/services/目录中,服务通过单个的XML配置文件来指定。这些配置文件则按以下格式命名:service-name.xml,每个文件对应一项具体的网络服务,如ssh服务等。我们需要将service配置文件放置在/etc/firewalld/services/目录中。service配置具有以下优点:

通过服务名字来管理规则更加人性化;

通过服务来组织端口分组的模式更加高效,如果一个服务使用了若干个网络端口,则服务的配置文件就相当于提供了到这些端口的规则管理的批量操作快捷方式;

1)firewalld-cmd命令区域中服务管理的常用选项说明:

2)firewalld服务管理示例如下(为默认区域设置允许访问的服务):

 [root@centos01 ~]# firewall-cmd --list-services 
dhcpv6-client ssh 
[root@centos01 ~]# firewall-cmd --add-service=http 
success
[root@centos01 ~]# firewall-cmd --add-service=https 
success
[root@centos01 ~]# firewall-cmd --list-services 

dhcpv6-client ssh https http

3)firewalld服务管理示例如下(为internal区域设置允许访问的服务):

[root@centos01 ~]# firewall-cmd --zone=internal --add-service=mysql 
       
success
[root@centos01 ~]# firewall-cmd --zone=internal --remove-service=samba-client 
     
success
[root@centos01 ~]# firewall-cmd --zone=internal --list-services 
       
ssh mdns dhcpv6-client mysql

4、端口管理

在进行服务配置时,预定义的网络服务可以使用服务名配置,服务所涉及的端口就会自动打开。但是,对于非预定义的服务只能手动为指定的区域添加端口。例如,执行以下操作即可实现在internal区域打开443/TCP端口。示例如下:

 [root@centos01 ~]# firewall-cmd --zone=internal --add-port=443/tcp
         
success

若想实现在internal区域禁止443/TCP端口访问,可执行以下命令:

 [root@centos01 ~]# firewall-cmd --zone=internal --remove-port=443/tcp
        
success

以上配置都为临时配置,若想将当前配置保存为永久配置,可以使用下面命令:

 [root@centos01 ~]# firewall-cmd --runtime-to-permanent
success

直接配置为永久性规则,须带–permanent选项,如下:

[root@centos01 ~]# firewall-cmd --add-icmp-block=echo-request --permanent  

success
[root@centos01 ~]# firewall-cmd --zone=external --add-icmp-block=echo-request --permanent      
      
success

三、firewalld两种配置模式

前面提到firewall-cmd命令工具有两种配置模式:运行时模式(Runtime mode)表示当前内存中运行的防火墙配置,在系统或firewalld服务重启、停止时配置将失效;永久模式(Permanent mode)表示重启防火墙或重新加载防火墙时的规则配置,是永久存储在配置文件中的。

firewall-cmd命令工具与配置模式相关的选项有三个:

本篇文章到此结束,如果您有相关技术方面疑问可以联系我们技术人员远程解决,感谢大家支持本站!

成都网站建设选创新互联(☎:028-86922220),专业从事成都网站制作设计,高端小程序APP定制开发,成都网络营销推广等一站式服务。


本文标题:Centos7的Firewalld防火墙基础命令详解
URL链接:http://cdbrznjsb.com/article/cojeedg.html

其他资讯

让你的专属顾问为你服务