How to set static routes in Ubuntu Server?
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?
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
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
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/
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
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