Port Forwarding From a Shared Connection

internet-sharingNetwork

Say I have a local network on which my computer's IP address is 192.168.0.2, and I've setup my router to forward port 80 to that IP.

I also have another device connected through my computer via internet sharing, and it has an IP of 192.168.1.2. Is it possible to forward port 80 from my computer to this other device, if so, how?

I should mention that my router isn't capable of port forwarding to 192.168.1.X, even though it otherwise happily routes the traffic from devices on that network.

Best Answer

valid until 10.7, natd is deprecated in 10.8 and above but should still work

A solution found here:

First use "ifconfig" to see how the wireless and ethernet interfaces are called. Probably the wireless interface is "en1" and the (shared) ethernet interface is "bridge0".

IP-settings of the "other device":

IP address: 192.168.1.x (x ≠ 0,1,2,other used last octets in the 192.168.1- network),255)
IP netmask: 255.255.255.0
IP gateway: 192.168.1.2
IP DNS server: 192.168.1.2

Mac OS X has no direct GUI option to configure port forwarding. However, you can create a shell script as follows (open terminal and create a script called osx_fw.sh):

#!/bin/bash
# http port forwarding with mac os x
killall -9 natd
sleep 5
# The following will forward 80 port to desktop computer located at 192.168.1.x
# 192.168.0.2 => airport IP
# 192.168.1.x => Desktop client ip
#  natd provides a Network Address Translation facility for use with divert(4) sockets under FreeBSD.
# -------------------------------------------------------------------------------------------------
/usr/sbin/natd -alias_address 192.168.0.2 -interface en1 -use_sockets -same_ports -unregistered_only -dynamic -clamp_mss -enable_natportmap -natportmap_interface bridge0 -redirect_port tcp 192.168.1.x:80 80 -l

Make the script executable:

chmod +x osx_fw.sh

Simply run this script whenever you need to forward ports:

sudo ./osx_fw.sh

Depending on your system you probably have to setup or modify some firewall rules.

since 10.8

check this answer