Permanently create an ifconfig loopback alias — macOS

A couple of days ago I’ve booted up my mac and was just about to start working when I noticed non of my docker containers was working properly.
I scratched my head for a few minutes before I realised that, yet again, I forgot to set up an alias to lo0 device…

I’ve decided it’s about time to stop being lazy and make this permanent, but how? adding an alias to lo0 requires root permissions

sudo ifconfig lo0 alias someIp

Being new to mac I was not sure what would be the best way to do this, editing the hosts file? creating a SUID file ?

After searching a bit I found the answer in appleDocs, it seems we can just create launch demon.

In a nutshell a launch demon is a process managed by the launchd service, we can create one to run once, continuously or periodically.

So, how do we utilise this to create an ifconfig alias?
Just create a new launch demon file and configure it like so:

sudo vi /Library/LaunchDaemons/org.my.ifconfig.plist
<?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.my.ifconfig</string>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/sbin/ifconfig</string>
<string>lo0</string>
<string>alias</string>
<string>YourIpHere</string>
</array>
</dict>
</plist>

Make sure to insert the desired ip in the last string argument.

And that’s it! on your next restart you should be set to go.

For more information about launch demons and how to configure them go to appleDocs.