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.
Show the routing table with iproute2:
ip route show
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
Or show the routing table using sys-apps/net-tools:
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
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. sys-apps/iproute2 needs CIDR style netmasks, so it will be in this example10.10.10.0/24
Adding a static route with sys-apps/iproute2
ip route add 10.10.10.0/24 via 192.168.1.50
Show the routing table using the
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
Or show routing table using
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
On older systems you possibly will use the route command instead of the upper described to add a static route:
route add -net 10.10.10.0 netmask 255.255.255.0 gw 192.168.1.50
The routing table is sorted from most specific routes to most 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
Adding a Permanent Static Route
For users of netifrc, 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.
... routes_wlan1="10.10.10.0/24 via 192.168.1.50 default via 192.168.1.1" ...
With dhcpcd as network manager the static route goes into /etc/dhcpcd.conf instead.
Both statements above mean:
- IP packets destined to the 10.10.10.0/24 network are send to 192.168.1.50
- IP packets destined to all 0.0.0.0/0 other networks are send to 192.168.1.1
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
- Network management — describes possibilities for managing the network stack.
- Longest prefix match (on Wikipedia)
- Gentoo Bug 5409326