Ubuntu – How to set static routes in Ubuntu Server

networkingrouting

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?

Best Answer

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

auto eth0
iface eth0 inet static
      address 192.168.1.2
      netmask 255.255.255.0
      up route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1
      up route add -net 172.16.0.0 netmask 255.240.0.0 gw 192.168.1.1