Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to setup routing between 3 subnets using an Ubuntu 12.10 server with 3 NICs. I would like to be able to have all these subnets route between one another as well as be able to access the internet.

The 10.0.2.0 network is connected to the internet. The Netgear router box connected to the Internet has an address of 10.0.2.1.

The three subnets are 10.0.2.0/16, 10.100.0.0/16, and 10.101.0.0/16.

I have tried to setup my /etc/network/interfaces to define both the NICs and the routing but it doesn't seem to be working. I can ping all the NICs on the ubuntu server from any machine on any of the respective subnets but I am not able to ping any systems on different subnets from any of those subnets. Neither can I get out to the internet from the subnets.

# /etc/network/interfaces

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
# eth0
auto eth0
allow-hotplug eth0
iface eth0 inet static
        address 10.0.2.154
        netmask 255.255.0.0
        gateway 10.0.2.1
        dns-nameservers 10.0.2.100 8.8.8.8

# eth1
auto eth1
allow-hotplug eth1
iface eth1 inet static
        address 10.100.0.4
        netmask 255.255.0.0
        dns-nameservers 10.0.2.100 8.8.8.8

# eth2
auto eth2
allow-hotplug eth2
iface eth2 inet static
        address 10.101.0.1
        netmask 255.255.0.0
        dns-nameservers 10.0.2.100 8.8.8.8

up route add -net 10.0.0.0 netmask 255.255.0.0 gw 10.0.2.1 dev eth0
up route add -net 10.100.0.0 netmask 255.255.0.0 gw 10.0.2.154 dev eth0
up route add -net 10.101.0.0 netmask 255.255.0.0 gw 10.0.2.154 dev eth0
up route add -net 10.101.0.0 netmask 255.255.0.0 gw 10.100.0.4 dev eth1
up route add -net 10.0.0.0 netmask 255.255.0.0 gw 10.100.0.4 dev eth1
up route add -net 10.100.0.0 netmask 255.255.0.0 gw 10.101.0.1 dev eth2
up route add -net 10.0.0.0 netmask 255.255.0.0 gw 10.101.0.1 dev eth2

I would love to hear any suggestions as to how I could fix my /etc/network/interfaces file to properly route between these subnets.

share|improve this question
add comment

2 Answers

For everyone's reference here is the final solution to my routing problem. Below you will find the final /etc/network/interfaces, /etc/iptables.rules and /etc/sysctl.d/10-routing.conf files I ended up with.

It turns out that I needed to remove the routing commands from my /etc/network/interfaces file and then setup ipforwarding.

=========================================
/etc/network/interfaces

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
# eth0
auto eth0
allow-hotplug eth0
iface eth0 inet static
        address 10.0.2.154
        netmask 255.255.0.0
        gateway 10.0.2.1
        dns-nameservers 10.0.2.100 8.8.8.8
        pre-up iptables-restore < /etc/iptables.rules

# eth1
auto eth1
allow-hotplug eth1
iface eth1 inet static
        address 10.100.0.4
        netmask 255.255.0.0
        dns-nameservers 10.0.2.100 8.8.8.8

# eth2
auto eth2
allow-hotplug eth2
iface eth2 inet static
        address 10.101.0.1
        netmask 255.255.0.0
        dns-nameservers 10.0.2.100 8.8.8.8

=================================================

/etc/iptables.rules

# Generated by iptables-save v1.4.12 on Fri Feb  1 20:43:36 2013
*nat
:PREROUTING ACCEPT [204:18924]
:INPUT ACCEPT [35:6098]
:OUTPUT ACCEPT [3:164]
:POSTROUTING ACCEPT [1:40]
-A POSTROUTING -o eth0 -j SNAT --to-source 10.0.2.154
COMMIT
# Completed on Fri Feb  1 20:43:36 2013

=================================================

/etc/sysctl.d/10-routing.conf

net.ipv4.ip_forward=1
share|improve this answer
    
