Static routing

What is a Route
A 'route' is a rule, set in your kernel, to determine which physical network interface or which gateway to use, to reach a particular network (or single host). There are many types of routed protocols this article simply covers routing of the IP protocol. IP routes are stored in the kernel. To see the current routing table on your system use as root:

or as a regular system user use:

The two commands will produce output similar to this:

Kernel IP routing table Destination    Gateway         Genmask         Flags Metric Ref    Use Iface 192.168.50.0   0.0.0.0         255.255.255.0   U     0      0        0 lan 192.168.1.0    0.0.0.0         255.255.255.0   U     2000   0        0 wlan1 127.0.0.0      0.0.0.0         255.0.0.0       U     0      0        0 lo 0.0.0.0         192.168.1.1     0.0.0.0         UG    2000   0        0 wlan1

The deprecated route command and other tools like ifconfig are being replaced by sys-apps/iproute2. All networking functions are now managed through the single, ip commmand, which has bit differing syntax and outputs:

to show the route:

produces default via 192.168.1.1 dev wlan1 metric 1 192.168.50.0/24 dev lan proto kernel scope link src 192.168.50.1 127.0.0.0/8 via 127.0.0.1 dev lo 192.168.1.0/24 dev wlan1 proto kernel scope link src 192.168.1.1

Adding a Static Route
To add a static route you have to know the IP and the subnet mask of a network you wish to route, and the gateway to that network.

In this example we route the network 10.10.10.0 with the netmask 255.255.255.0 to the gateway 192.168.1.50

To accomplish this use the following command:

or:

Our route table now looks like this:

Kernel IP routing table Destination    Gateway         Genmask         Flags Metric Ref    Use Iface 10.10.10.0     192.168.1.50    255.255.255.0   UG    0      0        0 wlan1 192.168.50.0   0.0.0.0         255.255.255.0   U     0      0        0 lan 192.168.1.0    0.0.0.0         255.255.255.0   U     2000   0        0 wlan1 127.0.0.0      0.0.0.0         255.0.0.0       U     0      0        0 lo 0.0.0.0         192.168.1.1     0.0.0.0         UG    2000   0        0 wlan1

The routing table is sorted from most specific routes to more general, and this is how it is read by the routing process. Longest prefix match - means the the smallest network, or the network with the largest netmask, or the most specific route f.e. 255.255.255.255 is at first position in a routing table.

the same routing able as shown by iproute2: default via 192.168.1.1 dev wlan1 metric 1 10.10.10.0/24 dev wlan1 via 192.168.1.50 src 10.10.10.1 192.168.50.0/24 dev lan proto kernel scope link src 192.168.50.1 127.0.0.0/8 via 127.0.0.1 dev lo 192.168.1.0/24 dev wlan1 proto kernel scope link src 192.168.1.1

Adding a Permanent Static Route
To add a permanent static route, open your favorite text editor to /etc/conf.d/net and adjust accordingly. Make sure to refer to your current routing table for help.

Both statements above mean:
 * 1) IP packets destined to the 10.10.10.0/24 network are send to 192.168.1.50
 * 2) IP packets destined to all 0.0.0.0/0 other networks are send to 192.168.1.1

The default route is used if:
 * the host has no physical or logical IP interface in the target network segment
 * the host has to send IP packets outside of his own network segment and there is no specific route in the routing table