Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have used Ubuntu since Ubuntu 8.10; today I'm working with Ubuntu 12.04 Server.

I am having difficulty keeping static routes on booting. I would usually put the route commands

/sbin/route add -net <IP>/<MASK> <GW> dev <ethX>

in /etc/rc.local or I would create a file (named routes) inside the directory /etc/network/if-up/, but I notice that on Ubuntu 12.04 it isn't working.

If I type the commands in the shell, they work, but the same commands don't work when they are in the specified file.

I already tried to change the file name to other names thinking that my file name (routes) could be erroneous in Ubuntu 12.04, but that also did not work.

I notice also that command /sbin/ifconfig works, less the /sbin/route.

What changed in network set-up?

How can I define static routes on Ubuntu 12.04?

share|improve this question
Just to check: is rc.local executable and it starts with the shebang line? Maybe its a problem with your command. You can try redirect stderr to a file (your_command > stderr.txt 2>&1) and inspect its output... – Salem Jul 25 '12 at 16:59
You should set routes in /etc/network/interfaces, not in /etc/rc.local. And new commands you should use is ip add and ip route. One command to rule the net. :) – Anders Jul 26 '12 at 0:37

You can put static routes in /etc/network/interfaces:

auto eth0
iface eth0 inet static
      up route add -net netmask gw
      up route add -net netmask gw
share|improve this answer
I will try it. Thanks, Eric Carvalho. – Frank Allan Jul 25 '12 at 17:15
Syntax for /sbin/ip: up ip route add via – w00t Jan 23 '14 at 13:01
Would you kindly explain your last two lines in the answer or provide a link to the knowledge source? Thanks! – itsols Sep 7 '14 at 7:23
You can view -net as a wildcard, saying all packets destined for 192.168.*.* should be routed via the default gateway The kernel will then try to resolve that default gateway but if it has an ARP entry already, then it will send the packet out the interface it has learned that gateway resides on with the destination MAC for that next hop. Hope that helps a bit. – Neil McGill Jun 4 '15 at 19:32

I found very often that the correct place to define a static route is in /etc/network/interfaces, it is ok if you are going to globally restart the network with /etc/init.d/networking restart for example. But if you are going to use ifdown and ifup to individually shutdown an interface, ifup will end with the error:

ifup eth1

RTNETLINK answers: File exists
Failed to bring up eth1.

Because of it tries to define a route but it is already defined. The interface will be up anyway but, ifup will not update /run/network/ifstate so next time you will us ifdown you will not able to do it unless you use the --force flag.

To make ifup to continue configuring even if routes are already defined, you can use this format when defining routes in /etc/network/interfaces

up ip route add via || true
up ip route add via || true

This way you will have the warning in the output but the interface configuration will be completed

ifup eth1

RTNETLINK answers: File exists
RTNETLINK answers: File exists
ssh stop/waiting
ssh start/running, process 18553
share|improve this answer
Why not simply add a "down ip route del via" to the config? – w00t Jan 23 '14 at 12:59
Because people do things like use ifconfig manually, instead of running ifdown, then want to be able to recover by using the correct tools. Thus it's helpful if those tools are resilient to unexpected state as they try to fix the state. The || true suggestion is a good one. Adding a down line too is helpful. – Phil P Jul 18 '15 at 22:23
From a random user: Use "replace" instead of "add" in the ip command. Ex: up ip route replace via "replace" would add the route if it doesn't exist yet, but if a route with the same selector exists it would be replaced by the current one, even if they have the same destination. – Xen2050 Mar 28 at 17:52

You can try this (add it to /etc/network/interfaces), this is almost complete way for setting routes:

auto eth0
iface eth0 inet static
    post-up /sbin/route add -net netmask gw
    post-up /bin/mount -t nfs /motd/
share|improve this answer

There is a package ifupdown-extra avaible in Ubuntu.
It provides automatic scripts (installed in /etc/network/*/), one of which is used to add static routes.

The configuration file for this is /etc/network/routes

The top of this config file has a good description:

This configuration file is read by the static-routes if-updown script and the /etc/init.d/networking-routes script to setup a list of routes associated either with a given interface or global routes.

An example route I use is: em3
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.