Here’s how you can configure ufw
to forward port 80 (HTTP) and 443 (HTTPS) to an internal server hosted on the local area network (LAN):
- First, make sure that
ufw
is installed:sudo apt-get install ufw
- Enable the firewall:
sudo ufw enable
- Allow incoming traffic on ports 80 and 443:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
- Configure port forwarding using the following commands:
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 192.168.0.10:80
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j DNAT --to-destination 192.168.0.10:443
Replace eth0
with the name of your Internet-facing network interface, and replace 192.168.0.10
with the IP address of the internal server.
- Save the firewall rules:
sudo sh -c "iptables-save > /etc/iptables.rules"
- Configure the firewall rules to persist after reboot:
sudo nano /etc/network/if-pre-up.d/firewall
Add the following contents to the file:
iptables-restore < /etc/iptables.rules
- Make the file executable:
sudo chmod +x /etc/network/if-pre-up.d/firewall
After these steps, incoming traffic on ports 80 and 443 will be forwarded to the internal server at IP address 192.168.0.10
.