based on @laurent anwser and this article:
- Add localhost alias script:
$ sudo touch /usr/local/bin/localhost_alias
$ sudo chmod +x /usr/local/bin/localhost_alias
- 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
- add autorun daemon description:
$ sudo touch /Library/LaunchDaemons/org.localhost.alias.plist
- 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>
- 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