Linux – preserving a multi-homed setup across reboots

gatewayiptableslinuxrouting

Having read "two gateways / two providers with different ips", and the Clinton East article that it links to, I have successfully set up a multi-homed RHEL5 system. However, I am hoping for a better way to preserve this state than by calling the ip rule and ip route commands from rc.local. Is there a configuration file that I could edit that would get read as the interfaces come up? Something like iptables-save and iptables-restore perhaps?

Best Answer

In RHEL, there's two places where these things should probably go.

For adding static routes in RHEL, you add them to /etc/sysconfig/network-scripts/route-INTERFACE where INTERFACE matches the name of the ifcfg-INTERFACE entries. The statements in the route-* files will be passed to ip route add and ip route del when the interface is brought up or down.

Now, for the ip rule commands, the best place I can think of would probably be in /sbin/ifup-local. If this file exists, it gets run after an interface is brought up. One note with this one, if /sbin/ifup-local exists, it will get called as an executable and passed an argument of the interface name. So you'll have to write handler code to check that argument against the interfaces you want to add configs for and then take the appropriate action.

As an example:

#/bin/bash

case $1 in
  eth0)
    /sbin/ip rule foo
    /sbin/ip rule bar
    ;;
  eth1)
    /sbin/ip rule baz
    /sbin/ip rule quux
    ;;
  *)
    /usr/bin/logger -t 'ifup-local' 'Called with unknown interface:  $1'
    ;;
esac

If you wanted to, you could include the ip route rules for each interface in ifup-local, too. If you wanted to remove the corresponding rules when an interface is downed, you could create a similar /sbin/ifdown-local script to handle that.

If it were me, even though RHEL provides a standardized way of handling static routes, I'd probably do everything in ifup-local and ifdown-local, just to keep all my interface specific bits together.