With the arrival yesterday of my Mac mini, I wanted to configure it to take the place of one of my trusty Debian servers. This server uses several IP addresses which I needed the Mac mini to configure automatically at startup. After much research, I could find nothing on automatically setting several IP address aliases in Mac OS X 10.3 non-Server. As such, I created a special Startup Item to take care of this. My sample code here sets up two IP address aliases on the en0 (usually wired Ethernet) interface.
There are several steps; these are all easiest to do if you're simply the root user the whole time (use sudo su to become root in a Terminal window). Read the rest of the hint for the steps...
First, create the directory /Library/StartupItems/NetworkAliases, and go there:
$ mkdir /Library/StartupItems/NetworkAliases
$ cd /Library/StartupItems/NetworkAliases
Then, we'll have to create two files which do the brunt of the work. Create a file called NetworkAliases (identically named as the directory), containing this text:
#!/bin/sh
#
# Douglas Fields, 1/21/2005
#
# Shell script to set IP address aliases on the Ethernet when safe.
#
# TODO: Just have a configuration file with the necessary aliases
# and what interface to use, and write a script which will remove
# all aliases whatever they are from the setting, even if they
# changed in the file (possibly store a "current aliases file"
# somewhere).
#
# SystemStarter start NetworkAliases
# SystemStarter restart NetworkAliases
# SystemStarter stop NetworkAliases
# Load defaults
. /etc/rc.common
if [ ${1:-noset} == "stop" ] || [ ${1:-noset} == "restart" ]; then
ConsoleMessage "Removing IP Address Aliases"
/sbin/ifconfig en0 -alias 192.168.1.15 netmask 255.255.255.0
/sbin/ifconfig en0 -alias 192.168.1.16 netmask 255.255.255.0
if [ ${1:-noset} = "stop" ]; then
exit 0
fi
fi
# Otherwise, we're either a "start" or a "restart"
ConsoleMessage "Setting IP Address Aliases"
/sbin/ifconfig en0 alias 192.168.1.15 netmask 255.255.255.0
/sbin/ifconfig en0 alias 192.168.1.16 netmask 255.255.255.0
exit 0
The key lines here are the ifconfig lines, which specify which IP addresses (and netmasks) to use. These commands are well documented in the man page if you wish more detail. Add as many as you like, but just make sure the top and bottom agree, differing only in the alias vs. -alias part.
{
Description = "Network IP Address Aliases";
Provides = ("NetworkAliases");
Requires = ("Network");
OrderPreference = "None";
Messages = {
restart = "Resetting IP Address Aliases";
start = "Setting IP Address Aliases";
stop = "Removing IP Address Aliases";
};
}
This file tells the system what our Startup Item is called and what it relies upon. In short, it says that it's called NetworkAliases and must run after Network which, of course, is the System startup item (in /System/Library/StartupItems, but probably deprecated with 10.3's new, poorly documented, mach_init mechanism) which sets up the Network.
$ chmod u+x *
Last step is for localization (which strictly is not necessary if you don't really care, but is included for completeness). Create a subdirectory structure for English:
$ mkdir -p Resources/English.lproj
And, in there, put a file called Localizable.strings, which the ConsoleMessage command can use to localize things:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>Removing IP Address Aliases</key>
<string>Removing IP Address Aliases</string>
<key>Setting IP Address Aliases</key>
<string>Setting IP Address Aliases</string>
</dict>
</plist>
And, now you're done. To test things out, you can start and stop your aliases with:
$ SystemStarter start NetworkAliases
$ SystemStarter restart NetworkAliases
$ SystemStarter stop NetworkAliases
You can run those with sudo if you're not root. When you're happy, reboot, and voila, your extra IP addresses will show up automatically at boot time.
Mac OS X Hints
http://hints.macworld.com/article.php?story=2005012202111996