Skip to content

Instantly share code, notes, and snippets.

@rollwagen
Last active May 24, 2021 10:06
Show Gist options
  • Save rollwagen/160b3e44b1c23091e3aa48a4e4a8f660 to your computer and use it in GitHub Desktop.
Save rollwagen/160b3e44b1c23091e3aa48a4e4a8f660 to your computer and use it in GitHub Desktop.
Port forwarding (linux, mac, ssh)

Port forwarding (linux, mac, ssh)

Examples and links on how to do port forwarding on

  • Linux using iptables
  • MacOS with pfctl (control the packet filter (PF) and NAT device)
  • with ssh

Additional topic/link:

iptables (linux)

sudo iptables -A FORWARD -i wlan0 -o eth0 -p tcp --syn --dport 3389 -m conntrack --ctstate NEW -j ACCEPT

sudo iptables -A FORWARD -i wlan0 -o eth0 -p tcp --syn --dport 3389 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

sudo iptables -A FORWARD -i eth0 -o wlan0 -p tcp --syn --dport 3389 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

#sudo iptables -P FORWARD DROP
sudo iptables -t nat -A PREROUTING -p tcp -i wlan0 --dport 3389  -j DNAT --to-destination 192.168.1.10:3389  
sudo iptables -t nat -A POSTROUTING -p tcp -o eth0 --dport 3389 -d 192.168.1.10


sudo iptables -t nat -I PREROUTING -p tcp -i wlan0 --dport 33389  -j DNAT --to-destination 192.168.1.10:33389  
sudo iptables -t nat -I POSTROUTING -p tcp -o eth0 --dport 33389 -d 192.168.1.10 -j SNAT --to-source 192.168.2.77

pfctl (mac)

echo "
rdr pass inet proto tcp from any to any port 33389 -> 192.168.152.130 port 3389
rdr pass inet proto udp from any to any port 33389 -> 192.168.152.130 port 3389
" | sudo pfctl -ef -

echo "
rdr pass inet proto tcp from any to any port 33389 -> 127.0.0.1 port 3389
rdr pass inet proto udp from any to any port 33389 -> 127.0.0.1 port 3389
" | sudo pfctl -ef -

# sudo pfctl -F all -f /etc/pf.conf
# sudo pfctl -s nat

SSH

ssh -L 0.0.0.0:3389:192.168.152.130:3389 [email protected]
ssh -L 0.0.0.0:3389:192.168.1.10:3389 [email protected]
ssh -L 0.0.0.0:3389:127.0.0.1:3389 [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment