Search:  
Gentoo Wiki

HOWTO_add_a_static_route


This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Contents

What is a route

A route is a rule used by your kernel to determine how to get someplace on a network. This HOWTO covers IP routes (routes on an IP network) but there are other types of routable networks. Routes are stored in the Linux kernel are accessible for viewing and editing to users.

Viewing your routing table

The easiest way to view your routes is to use the command:

/sbin/route -n

The table looks something like this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
127.0.0.0       127.0.0.1       255.0.0.0       UG    0      0        0 lo
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth1

The Destination is the network address for the routing entry, combined with the genmask (netmask) you can see what network is being routed to the Gateway listed. Please read the man page for route for more information on the meanings of these fields.

Adding a route

To add a route you must first know the network address of the network you wish to add, and the gateway to that network. In our case we are going to use the network 10.0.0.0 with a netmask of 255.255.255.0. We have a firewall on our network with an IP address of 192.168.1.50 that is the gateway to this 10.0.0.0 network.

To add the route manually we use the command:

route add -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.1.50

Our route table now looks like this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        192.168.1.50    255.255.255.0   UG    0      0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
127.0.0.0       127.0.0.1       255.0.0.0       UG    0      0        0 lo
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth1

As you can see the network 10.0.0.0 with a Genmask of 255.255.255.0 shows up with a gateway of 192.168.1.50. But how do we make sure this happens on boot?

Adding a route to /etc/conf.d/net

When adding a route to your /etc/conf.d/net file you need one more piece of information, the interface you will be routing out. This will likely be eth0, but can be something else. The easiest way to tell what interface you will route out is to look at your routing table for the line coresponding to the network the gateway for your route is on. In our case the gateway was 192.168.1.50. That coresponds to the 192.168.1.0 255.255.255.0 network, which is on eth1.

We add this to the /etc/conf.d/net file to configure the route:

routes_eth1=(
   "10.0.0.0/24 via 192.168.1.50"
)

If you do not have a "gateway" line in your net config file you probably already have a routes_ethX entry. It will likely have something like "default gw 1.1.1.1" in it. You can have multiple lines in your routes list.

In our case the routes list would look like this (without a gateway line):

routes_eth1=(
   "10.0.0.0/24 via 192.168.1.50"
   "default via 192.168.1.1"
)
Retrieved from "http://www.gentoo-wiki.info/HOWTO_add_a_static_route"

Last modified: Fri, 05 Sep 2008 07:05:00 +0000 Hits: 30,986