Persistent Static routes - macOS High Sierra


I've got a mac at work. It's connected to two physically separate networks, one of which has a default route, but the other needs routes when the adapter is plugged in.

Surprisingly, there's little information on this.


It's actually pretty simple. The command is as follows:

sudo networksetup -setadditionalroutes <adapter name> <network addr> <netmask> <gateway>

The <adapter name> section is a little strange, but this is easy as well. To obtain it (or change it to something easy) navigate to System Preferences -> Network -> Click the adapter on the left -> Gear icon (bottom of the list of adapters) -> Rename service. This allows you to either copy the existing name, or rename it!

If you want additional routes, you CANNOT simply run multiple instances of that command as it overwrites the entries each time, you must bundle all routes in like so:

sudo networksetup -setadditionalroutes <adapter name> <network addr 1> <netmask 1> <gateway> <network addr 2> <netmask 2> <gateway> <network addr 3> <netmask 3> <gateway>

These routes appear to be persistent on reboot - although I did have an issue of this command not working at all until I rebooted! (But my machine takes a lot of abuse).

It's worth noting that you can verify the routing table with netstat -rn to find the routing entries have actually gone in.

If you have a lot of networks you can use standard bash backslash format to separate the lines like so:

sudo networksetup -setadditionalroutes LAB \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \

That's it! Let's hope it doesn't change too much in future...