Persistent loopback interfaces in Mac OS X

One of the things that I miss in Mac OS X is support for multiple loopback addresses. Not just 127.0.0.1, but anything in the form 127.* (e.g. 127.0.1.1 or 127.0.0.2).

To add an additional IPv4 address to the loopback interface, one can use the following command in a Mac OS X terminal:

$ sudo ifconfig lo0 alias 127.0.1.1

Problems is that this doesn’t persist across reboots. To make it persist across reboots, one can create a “launchd” daemon that configures this additional IPv4 address. Something like this:

$ cat << EOF | sudo tee -a /Library/LaunchDaemons/com.felipe-alfaro.loopback1.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.felipe-alfaro.loopback1</string>
    <key>ProgramArguments</key>
    <array>
        <string>/sbin/ifconfig</string>
        <string>lo0</string>
        <string>alias</string>
        <string>127.0.1.1</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>
EOF

Then, start the service up:

$ sudo launchctl load /Library/LaunchDaemons/com.felipe-alfaro.loopback1.plist

And make sure it did work:

$ sudo launchctl list | grep com.felipe-alfaro
-   0   com.felipe-alfaro.loopback1
$ ifconfig lo0
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
    inet 127.0.0.1 netmask 0xff000000 
    inet6 ::1 prefixlen 128 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    inet 127.0.1.1 netmask 0xff000000 
    nd6 options=201<PERFORMNUD,DAD>

And, for the record, it is totally possible to have multiple services like the one before, each one for every additional IPv4 address. Just make sure to name the .plist files differently as well as the services name in the Label tag.

Advertisements

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