Today I learned: How to forward a port using ufw

For forwarding a port using ufw it’s necessary to operate on iptables rules defined in /etc/ufw/before.rules

Add the following rules to before.rules

*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 11.11.11.11:80
-A POSTROUTING ! -o lo -j MASQUERADE
# don't delete the 'COMMIT' line or these rules won't be processed
COMMIT

-A POSTROUTING ! -o lo -j MASQUERADE allows the traffic to be discerned as though not originating from a nat. ! -o lo prevents the lo interface to be masqueraded and break DNS resolving.

Leave a comment