nat_on_linux_with_iptables

NAT on linux with iptables

eth0 is our external network and eth1 is our intenral one:

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

to make all this permanent change the following parameter in /etc/sysctl.conf to 1:

net.ipv4.ip_forward = 1

then save the current iptables permanetnly. in ubuntu you do:

apt install iptables-persistent

choose yes at the end to save the current config and you're done! if you change other iptables rules, you can run the following command to update the saved settings:

iptables-save > /etc/iptables/rules.v4
  • nat_on_linux_with_iptables.txt
  • Last modified: 27.11.2023 18:04
  • by Pascal Suter