You've set up Source NAT here. I fail to see why you need that. It's also a bad thing to NAT in private addresses as it's not needed to share IP addresses - you have enough space! You're making spaghetti of your network here. –  gertvdijk Feb 2 '13 at 13:00
add comment

No need to add routes on the router. They're already directly connected there as a local interface! You can see that by running route -n. E.g.:

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.25.1    0.0.0.0         UG    0      0        0 wlan0   # def.gw
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 wlan0   # zeroconf
192.168.0.0     192.168.25.254  255.255.255.0   UG    0      0        0 wlan0   # route
192.168.11.0    0.0.0.0         255.255.255.0   U     0      0        0 testvpn # interf.
192.168.25.0    0.0.0.0         255.255.255.0   U     2      0        0 wlan0   # interf.
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0  # interf.

I've marked an actual route added manually here with # route. You'll notice that these lines are also listed with the G flag! The ones without that flag are just local, directly available.

The only thing you'll need to enable is IPv4 forwarding.

sudo sysctl -w net.ipv4.ip_forward=1

and enable it permanently by putting this line in a /etc/sysctl.d/10-routing.conf (you can make up a name) file:

net.ipv4.ip_forward=1

From here you'll have to tell the hosts in the networks about these routes. So, on a machine in the 10.100.0.0/16 for example, you'll need to add the routes like this:

# On a client somewhere in 10.100.0.0
auto eth0
iface eth0 inet static
  address 10.100.0.123
  netmask 255.255.0.0
  # route 10.0.0.0/16 via 10.100.0.4
  up route add -net 10.0.0.0 netmask 255.255.0.0 gw 10.100.0.4
  # route 10.101.0.0/16 via 10.101.0.1
  up route add -net 10.101.0.0 netmask 255.255.0.0 gw 10.101.0.1

A much better way to announce these routes is to tell them by using DHCP, or to make it the default gateway by using gateway. Alternatively, use 'real' routing protocols such as OSPF, RIP, etc.


By the way, the syntax in your /etc/network/interfaces file is wrong. The up entries should be part of each individual interface listed, indented by spaces such as the address entry.

share|improve this answer
    
I see your updated suggestion of adding routes to each host. Is there any way to avoid this other than using DHCP? All my hosts have static addresses and I don't have access to some of them to configure network settings or routing. Is it not possible to define routes on the Ubuntu server I am using as the router to automatically handle the routing without having to configure the clients? To be clear, I can tell all my users which IP addresses and gateways to use when setting up IP, I just can't tell them to add routes. That would be way too complicated. –  user127875 Feb 1 '13 at 22:03
    
@user127875 Yeah, I included several options: DHCP (which you just discarded), making this the default gateway, using a real routing protocol (RIPv2, OSPF, etc.). From the information you just told me, I think you'll have to revise your network - you're doing it wrong once you notice you'll have to apply configurations to individual machine generally. –  gertvdijk Feb 1 '13 at 22:05
    
Would it help to define a gateway for each interface on the Ubuntu server in the /etc/network/interfaces file? –  user127875 Feb 1 '13 at 22:21
    
@user127875 No! All your hosts in your network need to be able to know how to reach the networks. So, either they need to have the entries in the kernel routing table, or, you'll have to configure the default gateway they are on to route it properly. This is gettin off-topic here, as this is very basic IPv4 networking concept which is irrelevant to Ubuntu. I suggest to learn more about how routing works and making a plan how you're going to design your network. –  gertvdijk Feb 1 '13 at 22:25
    
I apologize for being obtuse, and really do appreciate your help. I totally understand that each client needs to be configured to know what the default gateway is. When my users setup the static IP for there systems I will tell them to set the gateway option to point to the address of the NIC of my Ubuntu server which is in their subnet. This way their client will know where to go to get out of the subnet. Then the Ubuntu router only needs to know what to do with the traffic that comes to it. From what you said earlier it seems that this should work with ipforwarding and no manual routes. –  user127875 Feb 1 '13 at 22:31
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.