How do I add a permenant static route in 10.4?

If add a static route via the terminal using it the following command:

route add -net 10.100.0.0 10.1.2.254 255.255.0.0

But each time I restart the server the route disappears.

How do I add it permanently (With Windows I just use the -p flag) or make sure the command runs each time on startup?

Posted on Jul 7, 2005 8:27 AM

Reply
15 replies

Jul 29, 2005 12:30 AM in response to Humberto

Here's a sample script called 'StaticRoutes'. Save them both in /System/Library/StartupItems/StaticRoutes/ or /Library/StaticRoutes as you prefer

StaticRoutes:

#!/bin/sh
##
# Script to add static routes
#
##
. /etc/rc.common
StartService ()
{
ConsoleMessage "Configuring static routes"
if [ "${STATICROUTES:=-NO-}" = "-YES-" ]; then
# Here's the line that adds a route to the network "<network>" via the router address "<router>"
# Repeat for as many routes you want to add
# e.g. /sbin/route add -net 172.16 10.1.1.1
# to route to 172.16.x.x via 10.1.1.1
/sbin/route add -net <network> <router>
fi
}
StopService ()
{
# nothing to do here
return 0
}
RestartService ()
{
#nothing to do here, either
return 0
}
RunService "$1"



StartupParameters.plist

<?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>Set static routes to other networks</string>
<key>OrderPreference</key>
<string>None</string>
<key>Provides</key>
<array>
<string>StaticRoutes</string>
</array>
<key>Requires</key>
<array>
<string>Network</string>
</array>
</dict>
</plist>



Note that under 10.4 you should probably consider using a launchDaemon rather than a StartupItem, although the StartupItem will still work.

If using a startup item, this script also uses a line STATICROUTES=-YES- in /etc/hostconfig to decide whether or not to load the routes. You'll need to add this line in order for the script to work

Nov 9, 2005 9:50 PM in response to Gary Bidwell

Tiger is more exacting with permissions on StartupItems scripts. Your script's enclosing folder, as well as the contents need to be set to u=rwx,go=rx (user has read/write/execute permission, group and everyone have only read and execute; by user, I mean root). To apply this to your StartupItems script folder, assuming it's in /System/Library/StartupItems, and it's called "StaticRoutes", let's first change the ownership:

chown -R root:wheel /System/Library/StartupItems/StaticRoutes

(add "sudo " at the beginning if you're not running as root while you type it, or you'll get an "Operation not permitted" error)

This will change the owner of the StaticRoutes folder to root, and the group to wheel (though this doesn't really matter, Apple's StartupItems permissions are set that way). THE -R OPTION APPLIED IT TO ALL ENCLOSED ITEMS (I add this note in all-caps, because -R can be dangerous in the wrong hands, as you can apply the wrong ownership to "/" (the top level of your drive, but just as easily to any number of other special directories) and kill any chance of starting up properly again. The alternative, however is to to a chown for every item separately.

The next thing you want to do is change the permissions so only root has full access to the folder and its contents:

chmod -R u=rwx,go=rx /System/Library/StartupItems/StaticRoutes

Again we're using the useful but dangerous -R option to apply the permissions to the contents as well. "u" means "user", or in our case, root. "g" means "group", which could be set to anything, but we're using wheel because Apple uses wheel. "o" means everyone. Don't add a space between u=rwx and go=rx, or you'll get an error. If you're not logged in as root as you type this, you may also get an "Operation not permitted" error -- add "sudo " to the front of the command. You could also get fancy with the options and do something like "u=rwx,g=u-w,o=g" (which means u=rwx, g=whatever u is, minus write privilege, and o=whatever g is), but that's better saved for another time...

The upshot of all this is that if you change the ownership to root, and the permissions to u=rwx,go=rx for your script folder and its contents, you'll have a fully-functioning script (assuming you remembered to add the line to hostconfig). I've had to change the permissions on all my custom StartupItems scripts, since they were set incorrectly for Tiger.

If you're leery about using Terminal, the excellent and free BatChmod will do the same thing with a graphical interface (see VersionTracker.com for download link).

--

B.Henderson: The script Gary Bidwell posted tests to see whether the line "STATICROUTES=-YES-" (those are hyphens, not underlines) exists in the /private/etc/hostconfig file -- if it's not, or if it says "STATICROUTES=-NO-", then the script won't continue running.

--

ccohen: $1 is the word "stop", "start", or "restart" you should be typing after the script name (ie if you wanted to start the script manually, you'd type "/System/Library/StartupItems/StaticRoutes start" (or drag the file into the Terminal window to add the path and filename, then type "start" at the end -- $1 is the first variable passed to the script (just as $2 would be the second, for a more complex script)). I'm not sure why you're at line 42, because when I copied and pasted the script from the webpage, I got "RunService $1" at line 40...

Dec 13, 2005 12:58 PM in response to Gary Bidwell

I added the script and plist exactly as outlined by Camelot to /Library/StartupItems/StaticRoutes and then made sure the ownership of /Library/StartupItems/StaticRoutes and its contents were set as outlined by Julian Daniel. This has worked for me and generated the static network routes I need.

Thank you all for this. I have been adding the static routes to other scripts in /System/Library/StartupItems with the risk that they disappear with each new OS update.

For anyone who is trying to find out if this is relevant to your network:

This worked for our network: 3 LANs connected by T1s, a single server on one of the LANs with internet connected to a second ethernet card on the server.

Without the static routes added, clients on the server's LAN would either get the internet or the remote private networks but never both. The remote LANs could either find the server or not find the server. Both of these are related to which interface was listed first in System Preferences on the server. Of course the bottom line was that the server didn't know how to direct traffic without these static routes.

Dec 14, 2005 2:43 PM in response to Erich Wetzel

I have lied...

I was able to generate an executeable script which would add the necessary route to our server's routing table. However, I cannot get it to run at startup. I have followed the steps exactly as listed above for script text, permissions and /etc/hostconfig addition. I have tried putting the script folder in places the OS looks to run startup items /System/Library/StartupItems and /Library/StartupItems to no avail.

If I run the script manually it works great wherever it is and adds the needed route. Any suggestions?

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 permenant static route in 10.4?

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