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. 🙂
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
LikeLiked by 1 person
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.
LikeLike
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.
LikeLike
Good one Jeff! I’ll have to give that a try!
LikeLike
Jeff’s solution works™ for me, even on my latest Yosemite 10.10.1 install.
LikeLike
This is probably the best way to make it persistent:
http://www.spanders.com/osx-loopback/
LikeLiked by 1 person
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
LikeLike
prefix with sudo
LikeLike
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)
LikeLike
try with sudo:
sudo ifconfig lo0 alias 127.0.0.2 up
LikeLike
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.
LikeLike
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
LikeLike
As this is the first entry on google, I found a working solution for Sierra which looks very clean:
Loopback Alias
Creates an alias on the loopback interface (lo0) with the IP 127.0.0.2 on macOS.
Installation
/Library/LaunchDaemons/com.runlevel1.lo0.127.0.0.2.plist
sudo chmod 0644 /Library/LaunchDaemons/com.runlevel1.lo0.127.0.0.2.plist
sudo chown root:wheel /Library/LaunchDaemons/com.runlevel1.lo0.127.0.0.2.plist
sudo launchctl load /Library/LaunchDaemons/com.runlevel1.lo0.127.0.0.2.plist
The alias will automatically be created at startup from then on.
You can confirm the alias was created with
ifconfig
:README.md
hosted with ❤ by GitHub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
com.runlevel1.lo0.127.0.0.2.plist
hosted with ❤ by GitHub
LikeLiked by 1 person