Skip to content

Instantly share code, notes, and snippets.

@tsertkov
Last active September 11, 2019 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsertkov/5470c501ef37275eddb3a15cd4b4e95a to your computer and use it in GitHub Desktop.
Save tsertkov/5470c501ef37275eddb3a15cd4b4e95a to your computer and use it in GitHub Desktop.
Forward ports through a Linux gateway with iptables
#!/usr/bin/env bash
i_in=en0
i_out=en1
dport=1234
dst=1.2.3.4
src=4.3.2.1
iptables -A FORWARD -i $i_in -o $i_out -p tcp --syn --dport $dport -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -i $i_out -o $i_in -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A PREROUTING -i ens5 -p tcp --dport $dport -j DNAT --to-destination $dst
iptables -t nat -A POSTROUTING -o ens5 -p tcp --dport $dport -d $dst -j SNAT --to-source $src
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment