Joeri Verdeyen bio photo

Joeri Verdeyen

Web-engineer, cyclist, Nespresso lover, Strava pusher.

Twitter LinkedIn Instagram Github Stackoverflow Last.fm Strava

Add persistent static routes on OS X Yosemite

When I setup my development environment with Docker, I’ll have to add a static route on every reboot. Here is a simple workaround on how to add a persistent static route on OS X Yosemite. This example will route all subnet-addresses 10.1.*.* traffic to the Vagrant machine with IP 172.17.8.101.


Create a simple bash script

Create a new file /usr/local/bin/static-routes.sh

vim /usr/local/bin/static-routes.sh

Add the following lines in /usr/local/bin/static-routes.sh You can add more routes if you want to.

#!/bin/bash
sudo route -n add -net 10.1.0.0 172.17.8.101

Make the script executable.

chmod +x /usr/local/bin/static-routes.sh

Add the bash script to the LaunchAgents

sudo vim ~/Library/LaunchAgents/com.docker.scripts.routes.static.plist
<!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>Label</key>
        <string>com.docker.scripts.routes.static</string>
        <key>Program</key>
        <string>/usr/local/bin/static-routes.sh</string>
        <key>ServiceDescription</key>
        <string>Persist static routes workarround</string>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <true/>
        <key>StandardErrorPath</key>
        <string>/dev/null</string>
        <key>StandardOutPath</key>
        <string>/dev/null</string>
    </dict>
</plist>

Load and enable the plist file

launchctl load ~/Library/LaunchAgents/com.docker.scripts.routes.static.plist

Check-up

Check if the route is added after a reboot

$ netstat -nr  | grep "172.17.8.101"
10.1/16            172.17.8.101       UGSc            0        0 vboxnet

Thanks for reading

Feel free to leave a comment if you have remarks or like this post