Admins eHow

September 26, 2011

Gateway on a different subnet on Linux

Filed under: Debian,General,linux — Tags: , , , , , — admin @ 7:50 am

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 :

post-up route add e.f.g.h/32 dev eth0
post-up route add default gw e.f.g.h
  • 3point2

    thank you! this was helpful to me. i had defined a gateway to the default gateway’s subnet but route still giving the error you mentioned. adding the gateway IP itself worked!

  • 3point2

    thank you! this was helpful to me. i had defined a gateway to the default gateway’s subnet but route still giving the error you mentioned. adding the gateway IP itself worked!

  • http://profiles.google.com/sollis Steve Ollis

    Thanks much.. I’d added the route to the subnet, and it didn’t work.. hadn’t thought about adding a specific route for the actual route of last effort

    Thanks

  • Servowire

    Thanks this saved me on a KPN (dutch) line.

  • Harry_alcala

    Great!! Is works successfully!!

    Thanks!!!

     

  • Rom

    Thank you! That really helped!

  • Kris Chi

    Nice work, thanks!

  • http://twitter.com/sudheer43 Sudheer Kumar

    Thanks a ton

  • Kallol

    thanks a lot sir !!

  • Dean

    Thank you

  • http://www.comtechies.com/ BIBIN WILSON

    Thanks a lot….u saved a lotta time for me…

  • Alphagrey

    When I run ‘route add e.f.g.h/32 dev eth0′ I get the following error. (I replaced e.f.g.h with my gateway ip)

    route: netmask 00000000 doesn’t make sense with host route

    Any thoughts?

  • esmaeil

    please be sure e.f.g.h/32 is your network range Ip address.

  • Marna Marie’ Strauss

    I’ve been struggling with this issue… but still I cannot add my 172* network to my nagios box on a 192* network
    I use:
    route add default gw 172.16.7.1/24 dev eth0
    172.16.7.1/24 Unknown host

    Yet I can ping the host.

    Any ideas please?

  • Jonathan L

    How to make the changes persistent in Red Hat/CentOS?

  • AdminseHow

    one easy way is to add the commands to /etc/rc.local

  • atrx

    Seems to me like you’re on the 192* network and not the 172* network. If this is the case you can ping the 172* network b/c the router is on both (not nagios). If these two logical netoworks are on the same physical network, create a sub interface. If they’re on different vlans, create a trunk and set up your interfaces on the vlans. If they’re on seperate physical networks (separated physically by a router) run an additional cord to the nagios server, or reconfigure the network.

    Hope this is applicable and helps.

  • Marna Marie’ Strauss

    hi there! Thank you for the update.

    Yes my Nagios server is located on the 192 network – if I update my “routes” on a windows PC I am able to ping/access the 172 network

    How do I “add a route” on Ubuntu?

  • David

    Thanks for this tuto. It helps me. But I’m stuck with an exercice

    Actually I have four machine Debian1 (192.168.1.1), Debian2(192.168.1.2), Debian3(192.168.50.1), Debian4(192.168.50.2)
    I can ping Debian1 and Debian2 together and it’s the same for Debian3 and Debian4. I find a way to ping Debian3 and Debian 4 with a gateway because of your tuto :D.
    But what I’d like to do is interconnected Debian2 and Debian4 with this range of IP 192.168.61.0/24 I suppose I have to create several gateway but I don’t know how to do. Maybe I need to send them an additonnal IP ?

    I really don’t know how to do it.

Powered by WordPress