Uncomplicated Firewall

来自 Arch Linux 中文维基
(重定向自Ufw

本文或本节需要翻译。要贡献翻译,请访问简体中文翻译团队

附注: 请完成翻译。(在 Talk:Uncomplicated Firewall# 中讨论)

来自项目主页:

Ufw(即Uncomplicated Firewall的缩写),是一个管理网络防火墙的程序。它提供了一个命令行界面,并旨在简单(uncomplicated)易用。
注意: 注意UFW可以使用iptablesnftables作为防火墙后端。此外由于nft接受iptables语法(例如在/etc/ufw/before.rules中),因此使用UFW管理规则的用户无需学习怎么对iptables或者nftables进行底层调用。

安装[编辑 | 编辑源代码]

安装软件包ufw

启动启用ufw.service使其开机自启。注意,如果iptables.service(或者其ipv6的部分)也被启用了,ufw.service将不会生效。

基本配置[编辑 | 编辑源代码]

以下是一个非常简单的配置示例,它将会默认拒绝所有连接,允许来自192.168.0.1-192.168.0.255局域网内的所有连接,允许所有目标为Deluge(一个BitTorrent客户端)的连接,并启用了来自任何地方的SSH连接数量限制:

# ufw default deny
# ufw allow from 192.168.0.0/24
# ufw allow Deluge
# ufw limit ssh

仅当第一次使用UFW时需要:

# ufw enable
注意: 请确保ufw.service已经被启用

最后,使用以下命令行确认更改已被应用:

# ufw status
Status: active
To                         Action      From
--                         ------      ----
Anywhere                   ALLOW       192.168.0.0/24
Deluge                     ALLOW       Anywhere
SSH                        LIMIT       Anywhere
注意: 状态报告仅限于用户添加的规则。在大多数情况下不会有什么问题,但最好还是了解一下内置规则是否存在。这些规则包括允许 UPNP AVAHI 和 DHCP 回复的过滤器。

使用以下命令来查看额外信息(包括默认策略):

# ufw status verbose

但这依然限制于用户指定的规则。为了查看所有设置的规则,可以使用:

# ufw show raw 

或者手册中列出的其他报告。由于这些报告也总结了流量情况,其输出有些难以阅读,为了方便阅读,你可以使用另一种检查接受流量的方法:

# iptables -S | grep ACCEPT

请注意,只要你还在使用ufw来管理iptables,就不要将后者启用

注意: 如果你在/etc/sysctl.d/*中设置了特殊的网络环境变量,请相应更新/etc/ufw/sysctl.conf中的内容(因为此配置会覆盖默认配置)。

转发策略[编辑 | 编辑源代码]

如果你需要使用 VPN (例如OpenVPNWireGuard),将在/etc/default/ufw中的DEFAULT_FORWARD_POLICY变量从"DROP"调整到"ACCEPT"以便无论用户如何设置如何都能转发所有数据包。要针对特定接口(例如 wg0)进行转发,用户可以在*filter块中添加以下行:

/etc/ufw/before.rules
# End required lines 

-A ufw-before-forward -i wg0 -j ACCEPT
-A ufw-before-forward -o wg0 -j ACCEPT

你可能还需要取消以下行的注释:

/etc/ufw/sysctl.conf
net/ipv4/ip_forward=1
net/ipv6/conf/default/forwarding=1
net/ipv6/conf/all/forwarding=1

Adding other applications[编辑 | 编辑源代码]

The PKG comes with some defaults based on the default ports of many common daemons and programs. Inspect the options by looking in the /etc/ufw/applications.d directory or by listing them in the program itself:

# ufw app list

If users are running any of the applications on a non-standard port, it is recommended to simply make /etc/ufw/applications.d/custom containing the needed data using the defaults as a guide.

警告: If users modify any of the PKG provided rule sets, these will be overwritten the first time the ufw package is updated. This is why custom app definitions need to reside in a non-PKG file as recommended above!

Example, deluge with custom tcp ports that range from 20202-20205:

[Deluge-my]
title=Deluge
description=Deluge BitTorrent client
ports=20202:20205/tcp

Should you require to define both tcp and udp ports for the same application, simply separate them with a pipe as shown: this app opens tcp ports 10000-10002 and udp port 10003:

ports=10000:10002/tcp|10003/udp

One can also use a comma to define ports if a range is not desired. This example opens tcp ports 10000-10002 (inclusive) and udp ports 10003 and 10009

ports=10000:10002/tcp|10003,10009/udp

Deleting applications[编辑 | 编辑源代码]

Drawing on the Deluge/Deluge-my example above, the following will remove the standard Deluge rules and replace them with the Deluge-my rules from the above example:

# ufw delete allow Deluge
# ufw allow Deluge-my

Query the result via the status command:

# ufw status
Status: active
To                         Action      From
--                         ------      ----
Anywhere                   ALLOW       192.168.0.0/24
SSH                        ALLOW       Anywhere
Deluge-my                  ALLOW       Anywhere

Black listing IP addresses[编辑 | 编辑源代码]

It might be desirable to add ip addresses to a blacklist which is easily achieved simply by editing /etc/ufw/before.rules and inserting an iptables DROP line at the bottom of the file right above the "COMMIT" word.

/etc/ufw/before.rules
...
## blacklist section
# block just 199.115.117.99
-A ufw-before-input -s 199.115.117.99 -j DROP
# block 184.105.*.*
-A ufw-before-input -s 184.105.0.0/16 -j DROP

# don't delete the 'COMMIT' line or these rules won't be processed
COMMIT

Rate limiting with ufw[编辑 | 编辑源代码]

ufw has the ability to deny connections from an IP address that has attempted to initiate 6 or more connections in the last 30 seconds. Users should consider using this option for services such as SSH.

Using the above basic configuration, to enable rate limiting we would simply replace the allow parameter with the limit parameter. The new rule will then replace the previous.

# ufw limit SSH
Rule updated
# ufw status
Status: active
To                         Action      From
--                         ------      ----
Anywhere                   ALLOW       192.168.0.0/24
SSH                        LIMIT       Anywhere
Deluge-my                  ALLOW       Anywhere

用户规则[编辑 | 编辑源代码]

所有的用户规则都储存在 etc/ufw/user.rulesetc/ufw/user6.rules 中,分别用于 IPv4 与 IPv6。

技巧和窍门[编辑 | 编辑源代码]

禁用远程ping[编辑 | 编辑源代码]

在以下文件中将 ACCEPT 改为 DROP :

/etc/ufw/before.rules
# ok icmp codes
...
-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT

如果你正在使用 IPv6, 请一同更改 /etc/ufw/before6.rules 中的规则。

关闭UFW日志[编辑 | 编辑源代码]

关闭UFW日志可以让UFW停止填充内核(dmesg)和消息日志:

# ufw logging off

UFW 与 Docker[编辑 | 编辑源代码]

Docke在默认状态下会编写iptables规则并忽视ufw规则,这可能会造成一些安全隐患。一种解决方案是使用ufw-docker脚本

提示:你可以从AUR中安装ufw-dockerAUR来使用。运行ufw-docker install来自动修复iptables规则。这个脚本也能帮你管理Docker相关的ufw规则,详细参见ufw-docker help

GUI前端[编辑 | 编辑源代码]

如果你使用KDE,你可以直接在系统设置 > 防火墙 对ufw进行管理。

Gufw[编辑 | 编辑源代码]

gufw 是一个为 Ufw 提供的 GTK 前端,旨在使 Linux 防火墙的管理尽可能简单易用。它具有常见端口和 p2p 应用程序的预设设置。其依赖于软件包python, ufw,以及GTK支持。

See also[编辑 | 编辑源代码]