Mac OS X – Adding a loopback alias

I do a lot of local web development on my MacBook Pro.  Frequently I had multiple tiers of servers running – a Jetty instance running the web tier and a JBoss/EJB server doing the business tier behind it.  The problem is JBoss opens up so many ports on a particular network adapter and trying to get JBoss and Jetty to share a single IP is a nightmare.  So the easier way is to just create a new IP or alias your localhost (127.0.0.1) into something like 127.0.0.2.  When you start up Jetty, you pass in the binding IP of .2 and then JBoss and Jetty place nice with each other.

The Mac OS command (at least 10.6) to create an alias is:

ifconfig lo0 alias 127.0.0.2

and to delete:

ifconfig lo0 -alias 127.0.0.2

This is not persistent – the alias will not survive a reboot.  I don’t need the alias often even to require it to be permanent.  If you’re curious how to keep it permanent, let me know and I’ll post that.  This is the hacker way to do it on the command line.  There is a much easier way to do it through the GUI but this way doesn’t stick around on the reboot, which is one of my requirements for my project – no retaining traces. 🙂

13 thoughts on “Mac OS X – Adding a loopback alias

  1. MagicMeta

    How do you make this persistent? Is it also possible to create a range of alias IP addresses for the loopback? For example 127.0.x.x, where it may be any of 127.0.0.2, 127.0.0.3, 127.0.1.0, 127.0.1.1, 127.0.1.2, 127.0.1.3, 127.0.2.0, 127.0.2.1 and so on…

    Regards,
    MM

    Like

  2. Astralbodies

    I’ve been investigating this and there is no real easy answer. Mac OS X Server has an IPAliases feature which the desktop version does not have built in. There is an example script that someone built that approximates the feature. BSD Linux, which Mac OS X is based on, would use /etc/rc.conf to set up the interfaces but with the transition to launchd, that configuration is no longer used. My suggestion is to create an Automator action that executes upon login – assuming of course you can wait until login time for the aliases to be created.

    I think you can do a wildcard alias but I don’t know off hand. I’ll verify and do another post.

    Like

  3. Jeff

    One answer to the persistence question may be to use a Login Hook:
    http://support.apple.com/kb/HT2420

    I created a script called loopback_alias with the following:

    #!/bin/sh
    ifconfig lo0 alias 127.0.0.2

    (You could add more aliases on additional lines.)

    Then I just followed the directions on the page linked to above (made the script executable and ran the command they show).

    Hope this helps others.

    Like

  4. Or in same time if i type not copy
    i get respond this way
    (Mac 10.10.x and same 10.11)
    ipconfig lo0 alias 127.0.0.2
    usage: ipconfig
    where is one of waitall, getifaddr, ifcount, getoption, getpacket, getv6packet, set, setverbose

    Why I can’t create alias ( I try to setup web design test on ip 127.0.0.2 – 127.0.0.5)
    I edit and update /etc/hosts (with BBEdit)

    Like

  5. I needed to add another IPv6 address to the loopback interface, and my guess, based on the instructions in the blog post, seemed to work OK: sudo ifconfig lo0 inet6 alias ::6

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s