0

If I have an 4 (or more) ethernet port linux box, How to setup it as router to handle 4 class c subnet ? for example, eth0 => 192.168.1.0/24, eth1 => 192.168.2.0/24, eth2 => 192.168.3.0/24, eth3 => 192.168.4.0/24, each client machine with in those 4 subnets can talk to each other.

Any suggest script ?

1 Answer 1

2

Per your interfaces settings, you need to let the kernel do Network Address Translation to route packets across these subnets. Try this first:

# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -d 192.168.2.0/24 -j SNAT --to 192.168.2.1
# iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -d 192.168.1.0/24 -j SNAT --to 192.168.1.1

Where 192.168.2.1 is the IP address of eth1 and the default gateway for subnet 192.168.2.0/24, 192.168.1.1 alike.

And make sure you have net.ipv4.ip_forward=1 in /etc/sysctl.conf. And run sysctl -p to make that change in effect.

11
  • Please comment if that works or not. I do not have machines to test right now at work. Apr 5, 2012 at 3:17
  • Bad argument '192.168.2.1' Bad argument '192.168.1.1' By the way, I am using Ubuntu Server 11.10 i386 3.0.0-17-generic-pae #30-Uuntu SMP Thu Mar 8 17:53:35 UTC...
    – Fox Mulder
    Apr 5, 2012 at 13:57
  • ok, I am change the -j SNAT 192.168.2.1 ==> j- SNAT --to 192.168.2.1, It's accept now.
    – Fox Mulder
    Apr 5, 2012 at 14:01
  • Sorry, I now update my post. You can test with only one iptables rule first. Apr 5, 2012 at 14:19
  • Thanks your help. It's won't work if only with POSTROUTING/SNAT, But after add PREROUTING/DNAT, It's work now...... :)
    – Fox Mulder
    Apr 5, 2012 at 14:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .