82

According to everything I read, the entire 127.x.x.x subnet should loopback.

However, on my Mac, I can only ping 127.0.0.1

I know I've done this before (though possibly on another OS) and has come in very useful for developing multiple SSL sites locally and for tunneling remote services for access on a local IP (for example I could ssh into my MySQL server, and just port forward the standard port to the same port on my local machine but on 127.0.0.2 while my local server ran at 127.0.0.1.

3 Answers 3

113
+50

Here is the short answer: sudo ifconfig lo0 alias 127.0.0.* up

Each alias must be added individually (sudo ifconfig lo0 alias 127.0.0.2 up, sudo ifconfig lo0 alias 127.0.0.3 up). It can be done manually for testing, or a subset or the complete list of the other 250 available numbers in that subnet can be made into StartupItems script that will do it automagically at boot time.

The long answer: According to RFC3330, 127.0.0.0/8 - This block is assigned for use as the Internet host loopback address. A datagram sent by a higher level protocol to an address anywhere within this block should loop back inside the host. This is ordinarily implemented using only 127.0.0.1/32 for loopback, but no addresses within this block should ever appear on any network anywhere.

4
  • 7
    If only there was a way to make it work for 127.*.*.* Commented Dec 12, 2012 at 4:27
  • If this turns out to also work on CentOS, you sir are getting a bounty from me. Commented Jul 18, 2014 at 19:33
  • Actually, either way you get a bounty. Since it doesn't work for CentOS, I'll just ask and answer that question once I find out the answer. Commented Jul 18, 2014 at 19:42
  • How I set up 4 servers on port 8, using different loopback addresses on debian9 Gnu/Linux superuser.com/a/1255308/62123 Commented Oct 2, 2017 at 8:31
22

For those interested, here is a little bash script that adds all the aliases for the IPs 127.0.0.*:

#!/bin/bash
for ((i=2;i<256;i++))
do
    sudo ifconfig lo0 alias 127.0.0.$i up
done
5
  • Am I reading this wrong? What you created looks like it does all the addresses from 127.0.0.1 to 127.0.0.255. If you change "sudo ifconfig lo0 alias 127.0.0.$i up" to "sudo ifconfig lo0 alias 127.$i.$i.$i up" that'll get you most of them right? However you would skip anything that is less than 2.. So maybe use a different variable for the other two octets? The highest IP address for loopback is 127.255.255.255
    – Everett
    Commented Dec 18, 2015 at 14:45
  • 1
    @Everett, you read this correctly, it only does the addresses 127.0.0.*. The reason I didn't cover the whole range is that it is actually quite slow to process even 255 addresses, so 255x255x255 would take way too long.
    – laurent
    Commented Dec 18, 2015 at 14:47
  • 1
    Thanks. I just wanted to make sure that the expectation was set, because someone will come on here and say, "Hey, it didn't work for 127.7.53.91." I wouldn't do that, but someone will...
    – Everett
    Commented Dec 18, 2015 at 14:49
  • 2
    Actually, @Everett's proposal wouldn't work either, since it would only enable adresses such as 127.7.7.7 or 127.42.42.42, that is all those with the three last digits are equals. You would need three distinct loops to actually enable all IPs in the 127/8 block. Yet doing this would certainly not be a good idea: it is highly probable, given that each IPs has to be enabled separately, that there are some resources attached to each assigned IP; allocating approximately 17.7 millions IPs (that is 256^3) might turn out to have some significant consequences...
    – James
    Commented May 16, 2016 at 16:49
  • 2
    @jwatkins Bottom line: Enable them only if you need them, which is a rule of thumb for everything, really. Commented Dec 17, 2016 at 3:20
7

based on @laurent anwser and this article:

  1. Add localhost alias script:
$ sudo touch /usr/local/bin/localhost_alias
$ sudo chmod +x /usr/local/bin/localhost_alias
  1. localhost_alias content:
#!/usr/bin/env bash

from=${1}
to=${2}

if [[ -z "$from" || -z "$to" ]]; then
  echo "Usage: $(basename "$0") 2-255 2-255 [from and to range numbers (127.0.0.[from-to])]"
  exit 0
fi 

for ((i=from;i<=to;i++))
do
    ifconfig lo0 alias 127.0.0.$i up
done
  1. add autorun daemon description:
$ sudo touch /Library/LaunchDaemons/org.localhost.alias.plist
  1. org.localhost.alias.plist content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>org.localhost.alias</string>
    <key>RunAtLoad</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/bin/localhost_alias</string>
      <string>2</string>
      <string>8</string>
    </array>
</dict>
</plist>
  1. And we have 127.0.0.2 - 127.0.0.8 aliases at boot

You can have launchd execute the daemon after creating these files, without a reboot, by executing

sudo launchctl load /Library/LaunchDaemons/org.localhost.alias.plist

Tested on MacOS Mojave

5
  • Since this is a /Library/LaunchDaemons definition, the script is run as root (see launchd.info), so there is no need to use sudo in the /usr/local/bin/localhost_alias script. With no sudo, there is no need configure sudoers either. Commented Jan 11, 2023 at 18:45
  • Also, you may want to add that you can immediately load the definition by running sudo launchctl load /Library/LaunchDaemons/org.localhost.alias.plist. Commented Jan 11, 2023 at 18:52
  • @MartijnPieters thank you for comments. Before approving edit I want to check, did you test non-sudo behavior?
    – rzlvmp
    Commented Jan 12, 2023 at 3:10
  • Yes, I did, I have this running on my laptop under macos Monterey Commented Jan 12, 2023 at 11:00
  • 1
    It looks like I forgot to include sudo in the sudo launchctl load /Library/LaunchDaemons/org.localhost.alias.plist command when I edited. My apologies. You do need sudo there as LaunchDaemons are only loaded in the root launchd. Commented Jan 12, 2023 at 13:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .