Set up permanent routing (Ubuntu 13.04) [duplicate]
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!
Solution 1:
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
Solution 2:
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?