Persistent static routes on OS X

Preface

A while ago I wrote a post about using launchd to configure static routes on OS X. This works fine except at boot time.  Just when I need it the most.

Second attempt

Giving up is easy. And I figured that there had to be a way to configure persistent static routes. This time the approach is using the command networksetup to do the job. It is not the easiest but it does the job. Routes configured on my server have survived a reboot!

Networksetup

Is a command with many options. The option that does it is sudo networksetup -setadditionalroutes. It requires options such as the network, network mask and router to reach this network. It also requires the name of a networkservice. And this is where things become slightly more difficult.

Networkservice list

A service is a name assigned to a network interface. Using the low level network names is not possible unfortunate. Time to figure out how to retrieve the names of the services. This is done with the command networksetup -listallnetworkservices. Now we have the names of the services. Time to inspect them further and try to find the service that we want to use for our persistent route.

Service details

The command networksetup -info <networkservice> will give us detailed information.  Just go through the list shown above until we find the service we’re after.

Adding persistent static routes

Once we have the network service identified we can add a static route with the command networksetup -setadditionalroutes <networkservice> [ <dest> <mask> <gateway> ]

Check the result with networksetup -getaddtionalroutes

Putting it all together

I want my server to be able to reach the network 10.0.0.0/24 via gateway 192.168.15.254. First I’m looking for a network service that connects to the network 192.168.15.0/24. As a result I learn that the network service name is ‘LAN’. Now I can add the route to this network service and check the result.

 

 

 

Disclaimer: All information posted is written with the upmost care and valid at the time of writing. Changes in versions can supersede the provided information. Please use your own judgement.