13

I have 2 connections: wlan0 (gw: 192.168.1.1) and eth0 (gw: 192.168.2.1).
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 31.135.208.0/21 gw 192.168.2.1 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 31.135.208.0/21 gw 192.168.2.1 dev eth0

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

auto eth0
address 192.168.2.125
gateway 192.168.2.1
netmask 255.255.255.0

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!

0
11

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
#!/bin/bash
# Check for specific interface if desired
[ "$IFACE" != "eth0" ] || exit 0
# Adding additional routes on connection
route add -net 31.135.208.0/21 gw 192.168.2.1 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/000resolvconf
/etc/network/if-up.d/avahi-autoipd
/etc/network/if-up.d/avahi-daemon
/etc/network/if-up.d/ethtool
/etc/network/if-up.d/ip
/etc/network/if-up.d/ntpdate
/etc/network/if-up.d/openssh-server
/etc/network/if-up.d/openvpn
/etc/network/if-up.d/script <---- your script here
/etc/network/if-up.d/upstart
/etc/network/if-up.d/wpasupplicant
7
  • 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
8

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

up route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1

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

Example interfaces file:

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

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.