Linux nat/iptables configuration for this setup

Space Rocker picture Space Rocker · Feb 14, 2013 · Viewed 14.9k times · Source

I have an experimental setup, of 4 linux (CentOS) machines:

enter image description here

All 4 machines are internally connected using different networks and can ping eachother directly connected interfaces. However only PC4 has access to internet.

I am trying to setup iptable rules that can allow PC1 to be able to access internet via PC4, but i have no idea how to do this.

I tried to add NAT at outgoing interface on PC2, PC3 and PC4:

iptables -t nat -F POSTROUTING
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

However, this does not work, can you give me some idea how in such a setup i can make PC1 access 10.0.0.1 network??

Answer

Satish picture Satish · Feb 14, 2013

Enable IP forwarding.

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

Permanent setting edit /etc/sysctl.conf and set 0 to 1

net.ipv4.ip_forward = 1

To enable the changes made in sysctl.conf you will need to run the command

sysctl -p /etc/sysctl.conf

Iptables Rules for NAT

# /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
# /sbin/iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
# /sbin/iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT