How-To: Add Static Route in Mac OS X

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

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


# Set static routing tables

. /etc/rc.common

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

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?

  • 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”

    It would make more sense to me if it were:

    “route -n add”

    • John

      You have to be root to change the routing tables, and you shouldn’t be running as root normally.
      If you are running a root shell then sudo is redundant, but does no harm. If you are (properly) running a user shell then the sudo is required.

  • 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

    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.

    • 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.

  • 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.

    This has been tested on multiple macs and works great!

  • Ju

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

  • Ju
  • Hi,

    I do all the things above and when my MacBook running on 10.10 Yosemite is rebooted, there was no static route setted. Need some help please.



Comments are closed.