Ubuntu – Port forwarding + shared connection with Ubuntu

crossover-cableethernetport-forwardingUbuntuubuntu-9.10

Because my wireless router's ethernet ports are defective, I set up a shared wireless connection from my laptop (which has wifi) to my eMac (which does not) via a crossover ethernet cable. The laptop is behind a router as 192.168.1.131, and the eMac is behind the laptop as 10.42.43.1 .

The laptop is running Ubuntu 9.10 (Karmic). I achieved the shared connection through NetworkManager Applet. I right-clicked on the network icon at the topright, went to Edit Connections, selected the Wired connection named "Auto eth0", clicked "Edit…", went to the "IPv4 Settings" tab, and selected the Method "Shared to other computers". The eMac can now access the Internet.

Now I want to enable port forwarding. There's a game I want to play that needs port 6112 forwarded (both TCP and UDP) in order to host games. I set up the router to enable port forwarding for 192.168.1.131 (the laptop), but port forwarding still isn't available on the eMac.

I suppose I need to pretend my laptop is a router and configure port forwarding on it, indicating that incoming connections to the laptop (192.168.1.131) should be forwarded to the eMac on the shared connection (10.42.43.1 ). Thus, packets coming into the router on port 6112 would be redirected to the laptop (by the router), then to the eMac (by the laptop).

My question is, how would I do that on Ubuntu (in light of NetworkManager's presence)? Also, if I can't get this to work, does anyone mind hosting a comp stomp? 😀

Best Answer

Don't know anything about Ubuntu config utils but here's the classic way:

echo 1 > /proc/sys/net/ipv4/ip_forward # enable forwarding
iptables -I FORWARD -p tcp -d 192.168.1.131 --dport 6112 -j ACCEPT #just to be sure firewall doesn't block
iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 6112 -j DNAT --to 10.42.43.1 #actual forward
#same for UDP
iptables -I FORWARD -p udp -d 192.168.1.131 --dport 6112 -j ACCEPT
iptables -t nat -A PREROUTING -p udp -i eth0 --dport 6112 -j DNAT --to 10.42.43.1 

You have to change -i eth0 to your wlan connection name. Generally

iptables -t nat -A PREROUTING [-p protocol] --dport [external port on router] -i ${WAN} -j DNAT --to [ip/port to forward to]

For more check Frozentux iptables guide.

Related Question