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!

Azamat
  • 436
  • 1
  • 4
  • 9

2 Answers2

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
Dan
  • 13,119
chaos
  • 27,506
  • 12
  • 74
  • 77
  • 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 08:36
  • @chaos , thank you for taking time to help me, here you go: http://pastebin.com/dNZMsHn7 – Azamat Oct 17 '13 at 06:05
  • So, I think i figured it out. First, your networking isn't starting because the syntax in /etc/network/interfaces is wrong. Second, for your concern it isn't recommended to use /etc/network/interfaces for managing this interface because every time you replug the cable, you need to /etc/init.d/networking restart to tun the if-up.d script to set the routes again. So, delete everything in the interfaces file relating to the eth0 interface. Then, do a /etc/init.d/networking restart and /etc/init.d/network-manager restart and make sure that "Enable Networking" is checked in the NM-Applet. – chaos Oct 17 '13 at 07:27
  • @chaos, in /etc/network/interfaces I have following only: auto lo iface lo inet loopback There is nothing relating to eth0.. – Azamat Oct 17 '13 at 22:03
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?

NGRhodes
  • 9,490