boup.
FR EN

Enabling IP forwarding and redirecting ports with firewalld

CheatsheetHomelabLinuxCheatsheet
On this page

Goal

The goal is to use a VPS as the entry point for the homelab network flows that must be reachable from the internet. Since the homelab connects to the VPS through WireGuard, there is no need to open any ports on the router / firewall at home.

Architecture

flowchart LR
    Internet([Internet])

    subgraph VPS["VPS (Point d'entrée Public)"]
        Firewall[firewalld]
        WG_Server[WireGuard Server]
    end

    Internet ---|TCP 80, 443| Firewall
    Firewall ---|Forward| WG_Server

    WG_Server ---|Tunnel Chiffré WireGuard UDP| WG_Client

    subgraph Home["Homelab (Réseau Privé)"]
        WG_Client[WireGuard Client]
        Services[Services Internes]
    end

    WG_Client --- Services   

Configuration steps

1. Enable IP forwarding (sysctl)

Check the current state:

bash
cat /proc/sys/net/ipv4/ip_forward

If the command returns 0, enable it in /etc/sysctl.conf:

bash
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

2. Install and configure firewalld

bash
apt install firewalld
sudo firewall-cmd --permanent --zone=public --add-forward

Edit the active zone (/etc/firewalld/zones/public.xml), adding the masquerade and the required forward-port rules, then reload:

bash
firewall-cmd --reload
sudo systemctl start firewalld

Test that the redirections work before enabling the service at boot:

bash
sudo systemctl enable firewalld

Example firewalld configuration

The homelab has no publicly routable IP address. A VPS at an external hosting provider acts as a relay: it receives incoming traffic on its public ports and forwards it, through a WireGuard tunnel, to the server at home.

firewalld config on the VPS side:

xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <service name="https"/>
  <service name="http"/>
  <port port="53478" protocol="udp"/>
  <port port="25565" protocol="tcp"/>
  
  <masquerade/>

  <forward-port port="80" protocol="tcp" to-port="80" to-addr="10.0.3.2"/>
  <forward-port port="443" protocol="tcp" to-port="443" to-addr="10.0.3.2"/>
  <forward-port port="443" protocol="udp" to-port="443" to-addr="10.0.3.2"/>
  <forward-port port="25565" protocol="tcp" to-port="25565" to-addr="10.0.3.2"/>

  <interface name="eth0"/>
  <forward/>
</zone>

WireGuard config on the VPS side (the UDP port 53478 above is the WireGuard listening port):

ini
[Interface]
PrivateKey = <>
Address = 10.0.3.1/24
ListenPort = 53478

[Peer]
PublicKey = <>
PresharedKey = <>
AllowedIPs = 10.0.3.2/32

The homelab (10.0.3.2 on the tunnel side) thus receives the traffic forwarded by the VPS as if it were directly exposed to the internet, without needing to use its router’s public IP address, or a static IP.