UFW Tutorial MX/antiX 22 - AKA Share internet with UFW

Here is where you can post tips and tricks to share with other users of MX. Do not ask for help in this Forum.
Message
Author
Rannug
Posts: 20
Joined: Sat Oct 22, 2022 4:57 am

UFW Tutorial MX/antiX 22 - AKA Share internet with UFW

#1 Post by Rannug »

A common use case for people is using linux as a firewall and sharing an internet connection,
so here is a short tutorial on how to accomplish this with an imaginary network.

Prerequisites: a spare machine or VM with two ethernet or wireless cards.

Using your favorite editor; I'm using nano here.

1. Enable ip-forwarding
- sudo nano /etc/ufw/sysctl.conf
- change the line net.ipv4.ip_forward=0 to net.ipv4.ip_forward=1
2. Add a rule to the nat table
- sudo nano /etc/ufw/before.rules
- Add the following to the file after the *filter section
*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 192.168.1.0/8 -o eth0 -j MASQUERADE
COMMIT
3. Add the route to allow traffic between NIC's
- sudo ufw route allow in on eth1 out on eth0 from 192.168.1.0/8
4. Restart UFW
- sudo ufw disable && sudo ufw enable
5. Done! Popcorn? :lion:

Addendum:

If you need to forward ports you edit the same files, I'm just using port 80 as an example, put your localnet instead of the ?.
Put this before the POSTROUTING segment.

*nat
:PREROUTING ACCEPT [0:0]
-A PREROUTING -p tcp -i eth0 --dport 80 -j DNAT \
--to-destination 192.168.1.?:80
COMMIT

finally:
- sudo ufw route allow in on eth0 to 192.168.1.? port 80 proto tcp

Popcorn for all! :lion:

Return to “Tips & Tricks by users (not for help)”