firewalld防火墙基础配置
admin
2023-03-12 03:20:55
0次

Linux的防火墙体系主要工作在网络层,针对TCP/IP数据包实时过滤和限制,属于典型的包过滤防火墙。

Linux防火墙是如何检查数据流量的?

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

若源地址关联到特定的区域,则执行该区域所制定的规则;
若源地址未关联到特定的区域,则使用传入网络接口的区域并执行该区域所制定的规则。
若网络接口未关联到特定的区域,那么就使用默认区域并执行该区域所制定的规则。
默认区域并不是单独的区域,而是指向系统上定义的某个其他区域。默认情况下,默认区域是public,但是也可以更改默认区域。以上匹配规则,按照先后顺序,第一个匹配的规则胜出,和网络设备的ACL匹配规则差不多,俗称匹配即停。
firewalld的相关预定义区域:
firewalld防火墙基础配置
firewalld防火墙有两种配置模式:

1 、运行时模式:表示当前内存中运行的防火墙配置,在系统或firewalld服务重启、停止时将失效;

2、永久模式:表示重启防火墙或重新加载防火墙时的规则配置,是永久存储在配置文件中的。

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

--reload:重新加载防火墙规则并保持状态信息,即将永久配置应用为运行时配置;
--permanent:带有此选项的命令用于设置永久性规则,这些规则只有在重新启动或重新加载防火墙规则时才会生效;若不带此项,表示用于设置运行时规则。
--runtime-to-permanent:将当前运行时的配置写入规则配置文件中,使当前内存中的规则称为永久性配置。
1、防火墙相关命令使用:

[root@localhost ~]# systemctl start firewalld                #启动防火墙
[root@localhost ~]# systemctl enable firewalld            #设置开机自启动
[root@localhost /]# firewall-cmd --reload                     #重载防火墙
[root@localhost ~]# systemctl status firewalld             #查看防火墙状态
[root@localhost ~]# systemctl stop firewalld                #停止防火墙
[root@localhost ~]# systemctl disable firewalld            #设置防火墙开机不自启动
[root@localhost ~]# firewall-cmd --get-zones                #显示预定义的区域
block dmz drop external home internal public trusted work
[root@localhost ~]# firewall-cmd --get-service                     #显示预定义的服务
[root@localhost /]# firewall-cmd --list-all --zone=dmz                #查看指定区域的配置
dmz
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

[root@localhost ~]# firewall-cmd --get-icmptypes                #显示预定义的ICMP类型
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命令执行结果中部分阻塞类型的含义如下:

destination-unreachable:目的地址不可达;
echo-reply:应答回应;
parameter-problem:参数问题;
redirect:重新定向;
router-advertisement:路由器通告;
router-solicitation:路由器征寻;
source-quench:源端抑制;
time-exceeded:超时;
timestamp-reply:时间戳应答回应;
timestamp-request:时间戳请求;

2、firewalld防火墙区域管理命令及示例:

firewalld防火墙基础配置

[root@localhost /]# firewall-cmd --set-default-zone=dmz           #设置默认区域为DMZ区域
[root@localhost ~]# firewall-cmd --get-default-zone                #显示当前系统中的默认区域
[root@localhost ~]# firewall-cmd --list-all                                 #显示默认区域的所有规则
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33       #显示网络接口ens33对应区域
[root@localhost ~]# firewall-cmd --get-active-zones           #显示所有激活区域
internal
  interfaces: ens33

 #执行以下操作可将网络接口ens33对应区域更改为internal区域,并查看:
 [root@localhost ~]# firewall-cmd --zone=internal --change-interface=ens33
 The interface is under control of NetworkManager, setting zone to 'internal'.
success
[root@localhost ~]# firewall-cmd --zone=internal --list-interfaces    #查看internal区域的接口列表
ens33
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33    #查看接口ens33对应的区域
internal

3、防火墙服务管理相关命令及示例:
firewalld防火墙基础配置
部分服务管理的示例:

为默认区域设置允许访问的服务

 [root@localhost services]# firewall-cmd --list-services          #显示默认区域内允许访问的所有服务
ssh dhcpv6-client
[root@localhost services]# firewall-cmd --add-service=http     #设置默认区域允许访问http服务
success
[root@localhost services]# firewall-cmd --add-service=https     #设置默认区域允许访问https服务
success
[root@localhost services]# firewall-cmd --list-services               #显示默认区域内允许访问的所有服务

ssh dhcpv6-client http https

为internal区域设置允许访问的服务:

[root@localhost services]# firewall-cmd --zone=internal --add-service=mysql 
#设置internal区域允许访问mysql服务
success
[root@localhost services]# firewall-cmd --zone=internal --remove-service=samba-client 
#设置internal区域不允许访问Samba-client服务
success
[root@localhost services]# firewall-cmd --zone=internal --list-services 
#显示internal区域内允许访问的所有服务
ssh mdns dhcpv6-client mysql

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

 [root@localhost services]# firewall-cmd --zone=internal --add-port=443/tcp
 #在internal区域打开443/tcp端口
success
 [root@localhost services]# firewall-cmd --zone=internal --remove-port=443/tcp
 #在internal区域禁止443/tcp端口访问
success

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

 [root@localhost services]# firewall-cmd --runtime-to-permanent
success

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

[root@localhost /]# firewall-cmd --add-icmp-block=echo-request --permanent   #禁止ping

success
[root@localhost /]# firewall-cmd --zone=external --add-icmp-block=echo-request --permanent           
#配置external区域禁止ping
success

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...