Linux – dynamic ‘route add’ command doesn’t work but static is working

linuxroute

i am having a situation where

route add

is not working but if i add that as a static entry in sysconfig/network-scripts/route-eth0

its working

Network details:

IPADDR=198.x.x.1

NETMASK=255.255.255.255

BROADCAST=198.x.x.1

if i add a static route using

route add default gw 192.x.x.254 

it returns
SIOCADDRT: No such process

simillary

ping 192.x.x.254

connect: Network is unreachable

strangely if i add it as static entry in route-eth0

route add 192.x.x.254 dev eth0 
route add default gw 192.x.x.254

everything works,i can see default gw in route -n
and ping is also working

So what is making the difference here?

Best Answer

You cannot add that route because that default gateway is in different subnet and thus is not reachable.

From man route:

   gw GW  route packets via a gateway.  NOTE: The specified gateway must be reachable first. This usually means that you have to set up a
          static  route  to the gateway beforehand. If you specify the address of one of your local interfaces, it will be used to decide
          about the interface to which the packets should be routed to. This is a BSDism compatibility hack.
Related Topic