Linux系统之firewalld防火墙基础配置

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

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

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

  1. 若源地址关联到特定的区域,则执行该区域所制定的规则;
  2. 若源地址未关联到特定的区域,则使用传入网络接口的区域并执行该区域所制定的规则。
  3. 若网络接口未关联到特定的区域,那么就使用默认区域并执行该区域所制定的规则。

默认区域不是单独的区域,而是指向系统上定义的某个其余区域。默认状况下,默认区域是public,可是也能够更改默认区域。以上匹配规则,按照前后顺序,第一个匹配的规则胜出,和网络设备的ACL匹配规则差很少,俗称匹配即停。ssh

firewalld的相关预约义区域说明以下:tcp

Linux系统之firewalld防火墙基础配置

firewalld防火墙有两种配置模式:ide

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

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

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

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

一、防火墙相关命令使用:router

[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:时间戳请求;

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

Linux系统之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

三、防火墙服务管理相关命令及示例:

Linux系统之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
相关文章
相关标签/搜索