Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automatically set up IP address aliases Network
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.

Now, create another file, StartupParameters.plist:
{
  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.

Now, make those two files user-executable:

$ 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.
    •    
  • Currently 3.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[18,208 views]  

Automatically set up IP address aliases | 8 comments | Create New Account
Click here to return to the 'Automatically set up IP address aliases' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatically set up IP address aliases
Authored by: smkaiser on Jan 25, '05 12:08:21PM
A much, much easier way to do this is to duplicate an interface in System Preferences, and then change the settings for the new interface(s) you add.
KB Articles 107322 and 106800 detail how to do it.

[ Reply to This | # ]
Single Link Multi-Homing
Authored by: morphis on Jan 25, '05 12:37:12PM

I to use the this method so I can still admin/access my Public wireless wan(10.xxx.xxx.xxx) and keep it separate from my local lan (192.168.xxx.xxx).

No need to make custom startup items.
Just Goto. System Preferences -> Network -> Network Port Configurations
And Go <New> give a name and select your (Ethernet) port.
Then just go set it up.

BTW this is called multi-homing. You've been able to do this since OpenTransport 1.3 in MacOS 8.1 though it was not quite as easy.



[ Reply to This | # ]
Agreed, System Prefs are the way to go
Authored by: lullabud on Jan 25, '05 12:42:42PM

If you do it the system preferences way it not only takes about 30 seconds, it gives you a network Location profile which you can use to easily reconfigure your network simply by clicking on Apple-menu -> Location -> [Profile name].

I use this at work to access printers that are physically on the same network as our public network, but are on a private static subnet. It works like a charm.



[ Reply to This | # ]
Agreed, again
Authored by: ghay on Jan 25, '05 12:57:58PM

I've never had any problems with this method.



[ Reply to This | # ]
Automatically set up IP address aliases
Authored by: Doug Fields on Jan 25, '05 05:22:29PM

The good thing about the above method is that it works headlessly. You don't even need VNC or anything. Now if everything on the Mac Mini could be done headlessly, I would love it, because this one (actually, all of mine) is going to be a headless server.

Thanks for the suggestions.



[ Reply to This | # ]
Automatically set up IP address aliases
Authored by: webbix on Jan 25, '05 07:32:11PM

Not certain but I thin I read the hint here about changing network locations from the terminal which could be done via ssh. I assume you should also be able to use the terminal to create locations in the appropriate OS X XML file.

It may require an initial connection with VNC or attaching a terminal but that should be the only time needed.



[ Reply to This | # ]
Airport and Error running StartupItem
Authored by: leono on Feb 01, '05 08:51:27AM

Hi, all. I agree that the System Prefs method is much easier, but it doesn't work if you're using an Airport connection. For some reason, it won't let you duplicate the Airport interface.

I wrote the scripts, changed the interface to en1 for Airport, set the perms, and tried to start it with SystemStarter. When I do, I get the following output:

Gooch:/Library/StartupItems/NetworkAliases leon$ sudo systemstarter start NetworkAliases
Welcome to Macintosh.
Initializing network
Exec failed for item /Library/StartupItems/NetworkAliases: No such file or directory
Network IP Address Aliases (4662) did not complete successfully.
The following StartupItems failed to properly start:
/Library/StartupItems/NetworkAliases - execution of Startup script failed
Startup complete.
Hangup

Anyone have an idea of how to resolve this? Thanks!



[ Reply to This | # ]
Airport and Error running StartupItem
Authored by: Doug Fields on Feb 09, '05 03:39:17PM

Give us an ls -l of your NetworkAliases directory.

You should make the script executable:

chmod +x <filename>

I may have omitted that from the discussion above.



[ Reply to This | # ]