Multi Homing Debian/Ubuntu instances

As some CentOS/RedHat folks might know you can use the GATEWAYDEV option inside the networks configuration file to accept the network default gateway only from this interface. (GATEWAYDEV=eth0 for example). This is particular useful when connecting instances to multiple networks like a public and internal network to eliminate unnecessary routing while using DHCP to assign the network addresses to the interface. The need is primarily given when both networks, public and internal push DHCP default router (gateway) information to allow multi homed and single homed instance in the same networks.

One way of implementing a similar feature like RedHat or the derivatives is to utilize DHCP client hooks to alter the DHCP options pushed from the server to the client. The DHCP client program does support enter an exit hooks, which allows for alteration pre and post interface configuration.

For this use case I implemented the following enter hook to ignore the default router information on all interfaces other than the elected one. Personally I would recommend that the public facing interface is always the first one and the Internet default gateway is using this interface. Currently all dhclient enter hooks are stored inside the /etc/dhcp/dhclient-enter-hooks.d directory and are executed in alphanumeric order. This is also the reason why I prefixed the script with the number 1.

RUN='yes'
 
if [ $RUN = 'yes' ]; then
  if [ "$reason" = "BOUND" -o "$reason" = "REBOOT" ]; then
    if [ $interface != 'eth0' ]; then
      test -f /tmp/dhclient-script.debug && echo "Stripping default GW off $interface" |tee -a /tmp/dhclient-script.debug
      new_routers=""
      old_routers=""
    fi
  fi
fi