UFW – Port Forwarding Guide

firewalliptablesufw

I realize UFW is mainly just for enabling/disabling access on particular ports, but I'm trying to forward one port to another. I want any connection to port 1724 to be forwarded to 1723.

The file /etc/default/ufw already has DEFAULT_FORWARD_POLICY="ACCEPT". And I believe I need to uncomment net/ipv4/ip_forward=1 in /etc/ufw/sysctl.conf. Finally I need to edit /etc/ufw/before.rules to add an iptables rule.

And here is where I get lost because I can't find any documentation for this file. Where do I add the rule? And how exactly?

In particular:

  • On exactly which line do I add the rule?
  • Do I put anything before or after it?

Best Answer

Add before filter section in /etc/ufw/before.rules(top of file):

*nat
:PREROUTING ACCEPT [0:0]
-A PREROUTING -p tcp --dport 1724 -j REDIRECT --to-port 1723
COMMIT

These rules will then need to be reloaded; simply disabling and re-enabling ufw or running ufw reload is not sufficient. The simplest solution is to reboot, otherwise you can use one of the workarounds mentioned in Launchpad bug #881137.