I have 2 connections: wlan0 (gw: and eth0 (gw:
My default connection is wlan0, but I wanted some websites load through my eth0, so I set up routing for those websites/IPs like this:

route add -net gw dev eth0

This works, however is not permanent (disappears after reboot). I tried to put this code in /etc/network/interfaces like this:

up route add -net gw dev eth0

but when I restart Ubuntu, it starts without networking (it crashes). I also put these lines into that file:

auto eth0

but still system boots without networking.

Currently I have put route command into /etc/rc.local, it works, but it is not permanent, because when I restart/re-connect my eth0 (for some reason), routing disappears.

Some notes:
I have long list of IPs to route.
My router, which I connected eth0 is set to DHCP.
I tried to connect eth0 both static and DHCP way.

Thanks for you help!


On my system it worked when i made a script in the folder /etc/network/if-up.d/ called script with execute permissions 755:

#ls -la /etc/network/if-up.d/script
-rwxr-xr-x 1 root root   46 Okt 15 11:46 script

There you can add command to execute when the connection is established, in your case:

#cat /etc/network/if-up.d/script
# Check for specific interface if desired
[ "$IFACE" != "eth0" ] || exit 0
# Adding additional routes on connection
route add -net gw dev eth0
route add ...
route add ...

You don't need to add something in /etc/network/interfaces besides auto eth0 and iface eth0 inet dhcp or whatever you have there. NetworkManager invokes the scripts in this directory by exec run-parts /etc/network/if-up.d in the script /etc/NetworkManager/dispatcher.d/01ifupdown. You can test this by (in my case):

# run-parts --test /etc/network/if-up.d
/etc/network/if-up.d/script <---- your script here
  • 1
    Nice answer - if you need to make the routes specific to one interface, so they only get added when eth0 is connected, try adding: [ "$IFACE" != "eth0" ] || exit 0 before the route add lines. – Greg Oct 15 '13 at 10:06
  • Good proposal, i added you line. – chaos Oct 15 '13 at 10:17
  • @chaos, thanks for answer. However script does not run when I connect to eth0. But it works when I run like this: run-parts /etc/network/if-up.d – Azamat Oct 15 '13 at 15:08
  • So your NM isn't invoking the script or it isn't existing. Can you please post the output of ps faux | grep -i network, cat /etc/NetworkManager/dispatcher.d/01ifupdown | grep run-parts and nm-tool – chaos Oct 16 '13 at 8:36
  • @chaos , thank you for taking time to help me, here you go: pastebin.com/dNZMsHn7 – Azamat Oct 17 '13 at 6:05

The syntax for adding routes to you interfaces is as follows:

up route add -net netmask gw

You don't need to add the device to the end.

Example interfaces file:

auto eth0
iface eth0 inet static
      up route add -net netmask gw

Based on info from: How to set static routes in Ubuntu Server?

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