How do you get loopback addresses other than 127.0.0.1 to work on OS X

1. Add localhost alias script:

$ sudo touch /usr/local/bin/localhost_alias
$ sudo chmod +x /usr/local/bin/localhost_alias

2. localhost_alias content:

#!/usr/bin/env bash
 
from=${1}
to=${2}
 
if [[ -z "$from" |]]; 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
    sudo ifconfig lo0 alias 127.0.0.$i up
done

3. add autorun daemon description:

$ sudo touch /Library/LaunchDaemons/org.localhost.alias.plist

4. 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

*Tested on MacOS Mojave.
** You must allow to run sudo script-name without password in sudoers file.

sudo touch /private/etc/sudoers.d/localalias
sudo vi /private/etc/sudoers.d/localalias
 
## Added localhost alias\\
ALL ALL=NOPASSWD: /usr/local/bin/localhost_alias
 
sudo chmod 440 /private/etc/sudoers.d/localalias