[CentOS] Static routes with a metric?

Thu Dec 15 17:54:07 UTC 2011
Matt Garman <matthew.garman at gmail.com>

Adding additional info for posterity, and in case anyone else runs
across this...

On Wed, Dec 7, 2011 at 12:28 PM, Benjamin Franz <jfranz at freerun.com> wrote:
> On 12/7/2011 10:03 AM, Matt Garman wrote:
>> Hi,
>>
>> [...]
>>
>> What I basically need to be able to do is this:
>> route add -host h1 gw g1 metric 0
>> route add -host h1 gw g2 metric 10
>>
>> Notice that everything is the same except the gateway and metric. I could
>> put this in /etc/rc.local, but was wondering if there's a cleaner way to do
>> it in e.g. the network-scripts directory.
>>
>
> If you create files in the /etc/sysconfig/network-scripts directory
> named according to the scheme
>
> route-eth0
> route-eth1
> route-eth2
>
> it will execute each line in the files as
>
> /sbin/ip route add <line>
>
> when each interface is brought up.
>
> Look in the /etc/sysconfig/network-scripts/ifup-routes script for all
> the gory details and features.

I actually did just that---looked at the ifup-routes script.  The
thing that threw me off is the comments about "older format" versus
"new format".  I probably read into the comments too much, but I
thought to myself, "I should probably use the new format, as they
might some day deprecate the old format."

But anyway, the "older format" is what I need.  With the older format,
it's exactly what you said above: each line corresponds to running "ip
route add <line>".  So what I added were lines in this format:

<addr>/<mask> via <gateway> dev <device> metric <N>

A contrived example might be:

10.25.77.0/24 via 192.168.1.1 dev eth0 metric 5

The "new format" is where each group of three lines corresponds to a
route.  You have the ADDRESSxx=, NETMASKxx=, GATEWAYxx= lines.
Clearly this is less flexible, particularly if you need to set a
metric like me.  :)

Anyway, hopefully that's useful for anyone in a similar situation!

-Matt