Somebody answered my earlier question by describing how to create a new routing table with source policy routing:
echo 13 eth3 >> /etc/iproute2/rt_tables
ip route add default via 10.20.0.1 table eth3
ip rule add from 10.20.0.2 lookup eth3
How do I make those "ip" commands persist across a reboot? I assume that there's some appropriate lines to be added to /etc/network/interfaces
. Is this the right away (adding "up" lines to the interface), or is there another way to do it?
iface eth3 inet static
address 10.20.0.2
netmask 255.255.255.0
up ip route add default via 10.20.0.1 table eth3
up ip rule add from 10.20.0.2 lookup eth3
down ip rule del from 10.20.0.2 lookup eth3
down ip route del default via 10.20.0.1 table eth3
(I'm on Ubuntu 12.04).
Different administrators accomplish this in different ways.
I'm primarily using Debian and I feel the "most correct", correct being defined as the most obvious, integrated and documented way to do this , is by adding
post-up
directives to your/etc/network/interfaces
file as you have done. If you do this make sure you don't cheat and just put all yourup
orpost-up
directives under one interface. Have each interface add the routes that appropriate to it.The other way I've commonly seen this done is with a custom init script very similar to one @mgorven has posted.
I've written an if-up script which automatically does this for every non-loopback interface. (I've just modified it to deal with non-DHCP interfaces without a defined gateway, so it may be buggy.) The routing tables need to be created beforehand.
/etc/network/if-up.d/source-route
: