Linux firewalld

防火墙是部署在网络关键节点(如内外网之间、路由器边界或主机内部)的安全控制设备,作用是根据设定规则过滤网络数据,防止未授权访问和网络攻击

特点:
可以是硬件,也可以是软件。
作用:
控制网络访问权限
根据规则允许或拒绝网络流量
实现对特定端口、协议、IP的过滤
部署:
路由器之后,交换机之前(常见)
路由器和公网之间(边界防护)
主机内部(如 Linux 的 firewalld/iptables)

报文的流动顺序

从内网访问公网

主机 --> 交换机 --> 防火墙(防火墙可能与路由器集成在一个设备上)--> 路由器 --> 公网

从公网访问内网

公网 --> 路由器(可能会进行net转换) --> 防火墙 --> 交换机 --> 主机

防火墙拦截流量的机制

target指定的四种行为

  1. REJECT
    拒绝所有传⼊的数据包,但特定规则允许的数据包除外。当 firewalld 拒绝数据包时,源机器会发出 有关拒绝的信息。
  2. default
    与 REJECT 的⾏为类似,但在某些情况下有特殊含义。详情请查看 firewall-cmd(1) man page 中 的 Options to Adapt and Query Zones and Policies 部分。
  3. ACCECPT
    接受除特定规则不允许的所有传⼊的数据包。
  4. DROP
    除⾮由特定规则允许,丢弃所有传⼊数据包。当 firewalld 丢弃数据包时,源机器不知道数据包丢弃的 信息。

概念

  1. Zone(区域) :预设了一组默认规则,用于将不同的网络接口或来源IP分类(如 public、internal、dmz、trusted等)。
  2. Services(服务) :一组预定义的端口和协议(如 http 服务=80/tcp)。
  3. Ports(端口):单独定义允许的端口,如 8080/tcp。
  4. Protocols(协议) :如 icmp(ping)、tcp、udp。
  5. Interfaces(网卡接口):那块网卡属于那个zone。
  6. Sources(来源地址):指定哪些 IP 地址被允许或限制访问。
  7. Rich Rules(高级规则):可设置更复杂的条件,如“某IP只允许访问某端口”。
  8. Masquerade(伪装):类似于 NAT,让内部网络通过防火墙访问外部。
  9. Forwarding(转发):控制是否允许数据包从一块网卡转发到另一块网卡。
  10. icmp-block-inversion(反转): 反转 icmp-blocks阻止的报文。
  11. icmp-block(阻止icmp报文): 用于阻止有关icmp的报文。
  12. target(方案): 指定这个区域使用的方案

需要注意的是允许与否,与这个区域的行为有关

Zone

Zone中预设了2、3、··· 12这些东西,通过interfaces指定一个网卡,就可以将这些预设指定到某个接口中。在这个接口中进行这个区域预设的流量进出规则。

  1. block
    任何传⼊的⽹络连接都会被拒绝,并报 prohibited 消息 。只有从系统启动的⽹络连接才能进⾏。
  2. internal
    当您主要信任⽹络中的其他计算机时,供内部⽹络使⽤。只接受所选的⼊站连接。
  3. dmz
    对于您的⾮企业化区⾥的计算机来说,这些计算机可以被公开访问,且有限访问您的内部⽹络。只接受所选的⼊ 站连接。
  4. trusted
    所有⽹络连接都被接受。
  5. work
    可⽤于您主要信任⽹络中其他计算机的⼯作。只接受所选的⼊站连接。
  6. public
    可⽤于您不信任⽹络中其他计算机的公共区域。只接受所选的⼊站连接。
  7. home
    ⽤于家⽤,因为您可以信任其他计算机。只接受所选的⼊站连接。
  8. drop
    所有传⼊的⽹络数据包都会丢失,没有任何通知。只有外发⽹络连接也是可⾏的。

当然以上这些都不是最重要到,它只是自动预设了一些配置,我们可以完全自己配置我们想要的防火墙,这才是重点。

防火墙机制

防火墙,根据四种默认行为配合相应的机制的值(如 icmp-blocks、ports和services等等)来控制流量的进与出。

命令

firewall-cmd 命令是在命令行操作防火墙的强大工具。

常用选项

–add- 和 --remove-

--add-service=--remove-service=
为services配置,添加一个service

[root@localhost xwang]# firewall-cmd --list-services
cockpit dhcpv6-client mdns samba-client ssh
[root@localhost xwang]# firewall-cmd --add-service=http --permanent
success
[root@localhost xwang]# firewall-cmd --list-services
cockpit dhcpv6-client mdns samba-client ssh
[root@localhost xwang]# firewall-cmd --reload
success
[root@localhost xwang]# firewall-cmd --list-services
cockpit dhcpv6-client http mdns samba-client ssh
[root@localhost xwang]# firewall-cmd --zone=home --remove-service=http --permanent
success
[root@localhost xwang]# firewall-cmd --reload
success
[root@localhost xwang]# firewall-cmd --list-services 
cockpit dhcpv6-client http ssh

--add-icmp-block-inversion--remove-icmp-block-inversion
add将icmp-block-inversion的值设为yes,remove将icmp-block-inversion的值设为no

[root@localhost xwang]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client http ssh
  ports: 9091/tcp
  protocols: icmp
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
[root@localhost xwang]# firewall-cmd --add-icmp-block-inversion 
success
[root@localhost xwang]# firewall-cmd --list-all
fipublic (active)
  target: default
  icmp-block-inversion: yes
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client http ssh
  ports: 9091/tcp
  protocols: icmp
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
[root@localhost xwang]# firewall-cmd --remove-icmp-block-inversion 
success
[root@localhost xwang]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client http ssh
  ports: 9091/tcp
  protocols: icmp
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

其他的add和remove就不详细举例了

  1. --add-port=--remove-port=
  2. --add-icmp-block=--remove-icmp-block=
  3. --add-protocol=--remove-procotol=
其他常用的

--reload
重新加载firewalld配置,通常是firewalld做出更改时,需要重新加载更新配置
--permanent
永久修改firewalld配置,即使设备重启,也不会重置修改。
--set-target=
指定这个区域的策略,需要结合--permanent 使用.
--get-target
获取某个区域当前的策略,需要结合--permanent 使用.

 修改target的值
 [root@localhost xwang]# firewall-cmd --permanent --get-target
default
[root@localhost xwang]# firewall-cmd --permanent --set-target=REJECT
success
[root@localhost xwang]# firewall-cmd --permanent --get-target
REJECT

--zone
指定操作的区域

[root@localhost ~]# firewall-cmd --zone=home --list-all      ;显示区域home中的所有信息
home
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: cockpit dhcpv6-client mdns samba-client ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

--list-all
列出指定区域的所有配置信息,如果不指定区域则会显示默认区域的信息

[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client http ssh
  ports: 9091/tcp
  protocols: icmp
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

--list-all-zones
显示所有区域的配置信息

firewall-cmd --list-all-zone
······
······
testZone1
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

testzone
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: 
  ports: 
  protocols: 
  forward: no
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
······
······

--list-[配置]
仅展示指定的配置

[root@localhost xwang]# firewall-cmd --list-services
cockpit dhcpv6-client http mdns samba-client ssh
[root@localhost xwang]# firewall-cmd --list-interfaces 
ens160

--get-default-zone
打印默认区域

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

--set-default-zone=
指定默认区域

[root@localhost xwang]# firewall-cmd --get-default-zone 
public
[root@localhost xwang]# firewall-cmd --set-default-zone=home
success
[root@localhost xwang]# firewall-cmd --get-default-zone 
home
//指定将默认区域从public改变为home

创建分区 和 服务

创建分区

  1. 创建的新分区所在的目录位置在 /etc/firewalld/zones
  2. 创建的新服务所在的目录位置在 /etc/firewalld/services

创建方法:
1. 将目录/usr/lib/firewalld/zones 中的模板文件cp 到上面的指定目录中,你葫芦画瓢篇,创建自己的新分区
2. 将目录/usr/lib/firewalld/services 中的模板xml 文件cp 到上面的指定目录中,比葫芦话琵琶,创建自己的新服务。

案例

firewalld 的一些案例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值