Add a permanent static route to Mac OS X

As a reminder for myself, but likely useful for you! If you are in a situation where you'd like to add a static route to you Mac OS X machine, you could issue:

$ sudo route add 10.0.0.0/8 192.168.1.1

This commands tells the TCP/IP stack to sent traffic with the destination 10.*.*.* via 192.168.1.1. But, this command is only active after issuing it and before the next reboot. To "save" this command, make it permanent, you can take these simple steps.
$ sudo mkdir /Library/StartupItems/AddRoute
# cd /Library/StartupItems/AddRoute
$ echo "{
        Description     = \"Add static routing tables\";
        Provides        = (\"AddRoutes\");
        Requires        = (\"Network\");
        OrderPreference = \"None\";
}" > StartupParameters.plist
$ echo "#!/bin/sh

# Set up static routing tables
# Roark Holz, Thursday, April 6, 2006

. /etc/rc.common

StartService ()
{
        ConsoleMessage "Adding Static Routing Tables"
sudo route add 10.0.0.0/8 192.168.200.15
}

StopService ()
{
        return 0
}

RestartService ()
{
        return 0
}

RunService "$1"" > AddRoutes
$ sudo chmod 755 AddRoutes

Now you should be prepared. After a reboot, the route will be visible. (netstat -rnf inet)

Comments

Yes, I agree. This is such a

Yes, I agree. This is such a useful idea now. - Mark Zokle

That didn't work for me. I

That didn't work for me. I also have something that works but I forget the URL. buy facebook likes

Use this method, it is

Use this method, it is better:

First, check the network services on your computer with the network setup command:

[email protected]:/dev$ networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
Display Ethernet
Ethernet Pantalla Trabajo
Display FireWire
Wi-Fi
Bluetooth PAN
Thunderbolt Bridge
[email protected]:/dev$

Then, identify the device to which you want to add the static routes. In my case it is "Ethernet Pantalla Trabajo".

[email protected]:/dev$ networksetup -getinfo "Ethernet Pantalla Trabajo"
Manual Configuration
IP address: 10.1.36.253
Subnet mask: 255.255.252.0
Router: (null)
IPv6: Automatic
IPv6 IP address: none
IPv6 Router: none
Ethernet Address: 0c:4d:e9:xx:xx:xx

In my case, the device is configured in manual and without any gateway. I have my gateway pointing to a wifi network that gives me internet access, and I want to route all the private address space to this interface. So I add the following command:

sudo networksetup -setadditionalroutes "Ethernet Pantalla Trabajo" 10.0.0.0 255.0.0.0 10.1.36.1 172.16.0.0 255.240.0.0 10.1.36.1 192.168.0.0 255.255.0.0 10.1.36.1

Notice that you have to add all the routes on the same command. If you use three separate ones, every time you set the command, it will remove the previous configuration, so you will be setting one route and remove other.

After that, you can check that everything is ok with this command:

[email protected]:/dev$ sudo networksetup -getadditionalroutes "Ethernet Pantalla Trabajo"
10.0.0.0 255.0.0.0 10.1.36.1
172.16.0.0 255.240.0.0 10.1.36.1
192.168.0.0 255.255.0.0 10.1.36.1
[email protected]:/dev$

Finally, you could check with netstat -rn that the routers are there:

[email protected]:/dev$ netstat -rn | grep 10.1.36
10 10.1.36.1 UGSc 1 0 en8
10.1.36/22 link#14 UCS 15 0 en8
10.1.36.1 0:0:c:7:ac:1 UHLWIi 3 0 en8 1152
10.1.36.253/32 link#14 UCS 1 0 en8
172.16/12 10.1.36.1 UGSc 2 0 en8
192.168.0/16 10.1.36.1 UGSc 1 0 en8

The better point is that those routes will be "attached" to the network interface forever. I mean, if you unplug the cable, the routes will disappear, if you plug the cable again, the routes will appear. That's perfect because if it doesn't work this way, you could have problems with routes while you are not at your usual location.

I hope it could be interesting for someone, it has saved me :)

The method posted here could be valid, but in my case, I work in an office with a thunderbolt display on my desk and a macbook air. Every time I take my mac away from the display, with the method posted here, I will have to reboot the mac to get the routes configured again. With the method I shared, I can move around with the mac, and when I return to my desk and I plug the computer to the screen, the routes are configured again :)

Also, I have a secondary thunderbolt display at home. When I connect my mac to my home's thunderbolt display, the routes are NOT configured, because it is a different network interface, and this is fantastic because I don't need these routes at home or when I am connected only to the wifi network.

I hope you could find this interesting.

Best regards!

ISO

Nice - works well for me too.

Nice - works well for me too. Thanks.

nice! It's very useful.

nice! It's very useful.

in Snow leopard Server 10.6.3

in Snow leopard Server 10.6.3 I need to write the StartupParameters.plist as follows:
{
Description = "Add static routing tables";
Provides = ("AddRoutes");
Requires = ("Network");
OrderPreference = "None";
}

also sudo chmod 755 StartupParameters.plist

- there is also an

- there is also an inconsistency between "AddRoute" and "AddRoutes"

doesn't work for several

doesn't work for several reasons.

- the first echo fails because regular user can't create file in directory owned by root

- the second echo fails for the same reason, plus also your attempt at nesting double quotes fails

- even after compensating for these problems, config fails on 10.4.11, simply doesn't work

thanks anyway

This didn't work for me, but

This didn't work for me, but I found something that did. This is just in case others are looking for a solution.

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

If you want to perm a route

If you want to perm a route in 10.10 Yosemite. here

You should create a quick script with

#!/bin/bash

sudo /sbin/route add -net x.x.x.0/24 xxx.xxx.xxx.xxx

save it in a path

chmod a+x the file

Then type this,..

sudo defaults write com.apple.loginwindow LoginHook /Path/To/Your/Script

POW .. Yosemite perm route with edit file .

About Consultancy Articles Contact




References Red Hat Certified Architect By Robert de Bock Robert de Bock
Curriculum Vitae By Fred Clausen +31 6 14 39 58 72
By Nelson Manning [email protected]