Skip to content

Instantly share code, notes, and snippets.

@raspi
Last active July 20, 2022 17:13
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 raspi/0460bf8bd503af7d76d93f1cb4d23987 to your computer and use it in GitHub Desktop.
Save raspi/0460bf8bd503af7d76d93f1cb4d23987 to your computer and use it in GitHub Desktop.
Quick NAT with GNU/Linux iptables firewall

Quick NAT with GNU/Linux iptables firewall.

First set static IP for LAN adapter:

ip address add 192.168.99.1/24 dev LANeth0

Enable packet forwarding:

echo 1 > /proc/sys/net/ipv4/ip_forward

Set NAT firewall rules:

iptables -t nat -A POSTROUTING -o WANeth0 -j MASQUERADE
iptables -A FORWARD -i WANeth0 -o LANeth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i LANeth0 -o WANeth0 -j ACCEPT

Now this machine can be used as gateway. On other machine(s):

ip route add default via 192.168.99.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment