Skip to content

Instantly share code, notes, and snippets.

@brandt
Last active February 3, 2024 01:28
Show Gist options
  • Save brandt/c2f9e8277c90a1c284770c7ca7966226 to your computer and use it in GitHub Desktop.
Save brandt/c2f9e8277c90a1c284770c7ca7966226 to your computer and use it in GitHub Desktop.
Creates a loopback alias with IP 127.0.0.2 at startup on Mac OS X

Loopback Alias

Creates an alias on the loopback interface (lo0) with the IP 127.0.0.2 on macOS.

Installation

  1. Install the plist to: /Library/LaunchDaemons/com.runlevel1.lo0.127.0.0.2.plist
  2. Set mode: sudo chmod 0644 /Library/LaunchDaemons/com.runlevel1.lo0.127.0.0.2.plist
  3. Set owner: sudo chown root:wheel /Library/LaunchDaemons/com.runlevel1.lo0.127.0.0.2.plist
  4. Load: 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:

/tmp ❯❯❯ ifconfig lo0
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
       	options=3<RXCSUM,TXCSUM>
       	inet6 ::1 prefixlen 128
       	inet 127.0.0.1 netmask 0xff000000
       	inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
       	inet 127.0.0.2 netmask 0xff000000
       	nd6 options=1<PERFORMNUD>
<?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>com.runlevel1.lo0.127.0.0.2</string>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/sbin/ifconfig</string>
<string>lo0</string>
<string>alias</string>
<string>127.0.0.2</string>
</array>
<key>StandardErrorPath</key>
<string>/var/log/loopback-alias.log</string>
<key>StandardOutPath</key>
<string>/var/log/loopback-alias.log</string>
</dict>
</plist>
@atErik
Copy link

atErik commented Oct 18, 2017

under this line:              <string>127.0.0.2</string>
i added+used this one more line:      <string>up</string>

and, under these 2 lines:      <key>StandardOutPath</key>
                    <string>/var/log/loopback-alias.log</string>
i also added+used these 2 more lines:
     <key>UserName</key>
     <string>root</string>

to view list of all net-interfaces, in "Terminal" window use below command:
    /sbin/ifconfig  -a

it is better to ping a local net-interface IP-address, to find out if its working or not:
    /sbin/ping  127.0.0.2  -c  1

if displayed Property-List (plist) cannot start/load the alias based net-interface 127.0.0.2, then, in "Terminal" try below 2 commands:
    sudo  launchctl  unload  -w  /Library/LaunchDaemons/com.runlevel1.lo0.127.0.0.2.plist
    sudo  launchctl  load  -w  /Library/LaunchDaemons/com.runlevel1.lo0.127.0.0.2.plist

to stop the net-interface, in Terminal you can use below command:
    /sbin/ifconfig  lo0  inet  127.0.0.2  -alias
note: the "launchctl unload ..." command, will not stop the alias net-interface, so to stop it, use above command manually.

if net-interface is stopped, then to start the net-interface again, use below 2 commands:
    sudo  launchctl  unload  -w  /Library/LaunchDaemons/com.runlevel1.lo0.127.0.0.2.plist
    sudo  launchctl  load  -w  /Library/LaunchDaemons/com.runlevel1.lo0.127.0.0.2.plist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment