Linux ubuntu firewalld 的原理&作用以及如何使用
在这篇文章中,我们将详细介绍Linux系统中的firewalld,它是一款强大的防火墙管理工具。我们将介绍firewalld的基本概念和作用,并通过实例演示如何使用它来保护您的系统。

一、什么是firewalld?
firewalld(防火墙守护程序)是Linux发行版中一款用于管理网络防火墙的动态守护程序。它提供了一个命令行和图形用户界面,用于配置和管理系统的防火墙规则。firewalld使用iptables、ip6tables、ebtables和nftables作为后端来处理网络数据包。它允许您在不需要重启防火墙或停止网络服务的情况下,实时更改防火墙规则。

二、firewalld的作用
firewalld的主要作用是保护您的系统免受未经授权的访问和攻击。它有助于防止黑客利用系统中的安全漏洞,以及限制对特定网络服务的访问。使用firewalld,您可以:
1. 配置和管理输入/输出网络流量的允许和拒绝规则。
2. 划分网络区域和服务,为每个区域和服务定义不同的访问权限。
3. 对特定的IP地址、子网或端口应用防火墙规则。
4. 实时更改防火墙规则,而无需重启防火墙或停止网络服务。
5. 使用图形用户界面或命令行工具管理防火墙规则。
三、安装与启动firewalld
在许多Linux发行版中,firewalld已经默认安装了。如果你的系统中没有firewalld,可以使用下面的命令进行安装:
在基于Debian的发行版(如Ubuntu)中:
apt-get install firewalld
在基于RHEL的发行版(如CentOS)中:
yum install firewalld
3.1安装完成后,启动firewalld并将其设置为开机启动:
systemctl start firewalld 默认一般都是开启的

systemctl enable firewalld

四、firewalld基本概念
在使用firewalld之前,我们需要了解一些基本概念:

- Zone(区域):firewalld通过区域来划分网络环境。每个区域都有一组预定义的规则,用于处理与该区域相关的网络流量。firewalld默认提供了多个预定义区域,如public、external、internal等。
- Service(服务):服务是一组与特定网络应用相关的端口和协议。在firewalld中,可以为每个服务定义不同的访问权限。例如,SSH、HTTP和HTTPS服务分别对应22、80和443端口。
- Interface(接口):网络接口是连接计算机和其他网络设备的物理或虚拟设备。在firewalld中,每个接口都分配给一个区域,用于处理经由该接口的网络流量。
- Source(源):firewalld可以针对特定的IP地址或子网限制网络访问。源可以是单个IP地址、CIDR表示的子网或者一个IP地址范围。
接下来,我们将通过实例演示如何使用firewalld。
五、firewalld实例
5.1 查看区域和服务
查看firewalld中所有可用的区域:
firewall-cmd --get-zones

查看当前活动区域:
firewall-cmd --get-active-zones

查看特定区域的详细信息:
firewall-cmd --zone=public --list-all

5.2 添加和删除服务
向特定区域添加服务:
firewall-cmd --zone=public --add-service=http

移除特定区域的服务:
firewall-cmd --zone=public --remove-service=http

请注意,上述命令所做的更改仅在当前会话中生效。若要永久保存更改,请在命令末尾添加 `--permanent` 选项。
5.3 打开和关闭端口
打开特定区域的端口:
firewall-cmd --zone=public --add-port=8080/tcp


关闭特定区域的端口:
firewall-cmd --zone=public --remove-port=8080/tcp

与添加和删除服务类似,若要永久保存更改,请在命令末尾添加 `--permanent` 选项。
5.4 更改网络接口的区域
将网络接口 ens33 从当前区域移动到 `internal` 区域:
firewall-cmd --zone=internal --change-interface=ens33

我们查看一下internal区域

5.5 添加和删除源
向特定区域添加源:
firewall-cmd --zone=public --add-source=192.168.1.0/24

从特定区域移除源:
firewall-cmd --zone=public --remove-source=192.168.1.0/24

与之前的操作类似,使用 `--permanent` 选项可永久保存更改。
六、总结
本文详细介绍了 Linux 中的 firewalld,包括其基本概念、作用和如何使用它来保护系统。firewalld 是一个强大且灵活的防火墙管理工具,可以帮助您有效地保护网络资源免受未经授权的访问和攻击。无论是通过命令行还是图形用户界面,都可以轻松地管理和配置防火墙规则。现在,您可以开始使用 firewalld 来保护您的 Linux 系统了。
相关推荐
-
PHP8种变量类型的详细讲解2025-02-22 00:32:24
-
php+apache 和 php+nginx的区别2025-02-22 00:21:27
-
PHP:与workerman结合实现定时任务2025-02-22 00:15:57
-
Nginx的Rewrite规则与实例2025-02-22 00:15:39
-
MySql中身份证字段的简单脱敏介绍2025-02-22 00:15:36