When do I need port forwarding on a dedicated server?
Every CheapDedicated machine has one public IPv4 address. When you run guests on that box, for example virtual machines under Proxmox VE or containers on a private bridge, they live on an internal subnet such as 10.10.0.0/24 that the internet cannot reach. Port forwarding lets you publish one service of a guest on a port of the host: connections to 203.0.113.10:2222 become connections to 10.10.0.21:22. It is also how you move a service to a new address while clients still use the old port, or relay a port to another host during a migration.
What are the moving parts?
| Piece | Where | Job |
|---|---|---|
net.ipv4.ip_forward=1 | kernel | Allow the host to route packets between interfaces |
| DNAT rule | nat table, PREROUTING | Rewrite the destination address and port |
| FORWARD accept | filter table, FORWARD | Let the rewritten packets through the host |
| MASQUERADE or SNAT | nat table, POSTROUTING | Give guests a way out, and make replies return via the host |
How do I turn on IP forwarding?
install -m 644 /dev/stdin /etc/sysctl.d/90-forward.conf <<< 'net.ipv4.ip_forward = 1' sysctl --system | grep ip_forward
Putting it in a file under /etc/sysctl.d makes it survive reboots. Proxmox VE installs usually need this too once you use a routed or NAT setup for guests.
How do I forward a port to a VM on the same box?
Assume the public interface is eno1, the guest bridge is vmbr1 on 10.10.0.1/24 and the guest is 10.10.0.21:
iptables -t nat -A PREROUTING -i eno1 -p tcp --dport 2222 -j DNAT --to-destination 10.10.0.21:22 iptables -A FORWARD -i eno1 -o vmbr1 -p tcp -d 10.10.0.21 --dport 22 -m conntrack --ctstate NEW -j ACCEPT iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT iptables -t nat -A POSTROUTING -s 10.10.0.0/24 -o eno1 -j MASQUERADE
The first rule rewrites the destination. The FORWARD rule matches on the new address and port, because by the time the packet reaches the filter table the rewrite has already happened; that is a common source of confusion. The conntrack line lets replies and follow-up packets through. The last rule gives guests outbound internet access through the host address, and because the guest uses the host as its gateway, replies to forwarded connections come back the same way.
Check the interface names with ip -br link; they vary by hardware. Find the public one with ip route get 1.1.1.1.
How do I forward a port to another server?
When the target is a different host on the internet, add source NAT as well, otherwise the target replies straight to the client and the client drops the reply:
iptables -t nat -A PREROUTING -p tcp --dport 8443 -j DNAT --to-destination 192.0.2.50:443 iptables -t nat -A POSTROUTING -p tcp -d 192.0.2.50 --dport 443 -j MASQUERADE iptables -A FORWARD -p tcp -d 192.0.2.50 --dport 443 -j ACCEPT
The target then sees the forwarding host as the client, not the real visitor. For web traffic, a reverse proxy such as nginx is usually the better tool because it can pass the real address in a header; see nginx reverse proxy in Docker. Remember that relayed traffic counts twice on the forwarding host: it arrives (free) and leaves again (counted against the monthly outbound allowance).
How do I forward a port to a different port on the same host?
Use REDIRECT, which is DNAT to the local machine:
iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
Now an app that listens on 8080 as an unprivileged user also answers on port 80. PREROUTING only sees traffic arriving from outside; connections made from the host itself go through the OUTPUT chain of the nat table, so add a matching OUTPUT rule if local tests should use port 80 as well.
Why can guests not reach the forwarded port by the public IP?
That is the hairpin problem: a guest connects to the public address, the host rewrites the destination back to a guest, and the reply skips the host. Fix it with a masquerade rule for traffic that both comes from and goes to the internal subnet:
iptables -t nat -A POSTROUTING -s 10.10.0.0/24 -d 10.10.0.21 -p tcp --dport 22 -j MASQUERADE
Or simply use internal addresses between guests, which is faster and avoids the extra NAT step.
How do I test and keep the rules?
From another machine run nc -vz 203.0.113.10 2222, and on the host watch iptables -t nat -L PREROUTING -n -v to see the packet counter of your DNAT rule climb. If the counter rises but the connection still fails, look at the FORWARD chain and the guest firewall. Save with netfilter-persistent save on Debian or Ubuntu. Docker manages its own NAT rules for published ports, so on a Docker host publish ports with -p rather than adding DNAT by hand. When you no longer need a forward, remove both the NAT and the FORWARD rule as described in Delete an iptables rule.
For a box that hosts many guests behind one address, look at S5 or D5, and see Virtualization hosts for how people lay out Proxmox on our hardware.
Machines Often Used to Host Guests Behind One IP
| Server | Processor | Cores / threads | Memory | Storage | Network | Per month | Smallest order |
|---|---|---|---|---|---|---|---|
| S5 | Intel Xeon E-2276G | 6 / 12 | 128 GB DDR4 | 2x 1 TB NVMe | 2x 10 Gbps | $88.72 | $88.72 (1 month) |
| S7 | Intel Xeon E-2288G | 8 / 16 | 128 GB DDR4 | 2x 1 TB NVMe | 2x 10 Gbps | $100.72 | $100.72 (1 month) |
| D5 | 2x Intel Xeon Gold 5315Y | 16 / 32 | 256 GB DDR4 | 2x 2 TB NVMe | 2x 25 Gbps | $305.34 | $305.34 (1 month) |
| M1 | 2x Intel Xeon Gold 5315Y | 16 / 32 | 512 GB DDR4 | 2x 4 TB NVMe | 2x 25 Gbps | $414.70 | $414.70 (1 month) |
Keep Reading
iptables basics
How iptables tables, chains and targets fit together, plus a minimal default-deny rule set for a new bare metal server.
OpenDelete an iptables rule
Remove a single iptables rule by number or by its exact spec, clear a chain safely and make sure the change survives a reboot.
OpenVirtualization hosts
Carve one physical box into many virtual machines with Proxmox VE or KVM, guests behind NAT on a single public address.
OpenProxmox VE
Proxmox VE 8 turns an x86 machine into a virtualization host with a web console for VMs and containers.
OpenFAQ
Quick Answers
No. REDIRECT delivers the packet locally, so routing is not involved. You need ip_forward only when packets leave toward another address.
It must match the rewritten destination address and port, because DNAT happens in PREROUTING before the filter table sees the packet.
Yes. Use -p udp in both the DNAT and the FORWARD rule, for example for a game server running inside a VM.
When the target uses the forwarding host as its gateway, yes. When you add MASQUERADE toward a remote target, it sees the forwarding host instead.
Outgoing bytes do. Inbound traffic is free, so a relay mainly counts the data it sends back out.