How-To: Add Static Route in Mac OS X

And because i feel lucky today, (0n a Friday the 13th, that looks kind of odd), here’s another how-to. This is how I added a static route on some Xserves.

Simple solution: route -n add 10.0.0.0/8 10.16.3.254

Permanent solution: Add a startup item (to avoid doing the above command every reboot)

cd /System/Library/StartupItems
mkdir AddRoutes
cd AddRoutes

Create a file called AddRoutes (note: same as the folder name)
vi AddRoutes

——————
#!/bin/sh

# Set static routing tables

. /etc/rc.common

StartService ()
{
if [ "${ADDROUTES:=-NO-}" = "-YES-" ]; then
ConsoleMessage “Adding Static Routing Table”
sudo route -nv add 10.0.0.0/8 10.16.3.254
fi
}

StopService ()
{
return 0
}

StopService ()
{
return 0
}

RestartService ()
{
return 0
}

RunService “$1″
——————

Then create a file StartupParameters.plist
——————
{
Description = “Add static routing tables”;
Provides = (”AddRoutes”);
Requires = (”Network”);
OrderPreference = “None”;
}
—————-

Then change permissions:
chmod 755 AddRoutes StartupParameters.plist

Reboot your computer. Verify with netstat -nr

  • Jason

    This does not seem to work for non-server Mac OS X.
    I am running Mac OS X 10.6 – Snow Leopard.

    • jules

      i’m having the same problem on non-server mac os x.  did anyone figure this out?

  • http://link Gangster52

    I liked your blog its nice. ,

  • Greg Shenaut

    Why is there a sudo in that command? It seems wrong or at least unnecessary to me.

    “sudo route -nv add 10.0.0.0/8 10.16.3.254″

    It would make more sense to me if it were:

    “route -n add 10.0.0.0/8 10.16.3.254″

  • Malbrouck

    Thanks for the hint. I’m going to try it on my 10.6 server.

    Anyhow I’m still wondering why there’s not an easier way of adding a static network route on MacOSX. This is something quite common and easy to do on many systems.

  • UnixMonkey

    A few quick items regarding the above solution:

    1) You should put this in /Library/StartupItems instead of /System/Library/StartupItems. The /System folder is reserved and should not be muddled with. You can get the same performance by putting this in /Library instead.

    2) If you copy/paste the above, be sure to edit the fancy quotation marks out of both files as the system will barf. Make them all simple quotes (i.e.; ” ).

    3) If you really want a persistent route, remove the “if” logic which means that line plus the “fi” line. It’s unnecessary as you always want to do this.

    4) I changed the actual route command by adding a -net designator as I wanted to completely route 192.168 through my VPN connection. Here’s an example:
    sudo route -nv add -net 192.168.0.0/16 192.168.235.1

    Once those are complete, you should be good to go. Load up Console and check your system.log for entries by “SystemStarter” to confirm no additional issues.

    • http://irrashai.wordpress.com/ irrashai

      thanks for the input. much appreciated.

    • serbentraut

      Thanks for the input!

  • UnixMonkey

    The site converted by quotation mark into a fancy. We really need a code tag or something similar.

  • http://www.chimac.net chimac

    I tried this solution and it didn’t work for me. I found a solution though that did work and you can find it here.

    http://chimac.net/2010/04/27/how-to-add-a-static-route-permanently-in-snow-leopard-or-10-6/

    This has been tested on multiple macs and works great!

  • Ju

    This is all I have gotten to work on Snow Leopard (10.6):

    http://www.hanynet.com/routesplit/index.html

  • Ju