Crumpled Thoughts

August 24th, 2006

Persistent IP Routes in Mac OS X Tiger

Posted by Chris in Technology, Sysadmin

One of the clients I work for has a Mac OS X server which handles most of the file shares on the network. They have two internet connections, one on their T1, and a secondary DSL connection for redundancy. The Mac OS X server has two network interfaces; one with an internet IP (behind a BSD firewall) and another on the private network. The default gateway on the Mac server is on the internet interface, and a seperate router on the internal network routes traffic to 5 local IP subnets for remote offices, and co-located servers.

Since the default gateway points to the internet, routes have to be added for the four other local subnets to send traffic for those subnets to the router on the internal network. These routes need to survive a reboot— they need to be persistent.

On a Windows box it is simple to add a persistent IP route. Along with your ‘route add’ command, you add a ‘-p’ to make it persistent. Easy enough. Not so simple on Mac OS X. On Linux you could just add the ‘route add’ statements to the /etc/rc.local file and they would be executed at startup, effectively making them persistent. Easy enough. This is not so easy on Mac OS X… but I found a way to do it.

Let me add my standard disclaimer that you do this at your own risk. I won’t be held responsible for any trouble you experience trying to do this. It is working great for me though :)

Open up terminal, and switch to the root user:

sudo su -

You’ll have to type in your password, and have ‘Administer the Server’ rights.

Change to the /Library/StartupItems directory

cd /Library/StartupItems/

The way I created my script was by copying one that was created by a MySQL 4 installer. You should be able to copy any of the directories in /Library/StartupItems to give you a starting point. Just substitute the one you’re using where you see me use ‘MySQLCOM’.

cp -rp MySQLCOM PersistentRoutes

Now change to the PersistentRoutes directory.

cd PersistentRoutes

If you list the contents of this directory you should see two files, one named for the service you copied, and StartupParameters.plist. We need to rename the service you copied to ‘PersistentRoutes’

mv MySQLCOM PersistentRoutes

Now we edit PersistentRoutes with your favorite text editor… mine’s vi.

vi PersistentRoutes

Go ahead and empty the file. If you’re using vi type ‘1000dd’ (no quotes) and it will delete 1000 lines. That should empty it :) . This is the contents of my PersistentRoutes file:

#!/bin/sh
. /etc/rc.common
ConsoleMessage "Adding Persistent IP Routes"
/sbin/route add 10.0.0.0/24 10.1.2.1 #Route to Boise Colo Facility
/sbin/route add 10.1.3.0/24 10.1.2.1 #Route for SonicWall L2TP Group VPN
/sbin/route add 10.1.4.0/24 10.1.2.1 #Route to Portland Office
/sbin/route add 192.168.69.0/24 10.1.2.1 #Route to Chris' House 

Save, and exit your text editor. ‘:wq’ in vi (write, quit)

Now we need to edit the StartupParameters.plist. Mine looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC “-//Apple Computer//DTD PLIST 1.0//EN” 
“http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
        <key>Description</key>
        <string>Persistent Routes for VPN Tunnels</string>
        <key>OrderPreference</key>
        <string>Last</string>
        <key>Provides</key>
        <array>
                <string>PersistentRoutes</string>
        </array>
        <key>Uses</key>
        <array>
                <string>Network</string>
                <string>NetworkExtensions</string>
        </array>
        </dict>
</plist>

That’s it! Now your routes will be added when you reboot. Need to add a new route?.. manually add it from the terminal using ‘/sbin/route add…’ then update the /Library/StartupItems/PersistentRoutes/PersistentRoutes file. Easy!

Hope this helps someone. It was causing me a bit of aggravation. Perhaps someday Apple will make this a bit easier. Use ‘netstat -r’ to display your routing table.

Cheers,
Chris

7 Responses to ' Persistent IP Routes in Mac OS X Tiger '

Subscribe to comments with RSS or TrackBack to ' Persistent IP Routes in Mac OS X Tiger '.

  1. Andy said,

    on October 30th, 2006 at 3:38 pm

    Hi

    I just followed these instructions but didnt have any success. The routes I added in the script do not show up in netstar -r.

    Help!

    Andy

  2. Chris said,

    on October 30th, 2006 at 11:29 pm

    Hmm… if you haven’t already added the routes using /sbin/route then try running “sudo SystemStarter start PersistentRoutes” from the terminal. This should add the routes from your /Library/StartupItems/PersistentRoutes/PersistentRoutes file.

    I’ve got this running on Mac OS 10.4.8 server and it’s working well. If you add a new route to the /Library/StartupItems/PersistentRoutes/PersistentRoutes file it is not added until you run the SystemStarter command above, or add it manually in the terminal using “/sbin/route add network gateway” (substituting the italicized parts with the actual values).

    Hope this is helpful. You can find example files here to unpack in your /Library/StartupItems directory to use as a start.

    -Chris

  3. Andy said,

    on October 31st, 2006 at 7:33 am

    Thanks very much, that’s sorted it out.

    Andy

  4. Leo said,

    on December 6th, 2006 at 2:35 pm

    Great Job!!! Thanks for the Help

  5. Ravindra said,

    on December 12th, 2006 at 1:42 am

    Hello,

    I am using Mac OS X 10.3.9 Will the above procedure works on my machine?
    My ‘StartupParameters.plist’ file is different and it looks like

    {
    Description = “persistent routes”;
    Provides = “PersistentRoutes”;
    OrderPreference = “None”;
    }

    Any modifications..

    Regards,
    Ravindranadh

  6. Bolo said,

    on December 20th, 2006 at 9:40 am

    Hello

    It’s working for me BUT But i reboot my laptop Pesistent Routes is not launch :(
    I must launch the hack manully
    sudo SystemStarter start PersistentRoutes”

    When i did SystemStarter -dn I can see this error

    SystemStarter[277]: Failed requirement/uses: Disks
    SystemStarter[277]: Checking Persistent Routes for VPN Tunnels
    SystemStarter[277]: No antecedents
    SystemStarter[277]: Soft dependancies: {type = fixed-mutable, count = 0, capacity = 2, values = (

  7. Joe said,

    on April 10th, 2010 at 9:28 pm

    Thank you so much for this info. Worked great.

Leave a reply

:mrgreen: :neutral: :twisted: :shock: :smile: :???: :cool: :evil: :grin: :oops: :razz: :roll: :wink: :cry: :eek: :lol: :mad: :sad: