防火墙是一种网络安全系统,它根据一组预定义的安全规则来监控和控制进出网络的流量。本页面将介绍简单防火墙(UFW),这是一种防火墙工具,通过使用iptables.
来简化 Linux 系统上的防火墙配置。此外,还有一些适用于 UFW 的图形用户界面(GUI)前端,例如 GUFW,但ufw commands are typically run on the command line. This page provides an overview of, and basic instructions for, the UFW command-line interface.
简单防火墙(UFW)
网络流量按端口号进行组织,端口号是用于将数据定向到特定应用程序的数字标签。防火墙通过打开或关闭特定端口来控制流量,从而允许或阻止某些类型的通信。
在 Linux 系统中,网络流量会经过内核的防火墙子系统进行过滤,该子系统称为netfilter。Netfilter 实现了数据包过滤;它根据源 IP 地址、目标 IP 地址、端口号、协议以及数据包的传输方向等因素,决定是否允许数据包通过系统。
系统管理员使用iptables来配置 netfilter,这是用于定义数据包过滤规则的传统命令行工具。然而,iptables can be complex. UFW(简单防火墙)是 Ubuntu 所使用的防火墙工具,能够简化防火墙的配置与管理。它充当iptables, allowing system administrators to set firewall rules without dealing with the complexity of raw iptables commands.
UFW 命令
的封装层。UFW 是一个用于定义防火墙访问规则的命令行程序。UFW 命令的一般语法为:sudo ufw [--dry-run] <command> [rule].
-
您唯一
ufwcommands require superuser privileges, and so must be preceded withsudo. -
您还可以选择添加
--dry-runto simulate theufwcommand without making actual changes. -
该
<command>is the main action that you want to perform, such asallow. -
根据具体命令,您还可以选择指定一个
ruleto apply to the command, such asallow 22.
例如,sudo ufw --dry-run allow 22 shows you the predicted outcome of allowing traffic on port 22, including the entire set of rules if the change were made.
安装并启用 UFW
要使用 UFW,您必须先在树莓派上安装它。安装完成后,UFW 已存在但处于禁用状态,这意味着它目前不会阻止任何流量。这样可以让您在不被锁定在外的情况下配置默认的访问规则。如果您通过 SSH(或其他远程方式)连接到树莓派设备,这一点尤为重要;为了避免被锁定,您必须在启用 UFW 之前先允许远程访问。
如果您通过 SSH 连接,建议的操作流程如下:
| 步骤 | 说明 | 命令 |
|---|---|---|
1 |
更新您的软件包列表。 |
|
2 |
安装 UFW。在启用 UFW 之前,输出会显示 UFW 处于未激活状态。 |
|
3 |
通过检查状态来验证安装是否成功。有关此命令的更多信息,请参阅规则管理. |
|
4 |
您可以(建议)配置默认策略,例如拒绝所有入站流量。默认策略会全局生效。 |
|
5 |
为了保持远程访问能力,在启用 UFW 之前允许 SSH 流量,可以通过指定服务名称(ssh)或默认端口(22/tcp)来实现。 |
|
6 |
可选地,如果您正在运行 Web 服务器,可以通过指定服务名称(http 和 https)或其默认端口(分别为 80/tcp 和 443/tcp),允许 HTTP 和 HTTPS 流量。 |
|
7 |
启用 UFW 以激活防火墙,并将其配置为在系统启动时自动启用。 |
|
8 |
验证防火墙的状态并查看当前生效的规则。有关此命令的更多信息,请参阅规则管理. |
|
您可以随时禁用 UFW,这样它就不会在系统启动时自动启用。使用以下命令可禁用 UFW 防火墙:
$ sudo ufw disable
使用允许和拒绝规则
本节列出了通过指定服务名称和端口来允许或拒绝流量的基本命令。一般而言,ufw syntax for allow and deny commands are as follows:
-
允许访问特定端口或服务:
sudo ufw allow [rule] -
拒绝访问特定端口或服务:
sudo ufw deny [rule]
下表提供了一些具体示例。
| 命令 | 说明 |
|---|---|
|
允许通过防火墙访问端口https://www.raspberrypi.org/blog/how-to-build-databases-using-python-and-text-files-hello-world-9/#comments。这将对该端口开放所有协议(TCP 和 UDP)。 |
|
仅允许TCP 协议在端口https://www.raspberrypi.org/blog/how-to-build-databases-using-python-and-text-files-hello-world-9/#comments. |
|
上。允许SSH 连接到您的 Raspberry Pi 设备。服务,而无需指定端口(SSH 通常为端口 |
|
拒绝通过防火墙访问端口https://www.raspberrypi.org/blog/how-to-build-databases-using-python-and-text-files-hello-world-9/#comments。这将关闭该端口的所有协议(TCP 和 UDP)。 |
|
拒绝TCP 协议在端口https://www.raspberrypi.org/blog/how-to-build-databases-using-python-and-text-files-hello-world-9/#comments. |
|
拒绝SSH 连接到您的 Raspberry Pi 设备。服务,而无需指定端口(SSH 通常为端口 |
高级 UFW 规则
UFW 不仅能执行基本的允许和拒绝规则,还可以:
-
允许或阻止特定 IP 地址来自特定端口。
-
Specify traffic direction (incoming and outgoing).
-
Rate limit connection attempts to help protect against brute-force or DDoS attacks.
-
Specify network interfaces以将规则专门应用于有线或无线网络。
有关这些选项的更多信息,请运行以下命令:man ufw. This displays the UFW manual page in your terminal, which includes documentation on UFW syntax, options, rule formatting, and examples.
允许或阻止特定 IP 地址
您可以允许或拒绝来自特定 IP 地址对特定端口的访问。这在您希望阻止可疑主机或仅允许受信任主机时非常有用。例如,以下命令将拒绝 IP 地址 192.168.2.1 访问端口 30。
$ sudo ufw deny from 192.168.2.1 to any port 30
Specify traffic direction
为了控制您的系统如何发送或接收网络流量,例如阻止针对系统的威胁(入站)或保护系统资源(出站),您可以阻止一个或两个方向的流量。具体来说,您可以添加out (for outgoing traffic) or in (for incoming traffic) to one of the following commands:
-
allow, which allows the traffic to flow in the specified direction. -
deny, which silently drops the traffic flowing in the specified direction. -
reject, which actively refuses the traffic flowing in the specified direction.
例如,以下命令会阻止 SMTP 端口(通常是 25/TCP)上的所有出站流量:
$ sudo ufw reject out smtp
Rate limit connection attempts
为防范暴力破解和DDoS攻击,limit command temporarily blocks any IP address that attempts to make too many connections in a short period of time (approximately 6 or more times within 30 seconds). For example, the following command watches how often an IP address attempts to open a new connection to SSH (usually port 22/tcp) and applies the rate limit.
$ sudo ufw limit ssh/tcp
Specify network interfaces
您可以设置仅适用于特定网络连接的防火墙规则(eth0 for wired networks or wlan0 for Wi-Fi). For example, the following command allows incoming TCP traffic on port 80 only on the eth0 network interface:
$ sudo ufw allow in on eth0 to any port 80 proto tcp
规则管理
UFW 按照规则的先后顺序进行处理,因此靠前的规则优先于靠后的规则。下表列出了一些实用的命令,可帮助您检查当前的防火墙配置,并在必要时删除规则。
| 命令 | 说明 |
|---|---|
|
显示 UFW 是否处于活动状态,并列出防火墙的所有当前设置(哪些端口和服务被允许或被拒绝)。 |
|
显示其他详细信息,例如默认策略和日志记录设置。 |
|
仅显示用户添加的规则(不包括内部规则和系统规则)。如果您需要审查更改或比较不同系统之间的配置,此功能将非常有用。 |
|
显示规则及其分配的索引号。这在您需要删除特定规则时非常有用。 |
|
根据指定的索引号删除您选择的规则。随后,UFW 会重新对剩余的条目进行编号。您可以再次运行 |