Sh*t my brain says and forgets about

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. 🙂

Previous

How Evernote Won My Time Warner Battle

Next

iPhone Emoji in Adium

13 Comments

  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

  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.

  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.

  4. Aaron

    Good one Jeff! I’ll have to give that a try!

  5. bertillus

    Jeff’s solution works™ for me, even on my latest Yosemite 10.10.1 install.

  6. Tantuss

    This is probably the best way to make it persistent:
    http://www.spanders.com/osx-loopback/

  7. I try this command in terminal ( in 10.10.5 and 10.11 ) Im administrator on mac

    ifconfig lo0 alias 127.0.0.2
    I get error: ifconfig: ioctl (SIOCAIFADDR): permission denied

    What I’m doing wrong or missing
    Thank you

  8. 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)

  9. See my blog post https://blog.felipe-alfaro.com/2017/03/22/persistent-loopback-interfaces-in-mac-os-x/ on how to make the loopback address(es) persistent.

  10. 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

  11. Martin Gansler

    As this is the first entry on google, I found a working solution for Sierra which looks very clean:
    https://gist.github.com/brandt/c2f9e8277c90a1c284770c7ca7966226

Leave a Reply to Trevor Cancel reply

Powered by WordPress & Theme by Anders Norén

%d bloggers like this: