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,

This means your gateways are, and you have these IP addresses respectively :,

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

iface eth0:1 inet static

iface eth1 inet static

iface eth1:1 inet static

post-up ip route add default via 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 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 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 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 🙂


#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"
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"
    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"

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

About The Author


    1. ckaraca

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

      route -n

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