yellowpigs.net

Multiple subnets

I recently had reason to set up a Linux box with two network interfaces, each with a different IP on a different subnet. I wanted both IPs to be addressable, for no routing/forwarding to be done between the two subnets, and for the Linux box to reply out the same interface that a connection was requested on.

This setup is explained pretty well in the Advanced Routing HOWTO. For anyone running Debian GNU/Linux (at least woody or sarge), here's my /etc/network/interfaces file to accomplish this:

  auto lo
  iface lo inet loopback

  auto eth0
  iface eth0 inet static
    address $ip1
    netmask $mask1
    network $network1
    broadcast $bcast1
    gateway $gw1
    up ip route add $network1/$cidr1 dev eth0 src $ip1 table T0 && \
    ip route add default via $gw1 table T0 && \
    ip rule add from $ip1 table T0
    down ip rule del from $ip1 table T0 || true

  auto eth1
  iface eth1 inet static
    address $ip2
    netmask $mask2
    network $network2
    broadcast $bcast2
    up ip route add $network2/$cidr2 dev eth1 src $ip2 table T1 && \
    ip route add default via $gw2 table T1 && \
    ip rule add from $ip2 table T1
    down ip rule del from $ip2 table T1 || true

I also needed to:

Network interfaces defined in the interfaces file can be brought up or down with ifup and ifdown. To view link, IP, route, and rule information, use ip link show, ip addr show, ip route show, and ip rule show respectively.