How do I add a static route (permanent) to Yosemite?

In previous Mac OS X releases, I was able to add a permanent static route to my system, following this article:

http://nellen.it/blog/2012/01/permanent-static-routes-for-mac-os-x/


========

Based on some research, here’s a way to add permanent static routes to Mac OS X Lion (and problably other OS Xs too).

The principe is that we create a bash script which is run on every boot and sets static routes.


First of all, you have to be a local admin.

Open a terminal and change to the StartupItems directory

cd /Library/StartupItems

Then create a new directory with root priviledges, e.g. AddRoutes and change into it

sudo mkdir ./AddRoutes cd ./AddRoutes

Now you have to create and edit the bash script

sudo touch ./AddRoutes sudo nano ./AddRoutes

Paste the following Lines into the bash script and adapt it for your network

#!/bin/sh # Set static routing tables . /etc/rc.common StartService () { sleep 10 ConsoleMessage "Adding Static Routing Table" sudo /sbin/route add -net 10.0.0.0 -netmask 255.0.0.0 -gateway w.x.y.z } StopService () { return 0 } StopService () { return 0 } RestartService () { return 0 } RunService "$1"

When you finished with the script, save it.

Then create the corresponding plist.

sudo touch StartupParameters.plist sudo nano StartupParameters.plist

When you created the plist file, paste the following lines into it.

{ Description = "Add static routing tables"; Provides = ("AddRoutes"); Requires = ("Network"); OrderPreference = "None"; }

When you finished the plist file, save it.
Finally we have to set the access rights

sudo chmod 755 /Library/StartupItems/AddRoutes/*

At the end we have to reboot and now your permanent static routes are set.

You can verify with the following

netstat -nr



============


But now with Yosemite, this doesn't working. Especially the point when executed this line in the :


RunService "$1"

in the startup script. Something has been changed in the latest OS. Any ideas, how to solve this OR another way to add a permanent static route?

Mac Pro, OS X Yosemite (10.10)

Posted on Oct 30, 2014 11:33 AM

Reply
7 replies

Oct 30, 2014 1:48 PM in response to disguise

In a nutshell... I'd create a LaunchDaemon plist file that invokes a shell script containing


your list of "sudo /sbin/route add " commands. I'd place this plist file in


/Library/LaunchDaemons


then, I'd either reboot to test or run


sudo launchctl load


command to tell launchd about the new plist file


If all this seems familiar, great - if not you have some reading to do.

Nov 20, 2015 4:57 PM in response to CGK_sys

Copy this file to /Library/LaunchDaemons/local.username.routes.plist

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>Label</key>

<string>local.username.routes</string>

<key>RunAtLoad</key>

<true/>

<key>ProgramArguments</key>

<array>

<string>/sbin/route</string>

<string>-n</string>

<string>add</string>

<string>10.1.1.0/24</string>

<string>192.168.1.1</string>

</array>

</dict>

</plist>


The 10.1.1.0/24 is the network you want routed, and the 192.168.1.1 is the gateway you will use (usually a second NIC without an existing gateway).


Load the new plist.

$ sudo launchctl load local.username.routes


Start the new plist without rebooting.

$ sudo launchctl start local.username.routes


Prove your normal traffic is unaffected (should return your normal gateway).

$ route get 8.8.8.8


Prove your static route is working (should return the gateway you entered above).

$ route get 10.1.1.100

Dec 3, 2015 9:57 AM in response to jnovack

So, I'm in the situation where I would like the route to be present the instant the user logs in. I have my plist file:


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>Label</key>

<string>static.route</string>

<key>ProgramArguments</key>

<array>

<string>route</string>

<string>add</string>

<string>-host</string>

<string>10.7.0.21</string>

<string>10.6.0.254</string>

</array>

<key>RunAtLoad</key>

<true />

<key>ServiceIPC</key>

<false />

</dict>

</plist>


and I put it in /Library/LaunchDaemons and rebooted. I sat at the machine, and the moment the login window appears, I logged in, opened a terminal, issued the netstat -nr command and did not see the route. I repeatedly issued netstat -nr and waited about a minute before the route appeared.


So I tried moving it to /Library/LaunchAgents, thinking that maybe it would load quicker... and it does load a little quicker, but not significantly quicker.


I need this route to be active the INSTANT a user logs on. And the user may not be an admin, so a login script with sudo won't work. Any ideas on how to flag the script as high-priority and get it to execute in a more timely fashion?


Thanks,

Paul

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do I add a static route (permanent) to Yosemite?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.