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.

1 thought on “Persistent static routes on OS X”

  1. Yeah! it works 🙂 Thank you very much!I only want to add that if you want to add several routes, you have to do it this way. For example I have routed every address into the private address space:iso@isoAir:/devsudo networksetup -setadditionalroutes “Ethernet Pantalla Trabajo” 10.0.0.0 255.0.0.0 10.1.36.1 172.16.0.0 255.240.0.0 10.1.36.1 192.168.0.0 255.255.0.0 10.1.36.1Doing that, I have finally three routes with the get command:iso@isoAir:/dev$ sudo networksetup -getadditionalroutes “Ethernet Pantalla Trabajo”
    10.0.0.0 255.0.0.0 10.1.36.1
    172.16.0.0 255.240.0.0 10.1.36.1
    192.168.0.0 255.255.0.0 10.1.36.1 In the beginning I was trying to configure it line by line, and every route I add, deletes the previous one. Configuring everything on the same line worked well.I think those routes are permanently added to the interface, I have tried to disconnect and connect again my Thunderbolt Display and the routes are disappearing and appearing again when I plug the screen on.I was looking for a way to do that, because while I am at the office I need my routes configured to reach the internal network, but also I wanted to remove them when I am out using only wifi, or when I am at home connected to my homes Thunderbolt Display.It seems to work well, if I notice any problem or improvement to this method, I will share :)Thanks again!ISO

Comments are closed.