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.
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!
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.
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.
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.
mac-mini:~ ladmin$ networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
*Bluetooth PAN 2
mac-mini:~ ladmin$ networksetup -getinfo LAN
IP address: 192.168.15.2
Subnet mask: 255.255.255.0
IPv6 IP address: none
IPv6 Router: none
Ethernet Address: 00:1f:5b:33:1d:75
mac-mini:~ ladmin$ sudo networksetup -setadditionalroutes LAN 10.0.0.0 255.255.255.0 192.168.15.254
mac-mini:~ ladmin$ networksetup -getadditionalroutes LAN
10.0.0.0 255.255.255.0 192.168.15.254
mac-mini:~ ladmin$ netstat -rn
Destination Gateway Flags Refs Use Netif Expire
default 18.104.22.168 UGSc 18 0 vlan0
default 192.168.15.254 UGScI 0 0 en0
default 192.168.15.254 UGScI 0 0 en1
default 192.168.15.254 UGScI 0 0 en2
10/24 192.168.15.254 UGSc 0 0 en1
127 127.0.0.1 UCS 0 0 lo0
127.0.0.1 127.0.0.1 UH 75 2330825 lo0
169.254 link#8 UCS 1 0 vlan0
169.254 link#4 UCSI 0 0 en0
169.254 link#5 UCSI 0 0 en1
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.