Static routing

From Gentoo Wiki
Jump to:navigation Jump to:search
This page contains changes which are not marked for translation.
Other languages:

A route is a rule set in the kernel that is used to determine which physical network interface or gateway is needed in order to reach a particular network (or single host). There are many types of routed protocols; this article covers routing of the IP protocol in the Linux kernel.

Although IP routes are stored in the kernel, they are modifiable by userspace tools as described in the following examples.

Showing routes

Show the routing table with iproute2:

user $ip route
default via dev wlan1 metric 1 dev lan proto kernel scope link src via dev lo dev wlan1 proto kernel scope link src

Adding a static route

The IP address, subnet mask (CIDR), and gateway are necessary prerequisite information before adding a static route.

In this example the network with a subnet mask will be routed to the gateway. CIDR style netmasks are required when adding routes using commands from the sys-apps/iproute2 package (ip). The following example will add the route:

root #ip route add via

Show the routing table using the ip route command:

user $ip route
default via dev wlan1 metric 1 dev wlan1 via src dev lan proto kernel scope link src via dev lo dev wlan1 proto kernel scope link src

The routing table is sorted from most specific routes to most general. 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. is at first position in the routing table.

Adding a permanent static route

For users of the netifrc scripts (OpenRC's standard network tools), permanent static routes can be added by opening a preferred text editor to /etc/conf.d/net and adjusting the file accordingly.

Reference the current routing table for help.

FILE /etc/conf.d/net
routes_wlan1=" via
    default via"

With dhcpcd as network manager the static route goes into /etc/dhcpcd.conf instead.

Both statements above mean:

  1. IP packets destined to the network are send to
  2. IP packets destined to all other networks are send to
Note means all other networks without a prefix (Subnet mask), the default route.

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 its own IP network segment, and there is no specific route found in the routing table for target IP network.

See also

External resources