IP Aliases with NetworkManager

As we all know NetworkManager makes life with Gnome easier for the most part. Unfortunately when it comes to more advanced network setups it can get complicated to impossible with NM. I finally got IP aliasing working with NM after a long read.

In Debian based systems (Debian/Ubuntu/Mint/etc) you have /etc/NetworkManager which holds configuration information for NM. Under that directory is another directory called dispatcher.d (/etc/NetworkManager/dispatcher.d). This directory holds scripts that are executed during the differing phases of operation of NM and are executed in alphabetical order.

So to get aliasing working for an interface just add a script to be executed after the ifupdown script for your interface. Here is my current listing in that directory.

root@atitude:/etc/NetworkManager/dispatcher.d# ls -al
total 16
drwxr-xr-x 2 root root 4096 2012-06-15 11:38 .
drwxr-xr-x 5 root root 4096 2011-08-02 14:08 ..
-rwxr-xr-x 1 root root 1419 2011-04-15 06:54 01ifupdown
-rwxr-xr-x 1 root root  837 2012-06-15 09:39 01ifupdownaliases

01ifupdown is the normal control script for NM and 01ifupdownaliases is the one to control the aliases for my card.

Here is the content of 01ifupdownaliases:

#!/bin/sh -e
# Script for nm to add aliases to eth0

if [ -z "$1" ]; then
    echo "$0: called with no interface" 1>&2
    exit 1;
fi
        eth0="eth0"
if [ "$1" != "$eth0" ]; then
        exit 0;
fi


# Run the right scripts
case "$2" in
    up|vpn-up)
        ip address add 10.1.1.150/24 brd + dev eth0
	ip address add 192.168.2.2/24 brd + dev eth0
        ;;
    down|vpn-down)
	ip address delete 10.1.1.150/24 dev eth0
	ip address delete 192.168.2.2/24 dev eth0
        ;;
    hostname|dhcp4-change|dhcp6-change)
        exit 0
        ;;
    *)
        echo "$0: called with unknown action \`$2'" 1>&2
        exit 1
        ;;
esac