Even though this seems an easy task, when you add IP’s with different gateways, you have to route the IP’s for the correct gateways.

Let’s say you have two networks,
192.168.80.128/25
192.168.90.128/25

This means your gateways are 192.168.80.129, 192.168.90.129 and you have these IP addresses respectively : 192.168.80.130-254, 192.168.90.130-254

Lets assign the 192.168.80.x IP’s to eth0 and 90.x to eth1.
Then we’ll have to edit our /etc/network/interfaces file accordingly:

auto lo
iface lo inet loopback

auto eth0 eth0:1 eth1 eth1:1

iface eth0 inet static
    address 192.168.80.130
    netmask 255.255.255.128
    gateway 192.168.80.129

iface eth0:1 inet static
    address 192.168.80.131
    netmask 255.255.255.128

iface eth1 inet static
    address 192.168.90.130
    netmask 255.255.255.128

iface eth1:1 inet static
    address 192.168.90.131
    netmask 255.255.255.128

post-up ip route add default via 192.168.90.129 dev eth1 table 101

As you can see, we did not specify any gateways for other devices than eth0. This is because, we will route the 192.168.90.x IP addresses through 192.168.90.129 but the system will not know it as a default gateway. That’s why we added the last line to interfaces, we tell to route 192.168.90.129 using table 101. Well, you’ll say “We haven’t defined that table yet, what’s this 101?”, that’s true, normally you have to define it in your shell, but we’ll add it to our rc.local so that it will be there all the time (after a reboot).

In order to achieve this, we add the following line to /etc/rc.local (add it before the “exit 0”)

ip rule add from 192.168.90.128/25 lookup 101

Now write the ip rule to your shell too (so you won’t need a reboot) then restart your networking service.

/etc/init.d/networking stop && /etc/init.d/networking start

Here you go, route -n will show you only one gateway, yet you’ll be able to use both IP classes on both devices. So technically we did not add two gateways, but we’re using both of them.

Don’t forget that we only added 4 IP’s here, if you want to add all of them in the block, you better write a script to generate it.

Ok you don’t have to cry about it, below is a Python script I wrote for you, edit it accordingly 🙂

#/usr/bin/python

#ip generator script - plugged.in

IP_c1 = 80
IP_c2 = 90

print "auto lo"
print "iface lo inet loopback"

print "auto eth0"
print "iface eth0 inet static"
print "\taddress 192.168.%i.130" % IP_c1
print "\tnetmask 255.255.255.128"
print "\tgateway 192.168.%i.129" % IP_c1

print "auto %s %s" % (" ".join(["eth0:%i" % i for i in range(1,124)])," ".join(["eth1:%i" % i for i in range(1,128)]))

for c in [IP_c1,IP_c2] :
    if c == IP_c1 :
        for i in range(131,255) :
            print "iface eth0:%i inet static" % (i-130)
            print "\taddress 192.168.%i.%i" % (c,i)
            print "\tnetmask 255.255.255.128"
    else :
        for i in range(130,255) :
            print "iface eth1:%i inet static" % (i-129)
            print "\taddress 192.168.%i.%i" % (c,i)
            print "\tnetmask 255.255.255.128"

print "post-up ip route add default via 192.168.90.129 dev eth1 table 101"
3 Responses to Add Multiple Gateways to Multiple NICs on Ubuntu Server
  1. Khalil Khamlichi 28 March 2014 at 18:46 Reply

    Nice article, thanks

  2. Any way to add two default gateways for two NIC on centos if there is only one network interface?

    • You should rather add a route to second gateway:
      check your routes by typing:

      route -n

      and such that your second gateway is 192.168.1.1 type:
      route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1


[top]

Leave a Reply

Your email address will not be published. Required fields are marked *