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

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:


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


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

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

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:)
if [ "$RET" -ne 0 ] ; then
MSG="$RET (error)"
MSG="0 (OK)"
ConsoleMessage "IPAliases: ifconfig $INTERFACE alias $IPADDR $NETMASK / returned $MSG"

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

Aucun commentaire: