September 26, 2011

Theoretically host IP and gateway should be on the same IP subnet. but there are some situations where host IP and gateway subnet are on different subnets. like my situation today. I was assigned two additional IPs for my server by my Data-center, but IPs were from a different subnet compared to server main IP. these IPs will work if you set them as additional IPs. but I needed them to create two new VPS’s on my server with bridged network interface. in this situation additional IPs should serve as main IP address and there is no gateway on same subnet available.
So here are the assumptions :

a.b.c.d is the host IP
e.f.g.h is the gateway IP
a.b.c.d & e.f.g.h are on different subnets.

by default if you try to set gateway by following command :

route add default gw e.f.g.h

you will get this error :

SIOCADDRT: No such process

the trick is simple , first add a route to default gateway itself and then set the default gateway , like this :

route add e.f.g.h/32 dev eth0
route add default gw e.f.g.h

remember you may need to change eth0 to your device name , it may be eth1 or wlan0 or anything.

How to make these route changes persistent ?

For Debian/Ubuntu :
Add the following lines to /etc/network/interfaces :