Configuring IP aliasing with Mac OS X

For some reason, I want to set up some alternate IP addresses on my MacBook, that runs Mac OS X. Basically, to set up such an address on Mac OS X, you run something like :

ifconfig en0 alias 10.10.0.1 255.255.255.0

This is easy to do it this way, but this is pretty volative : when you reboot your machine, the alias will not be there anymore. Basically, it seems there is no way to keep beyond the reboots ... at least with the workstation version of the operating system. I’ve read on some site (sorry, I didn’t keep it in my bookmark) that Mac OS X Server has such a process, with the script /System/Library/StartupItems/IPAliases/IPAliases, that reads the configuration file /etc/IPAliases.conf. It will the following variable is defined in /etc/hostconfig:

IPALIASES=-YES-

The format of the configuration file is available online at Apple’s developper site.

Actually, I was suprised to see an Apple’ support site article telling you how to set IP aliasing creating the script /Library/StartupItems/IPAliases/IPAliases and the corresponding plist file /Library/StartupItems/IPAliases/StartupParameters.plist. This is just a script that does not use any configuration. Well, this article is about Mac OS X 10.0. At least since 10.3 (I’ve learned the system with this version), you are not supposed to modify anything in /System so I consider that /Library/StartupItems better fits.

Finally, I decided to write the script /Library/StartupItems/IPAliases/IPAliases to reproduce Mac OS X Server’s one behaviour. It is pretty very simple and has nothing magic. Just that it is useful. Notably, I am not sure using a configuration variable put in /etc/hostconfig is «the way» to do with recent version of Mac OS X (Leopard by now). Here it goes:

#! /bin/sh

. /etc/rc.common

CONF=/etc/IPAliases.conf

if [ ! "${IPALIASES:=-NO-}" = "-YES-" ] ; then
exit 0
fi

if [ ! -f "$CONF" ] ; then
ConsoleMessage "File $CONF does not exist. No IP aliases to define."
exit 0
fi

cat $CONF | grep -vE '^[[:space:]]*(#.*)? | while read ALIAS ; do
INTERFACE=$(echo $ALIAS | cut -f1 -d:)
IPADDR=$(echo $ALIAS | cut -f2 -d:)
NETMASK=$(echo $ALIAS | cut -f3 -d:)
NETMASK=${NETMASK:+netmask $NETMASK}
ifconfig $INTERFACE alias $IPADDR $NETMASK
RET=$?
if [ "$RET" -ne 0 ] ; then
MSG="$RET (error)"
else
MSG="0 (OK)"
fi
ConsoleMessage "IPAliases: ifconfig $INTERFACE alias $IPADDR $NETMASK / returned $MSG"
done

Then the plist file /Library/StartupItems/IPAliases/StartupParameters.plist that goes with it:

{
Description = "IP aliasing startup";
Provides = ("IPAliases");
Requires = ("Network");
OrderPreference = "None";
Messages =
{
start = "Starting IP aliasing";
stop = "Stopping IP aliasing";
};
}

It reads a configuration file named /etc/IPAliases. This is sample:

# IPAliases.conf
#
en0:10.1.0.1/24
en0:10.2.0.1/24

Aucun commentaire: