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.
Show the routing table with iproute2:
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
The IP address, subnet mask (CIDR), and gateway are necessary prerequisite information before adding a static route.
In this example the
10.10.10.0 network with a
255.255.255.0 subnet mask will be routed to the
192.168.1.50 gateway. CIDR style netmasks are required when adding routes using commands from the package (ip). The following example will add the
ip route add 10.10.10.0/24 via 192.168.1.50
Show the routing table using the ip route command:
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
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.
255.255.255.255 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.
routes_wlan1="10.10.10.0/24 via 192.168.1.50
default via 192.168.1.1"
Both statements above mean:
- IP packets destined to the
10.10.10.0/24network are send to
- IP packets destined to all
0.0.0.0/0other networks are send to
0.0.0.0/0 means all other networks without a prefix (Subnet mask), the default route.
The default route
0.0.0.0/0 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.
- iproute2 — a tool developed to unify network interface configuration, routing, and tunneling for Linux systems.
- Network management — describes possibilities for managing the network stack.
- Longest prefix match (on Wikipedia)