Iptables NAT
Tuesday, May 24th, 2011Here is a quick and dirty iptables based NAT solution for linux servers:
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 --source 192.168.0.1 -j ACCEPT iptables -A FORWARD -i eth1 -o eth0 -j REJECT
Here we will provide internet access to the host 192.168.0.1 behind eth1 over our own internet connection on eth0.
The ip of eth0 can be dynamic. The host 192.168.0.1 will be using the ip of eth0 on the internet.
We only accept traffic from the ip 192.168.0.1, the rest is rejected.
Oh, and don’t forget to enable ipv4 forwarding in the kernel!
Add:
net.ipv4.ip_forward = 1
to /etc/sysctl.conf and run:
sysctl -p /etc/sysctl.conf
Or just run:
echo 1 > /proc/sys/net/ipv4/ip_forward
Your Milage May Vary…