61

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?

3
  • 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, 2012 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, 2012 at 0:37
  • Definitely read man 5 interfaces Nov 2, 2018 at 15:09

8 Answers 8

75

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
9
  • 18
    Syntax for /sbin/ip: up ip route add 192.168.0.0/24 via 192.168.1.1
    – w00t
    Jan 23, 2014 at 13:01
  • 1
    Would you kindly explain your last two lines in the answer or provide a link to the knowledge source? Thanks!
    – itsols
    Sep 7, 2014 at 7:23
  • You can view -net 192.68.0.0 as a wildcard, saying all packets destined for 192.168.*.* should be routed via the default gateway 192.168.1.1. 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.
    – Goblinhack
    Jun 4, 2015 at 19:32
  • 1
    @BЈовић It is described in 16.04 interfaces man page under section IFACE OPTIONS: "up command". The interfaces manual page for 18.04 was modified. up is listed in ifupdown-addons-interfaces. Jun 4, 2019 at 11:18
  • 2
    As it is 2021, you should no longer configure /etc/network/* or use ifup or ifdown. On Desktop systems use NetworkManager (see the answer of @ofirule), on Server systems use netplan (see answer of @Iman Kermani).
    – mats1995
    Jan 3, 2022 at 13:11
20

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 172.16.0.0/24 via 192.168.10.1 || true
up ip route add 192.168.0.0/16 via 192.168.10.1 || 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
3
  • 1
    Why not simply add a "down ip route del 192.168.0.0/16 via 192.168.10.1" to the config?
    – w00t
    Jan 23, 2014 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, 2015 at 22:23
  • 6
    From a random user: Use "replace" instead of "add" in the ip command. Ex: up ip route replace 172.16.0.0/24 via 192.168.10.1 "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, 2016 at 17:52
10

As it is 2021 you can config it using netplan on ubuntu 18.04 or later (tested on ubuntu 20.04). compelete reference is available on netplan site. the config file can be found under /etc/netplan/ directory.

network:
  version: 2
  ethernets:
    ens160: # in my case the interface name is ens160
      dhcp4: false  # or true. depends on your situation
      addresses: [192.168.0.10/24]  # your static IP address
      # the actual answer is here
      routes:
        - to: 0.0.0.0/0  # or any other subnet you like. 0.0.0.0/0 means default gateway
          via: 172.16.0.1  # or any other gateway you want.
          on-link: true   # as you see my interface IP address and default gateway are not in the same subnet so we should put this true but if they are in the same subnet this is not required. 

PS. the YAML configuration files are sensitive to indentation and it does not accept tabs. the indentation is 2 spaces.

don't forget run netplan apply.

1
  • This was exactly the kind of answer I was looking for. Thanks!
    – AdHorger
    Nov 14, 2022 at 18:11
9

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 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1
    post-up /bin/mount -t nfs 192.168.0.203:/motd/ /motd/
6

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:

192.168.240.0 255.255.255.0 192.168.130.3 em3
4

Using nmcli I was able to run a persistent solution (@isapir solution didn't work on my device).

sudo nmcli connection modify <conection-name> +ipv4.routes "<ip>/<mask> <gw> <mt>"

For example:

sudo nmcli connection modify ssid-name +ipv4.routes "1.1.1.0/24 192.168.0.1 100"

After that applying the changes is possible by setting the connection down and up again (no need for full NetworkManager restart):

sudo nmcli con down ssid-name ; sudo nmcli con up ssid-name

To see current connections run:

sudo nmcli connection
2
  • 2
    This answer should be use. As it is permanent after reboot and also works with newer versions of Ubuntu and NetworkManager. Also for those who don't know, if you want to your route has the most priority use 0 metric: sudo nmcli connection modify ssid-name +ipv4.routes "1.1.1.0/24 192.168.0.1 0" Oct 29, 2020 at 8:54
  • As NetworkManager is only available (by default) on desktop systems: if it is not available, you should use netplan - see the answer of @"Iman Kermani". @"Pouria Moosavi": The primary match for a route is the prefix length (== most specific route to a network). The metric is only evaluated thereafter.
    – mats1995
    Jan 3, 2022 at 13:37
3

what worked for us was

sudo route add -net 192.168.0.2/32 gw 192.168.1.1 netmask 255.255.0.0

i used to run on a Mac:

sudo route -n add -net 192.168.0.2/32 192.168.1.1

but on a ubuntu the "gw" between IPs and the last "netmask" part was missing (also -n not required on ubuntu)

2
  • 13
    this works perfectly for temporary (non-persistant) routes, the question is about getting routes persistent
    – anneb
    Jul 27, 2017 at 13:06
  • Not working SIOCADDRT: Invalid argument
    – Madeo
    Jul 18, 2021 at 7:02
3

I prefer to use nmcli.

nmcli device modify <device> +ipv4.routes "<ip>/<mask> <gw>" ipv4.route-metric 0

For example:

nmcli device modify wlp2s0 +ipv4.routes "1.1.1.0/24 192.168.0.1" ipv4.route-metric 0

After that apply the changes by restarting the NetworkManager service:

sudo systemctl restart NetworkManager.service
1
  • 1
    nmcli device... commands will not persist on reboot. nmcli connection... commands will. see @ofirule solution. Mar 11, 2020 at 15:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .