18 October 2010

Static Routes in Solaris

This tip may seem somewhat trivial as we all know that to retain static
routes in Solaris, you simply create an init script such as the following:

        adler [0] ls -l /etc/rc2.d/S76route
        -rwxr-xr-x   1 root     other        392 Mar 24  2009 /etc/rc2.d/S76route*
        adler [0] cat /etc/rc2.d/S76route
        #!/bin/sh
        /usr/sbin/route add -net 10.150.0.0/16 172.5.21.145
        /usr/sbin/route add -net 10.151.0.0/16 172.5.21.145
        /usr/sbin/route add -net 10.152.0.0/16 172.5.21.145
        /usr/sbin/route add -net 10.153.0.0/16 172.5.21.145
        /usr/sbin/route add -net 10.154.0.0/16 172.5.21.145
        /usr/sbin/route add -net 10.155.0.0/16 172.5.21.145
        /usr/sbin/route add -net 10.156.0.0/16 172.5.21.145
        /usr/sbin/route add -net 10.157.0.0/16 172.5.21.145
        /usr/sbin/route add -net 10.158.0.0/16 172.5.21.145

What you might not know, is that in Solaris 10, Solaris now has a means
of retaining persistent static routes, complete with a new option (-p)
to 'route' and a config file.  In Solaris 10, the following 'route'
command would set a persistent route to be retained through reboots:

        adler [0] route -p add -net 172.3.2.0 172.10.18.4 255.255.254.0
        add net 172.3.2.0: gateway 172.10.18.4
        add persistent net 172.3.2.0: gateway 172.10.18.4

The above created route would still appear the same in a listing of the
routing table, however, you may notice that there is a secondary line
of output upon creating the route:

        add persistent net 172.3.2.0: gateway 172.10.18.4

This simply means that the 'route' command updated config file
/etc/inet/static_routes.  By default, this file will not exist until
a static route is created via 'route -p ...' or you create it.  Before
getting to contents, the following are the ownership / permissions set
to the file by 'route':

        adler [0] ls -l /etc/inet/static_routes
        -rw-r--r--   1 root     root          45 Oct  6 13:35 /etc/inet/static_routes

And now, the contents, which are effectively the arguments to 'route add':

        adler [0] cat /etc/inet/static_routes
        # File generated by route(1M) - do not edit.
        -net 10.241.11.0 172.10.18.3 255.255.255.0
        -net 172.3.2.0 172.10.18.4 255.255.254.0

Yes, I know it says do not edit, though in checking out the source of
'route' via opensolaris.org, it doesn't appear that manual editing
is an issue.  Finally, Solaris has a native, standardized means of
configuring persistent static routes.

Additionally, to remove a static route, delete it from
/etc/inet/static_routes and remove via 'route' or simply use the following
'route' command:

        adler [0] route -p delete -net 172.3.2.0 172.10.18.4 255.255.254.0


see also:
    Static Routes in FreeBSD
    Static Routes in Linux