31 January 2011

Static Routes in FreeBSD

A friend of mine recently told me that while he appreciated the detail
and content of my writings, a good deal of the subject matter was a
little beyond him.  In support of the fresh-faced sysadmins and those
that might be new to FreeBSD (we were all there once), this write up is
how to add static routes to a FreeBSD host.  Our details for this are:
        HOST:           beastie
        PROMPT:         beastie [0]
        OS:             FreeBSD 8.1
While the examples are performed on FreeBSD 8.1, they should equally
apply at least back to FreeBSD 4.8.

In the following example, a route is added for a management network with
a destination of  This network encompasses all addresses
starting from and ending
        beastie [0] /sbin/route add -net -iface em0
        add net gateway em0
The route is added with a gateway of one of our network interfaces
(-iface em0), meaning the network is directly reachable through this
interface.  In the next example, we add a route for a monitoring network.
The destination is, using as the gateway and as the netmask (addresses - (CIDR:
        beastie [0] /sbin/route add -net
        add net gateway
The above 'route' commands illustrate two different ways 'route' allows
for specifying a netmask for the network in question.  The first uses
CIDR notation (/24) immediately following the destination.  In the second
example, the netmask is specified following the gateway.  Either way is
perfectly acceptable.  In the next command, a host route for an operations
server is added for host, with as the gateway:
        beastie [0] /sbin/route add -host
        add host gateway
A check of our routing table shows the newly added routes (I've identified
them with "<====" at the end of the lines):
        beastie [0] /usr/bin/netstat -f inet -nr
        Routing tables
        Destination        Gateway            Flags    Refs      Use  Netif Expire
        default            US          0        0    lo0       link#1             U           0        0    em0        link#1             UHS         0        0    lo0        link#1             UHS         0        0    lo0      08:00:27:ce:11:c0  US          0        0    em0  <====         UGS         0        0    em0  <====          link#3             UH          0       82    lo0      UGHS        0        0    em1  <====    link#2             U           1     1287    em1      link#2             UHS         0        0    lo0
In order to retain the above routes through a reboot, we need to update
'/etc/rc.conf'.  To specify static routes, we add "static_routes" and
supply a double quoted, space delimited string as its value:
        static_routes="monitoring management ops_server"
Each element of the string gets a separate subsequent entry of
"route_ELEMENT" whose value is passed off to 'route add' during system
        route_monitoring="-net -iface em0"
Of note, static routes need to be added after their dependent interfaces
are brought online.  Also, while underscores (_) are accepted as part of
the element, hyphens (-) are not.  Below are the contents of our updated
'/etc/rc.conf' accounting for our new static routes after our interfaces:
        beastie [0] /usr/bin/grep -v ^# /etc/rc.conf
        ifconfig_em0="inet netmask"
        ifconfig_em1="inet  netmask"
        static_routes="monitoring management ops_server"
        route_monitoring="-net -iface em0"

see also:
    Static Routes in Solaris
    Static Routes in Linux

No comments: