Admins eHow SysAdmin Tips & Tricks

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!

  • 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


  • Servowire

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

  • Harry_alcala

    Great!! Is works successfully!!



  • Rom

    Thank you! That really helped!

  • Kris Chi

    Nice work, thanks!

  • Thanks a ton

  • Kallol

    thanks a lot sir !!

  • Dean

    Thank you

  • 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 dev eth0 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 (, Debian2(, Debian3(, Debian4(
    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 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.

  • Kapil Pant

    Great ! Thank you for the valuable information. I was searching for the proper routing command for a while. Finally now I am able to install Debian and ubuntu server 🙂

  • Nalla Sivam

    Thank you Admin 🙂 It worked.

  • Marco

    Hi there

    This is an old post, I hope it’s still allowed to ask here.
    I am now struggling since days with my xenserver-installation. Maybe you find some minutes to help me, if possible.

    There is a dedicated host with 1 dedicated IP and a /29-subnet. The subnet is failover-ready and I can “move/point” it
    to a different dedicated server if I need that. The /29-subnet has as gateway the dedicated-main-ip.

    Dedicated Server
    OS: XenServer 7.2
    IP: 111.222.333.1
    Gateway: 111.222.333.193

    VirtualMachine1 on DedicatedServer
    OS: Debian 8
    IP: 111.222.444.81
    Gateway: 111.222.333.1 (as you can see gateway is dedicated-main-ip)

    So, my config for my Debian-VM is as follows:
    auto lo
    iface lo inet loopback

    auto eth0
    iface eth0 inet static

    address 111.222.444.81
    broadcast 111.222.444.87

    post-up route add 111.222.333.1 dev eth0
    post-up route add default gw 111.222.333.1

    So, if I try to ping or something like that I get NO error like “Network not reachable”.
    But on the other hand there is no access to the “internet”, I aswell cannot ping the gateway or so.

    Does anyone understand the problem in this combination with standard-installation of XenServer?

    Best regards

  • Nikhil Sapa

    hi my practical I write this and still I don’t get 5/5 marks please twell why you post wrong artiucle?

Powered by WordPress