Multiple network interfaces on Linux PC

3

1

I have a question related to multiple network interfaces on Linux PC.

Suppose I have two connected network interfaces. Let us say that both network interfaces connect to LANs. Let us say one LAN (connected with eth0) has IP addresses 192.168.10.* and other LAN (connected with eth1) has IP addresses 192.168.20.*.

How can I configure Linux, so that all requests sent to any IP address starting with 192.168.10. will be sent over eth0 and all requests sent to any IP address starting with 192.168.20. will be sent over eth1?

geekowl

Posted 2013-02-02T01:50:26.577

Reputation: 31

Answers

1

There's nothing special you need to do. Interface routes are added by default and do exactly this. The system will send traffic out the interface "closest" to the packet's destination.

David Schwartz

Posted 2013-02-02T01:50:26.577

Reputation: 58 310

Thank you for the reply. I really appreciate it. If I connect to LAN on eth0 and WAN (Internet) on eth1, I cannot browse or use any Internet related software. If I disconnect from LAN on eth0 and restart eth1, I can browse and use software related to Internet. Why would this happen? – None – 2013-02-02T03:56:36.900

Do you have a default route (gateway) on the eth0 interface? You shouldn't. – David Schwartz – 2013-02-02T04:35:42.797

0

@David Schwartz is correct. But you also need to make sure the subnet masks are set correctly. In your case above, you want to set the subnet masks of both interfaces to 255.255.255.0.

Your original question stated there was a LAN behind both interfaces (not the "Internet") so the above is correct for that situation.

If you want the machine to forward traffic between subnets (subnet = LAN for the purposes of this) you need to make sure you echo 1 to /proc/sys/net/ipv4/ip_forward - and also make sure your iptables is set to not filter anything. If you don't do this, machines behind both LANs can reach the Linux box but not the other LAN.

Now, you seem to want to have an interface reach both the Internet and a LAN. It can be done if the LAN in question has a router and you know that router's IP address. You would set the Linux box's default gateway to the IP and subnet of the router. (If there is no router to the Internet on any LAN, then you don't set a default gateway at all.)

What's happening when you "restart eth1" is likely that eth1 is getting an IP via DHCP from a router and your IP addresses and default route assignments are actually changing when you do that.

If your DHCP is set correctly on the LAN with the router, with an IP reservation or similar, you can probably just let DHCP take care of it.

LawrenceC

Posted 2013-02-02T01:50:26.577

Reputation: 63 487