如何配置UFW以允许IP转发?

我在家用服务器上安装了UFW、OpenVPN和Virtualbox。我已经设置了一个仅主机网络(vboxnet0)来供虚拟机客户端使用,IP范围为10.0.1.0,并在OpenVPN连接的另一端配置了另一个IP范围为10.0.0.0。
主机已配置IP转发,所以当UFW被禁用时,它们可以无问题地相互通信。然而,我想运行UFW,因为这个主机将可以通过Web访问,并且我希望有一些访问控制。
我应该如何配置UFW以允许这种类型的流量?
我尝试了各种组合:ufw allow allow in|out on vboxnet0|tun0,但都没有成功。
我的UFW规则如下:
root@gimli:~# ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
Anywhere                   ALLOW       10.0.0.0/16
Anywhere on vboxnet0       ALLOW       Anywhere
Anywhere on tun0           ALLOW       Anywhere

Anywhere                   ALLOW OUT   Anywhere on vboxnet0
Anywhere                   ALLOW OUT   Anywhere on tun0

任何帮助都将不胜感激。

你能更新这里的最佳答案吗?@Michal Sylvester有一个非常好的答案,更适用于当前状态,使用route前缀来传递参数给ufw - Anders
4个回答

现在可以了 - 来自ufw手册页:

Rules for traffic not destined for the host itself but instead for traffic that should be routed/forwarded through the firewall should specify the route keyword before the rule (routing rules differ signifi‐ cantly from PF syntax and instead take into account netfilter FORWARD chain conventions). For example:

     ufw route allow in on eth1 out on eth2

This will allow all traffic routed to eth2 and coming in on eth1 to traverse the firewall.

     ufw route allow in on eth0 out on eth1 to 12.34.45.67 port 80 proto tcp

This rule allows any packets coming in on eth0 to traverse the firewall out on eth1 to tcp port 80 on 12.34.45.67.

In addition to routing rules and policy, you must also setup IP forwarding. This may be done by setting the following in /etc/ufw/sysctl.conf:

     net/ipv4/ip_forward=1
     net/ipv6/conf/default/forwarding=1
     net/ipv6/conf/all/forwarding=1

then restarting the firewall:

     ufw disable
     ufw enable

Be aware that setting kernel tunables is operating system specific and ufw sysctl settings may be overridden. See the sysctl manual page for details.


4对于那些希望在OpenVPN服务器上允许OpenVPN客户端之间进行TCP流量通信的人来说,这个方法是可行的。例如:ufw route allow in on tun0 out on tun0 - logion
1记录一下,ufw route 命令自版本0.34起就存在了。 - Joril
4这应该是新接受的答案。 - creekorful
这是否启用了所有IP转发(从而绕过所有这些步骤:https://www.ducea.com/2006/08/01/how-to-enable-ip-forwarding-in-linux/),还是仅仅使`ufw`能够友好地进行IP转发...或者其他什么? - Johnny Utahh
这是我见过的关于ufw和路由的最好的文章之一。只有当你了解ufw时,才需要这些内容。 - Anders

我搞定了。
编辑 `/etc/default/ufw` 文件,并将 `DEFAULT_FORWARD_POLICY` 设置为 `ACCEPT`。
DEFAULT_FORWARD_POLICY="ACCEPT"

18有没有仅允许它转发特定端口的方式,而不是设置为接受所有内容? - Marcus Downing
1我猜你需要在编辑文件后重新启动 ufw:service ufw restart - Minh Danh
4可以通过命令行界面(CLI)使用sudo ufw default allow routed来更改默认策略。 - RomanK

这个ufw命令对我来说很好用: sudo ufw default allow FORWARD
为了确保更改生效:sudo service ufw restart

这会导致一个"无效的语法"错误。文档中说:"DIRECTION可以是incoming、outgoing或routed之一"。 - ColinM
@ColinM 这个方法对我在Xubuntu 16.04.5 LTS上有效。 - baptx
FORWARD在Ubuntu上的作用类似于routed的别名。 - patricktokeeffe

如果您在/etc/default/ufw中将DEFAULT_FORWARD_POLICY设置为ACCEPT,防火墙将转发所有数据包,而不考虑用户界面的设置。
我认为用户界面只适用于简单的进出过滤。要进行转发,您需要在/etc/ufw/before.rules中添加iptables规则,就像这样:
-A ufw-before-forward -i eth1 -p tcp -d 192.168.1.11 --dport 22 -j ACCEPT

你可能已经有一个规则,允许从内部到外部的连接,并且另一个规则允许来自相关和已建立的TCP会话的数据包返回。
我不是iptables专家,花了很长时间才弄明白这个(使用ip6tables,但应该类似)。也许在你的情况下,这并不是全部需要的。
最好的问候。