iproute and Routing Tables

Topics · Published 2019-07-19 · Revised 2026-11-23
Reviewed corrections applied locally; pre-review version preserved separately · Joomla source

107,559 Total views across this article’s versions; includes the historical baseline where available.

Routes describe how the kernel reaches destinations. Policy rules decide which routing tables to consult. Together they provide Linux policy routing; iptables is a separate packet-filtering interface, not a third component of the RPDB.

Routes

ip route belongs to iproute2 and manages kernel routing tables. The older route command belongs to net-tools. It can still help when reading historical output, but it does not provide iproute2’s policy-table controls.

Use ip -4 route show table all to inspect all IPv4 tables, or name one table explicitly. Modern Linux table identifiers are 32-bit values, not a limit of 256 tables. The reserved identifiers and optional names are explained below.

Routing Rules

ip rule manages the Routing Policy Database: an ordered list of selectors and actions. Lower numeric priority values run first. A matching rule can look up a table or return a terminal action such as prohibit.

Within a consulted table, destination-prefix selection finds the most specific eligible route. A failed lookup or selected throw route permits later policy rules to be tried. A successful route or terminal rejection ends the lookup. Rule priority is separate from table ID and route metric; 32768 is not a documented maximum number of policy rules.

See Follow the (ip) Rules for selectors and explicit priorities.

Routing Table Names: rt_tables

The file usually called rt_tables is a name-to-number mapping, not a master routing table or a database of routes. iproute2 can read names from /etc/iproute2/rt_tables or a vendor file such as /usr/lib/iproute2/rt_tables; exact locations depend on the installed build. Numeric IDs work without adding an alias.

A typical mapping includes:

255 local
254 main
253 default
0 unspec

The kernel uses table numbers. It does not reread this file for every packet. File order does not set table priority. Blank lines and comments are accepted by the iproute2 name-file parser. A historical commented line such as #1 inr.ruhep is an example alias, not another active built-in table.

Default (Built-in) Routing Tables

A normal initial IPv4 policy ruleset looks like this:

0:      from all lookup local
32766:  from all lookup main
32767:  from all lookup default

These are rule priorities, not table IDs. The local table (255) contains local-delivery and broadcast routes; the main table (254) normally contains ordinary routes; the default table (253) is initially empty and is consulted later by its policy rule. unspec (0) is reserved, not a fallback table for failed packets.

The table named default is different from a default route, which is a destination prefix of 0.0.0.0/0 in IPv4 and can appear in main or another table. Addresses and routes still need to be configured, often by the host’s network manager.

ip -4 rule show
ip -4 route show table local
ip -4 route show table main
ip -4 route show table all

Examining Existing Routes

ip -4 route list and ip -4 route show table main normally display the same IPv4 table. For a host with address 192.168.10.11/24 on eth0 and a reachable gateway at 192.168.10.10, the output could include:

default via 192.168.10.10 dev eth0
192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.11

The first line is a default route through a gateway. The second is a directly connected prefix; src records the preferred local source address when an application has not already chosen one. It does not rewrite forwarded packets and is not source NAT.

The display describes routing state. It does not prove the gateway is working, that forwarding is enabled, or that a firewall permits a particular connection. Inspect the chosen route for a specific destination as well as the table:

ip -4 route get 192.168.10.25

ip route Syntax

Here are common route types:

TypeMeaning
unicastA path to destinations covered by the prefix.
localDelivery to an address on this host.
broadcastDelivery as a link broadcast.
blackholeDiscard silently.
unreachableDiscard and report host unreachable, subject to ICMP error rules.
prohibitDiscard and report administratively prohibited, subject to ICMP error rules.
throwEnd this table lookup as though no route were found; later policy rules may provide a route.

Useful attributes include dev (output device), via (next-hop address), src (preferred local source), metric (route preference), scope, and proto.

For ordinary IPv4 routes, host scope describes local delivery, link scope describes direct link reachability, and global scope permits wider reachability, such as through a gateway. Default scope depends on the route type; it is not always global.

The protocol tag describes a route’s origin or administrative classification. kernel identifies routes installed by the kernel; static identifies an administrator’s static route; boot is the conventional default for an otherwise unspecified protocol. None of these tags makes a route survive reboot by itself. See ip-route(8) for other types and attributes.

Adding New Routes

Choose whether the route belongs in main or in a separate policy table. A separate table also needs a rule selecting it for normal policy routing. Administrative changes require suitable privileges, normally root or CAP_NET_ADMIN.

The general form below is a template; the capitalized words are placeholders, not literal shell input:

ip -4 route add PREFIX via GATEWAY dev INTERFACE table TABLE_ID

PREFIX is a destination prefix. GATEWAY is one address, without a prefix length, and must be reachable through the specified interface.

For example, if eth1 is already configured as 192.168.1.10/24, 192.168.1.100 is an on-link router, and table 200 is unused, these commands populate that table:

ip -4 route add 192.168.1.0/24 dev eth1 scope link src 192.168.1.10 table 200
ip -4 route add default via 192.168.1.100 dev eth1 table 200
ip -4 route show table 200

This is the same table used in the later step-by-step example; do not add its routes twice. The routes do not yet select traffic for table 200.

The preceding commands change the active routing configuration.[2]

Main Routing Table Best Practices

Keep separate the numbers used for table IDs, policy-rule priorities, and route metrics. A table ID identifies a table; it does not rank it. Values 100–200 are a possible local naming convention, not a kernel requirement.

Inspect existing configuration before choosing an unused table ID and explicit rule priority. Preserve a recovery path and verify both routing and firewall behavior before making changes persistent.

Adding Routes to the main Table

For an ordinary unicast route, omitting table selects main. Being explicit makes examples easier to follow:

ip -4 route show table main

For otherwise comparable routes to the same destination prefix, a lower metric is preferred. IPv4 route metrics use an unsigned 32-bit value, so 65535 is not the maximum. A metric does not override a more specific eligible destination prefix or the order of policy-rule lookups.

Adding New Routes to Existing Routing Tables

Supply table TABLE_ID with the route command. Names are optional aliases for that ID. A next-hop address after via is an address, not a subnet or an address with /24 appended.

ip -4 route add PREFIX via GATEWAY dev INTERFACE table TABLE_ID src LOCAL_ADDRESS

This is a syntax template. src is optional and, when used, must identify a suitable local address. Direct routes omit via; terminal routes such as blackhole do not need an output interface.

Route Priority Processing

Think of selection as two stages:

  1. The RPDB examines policy rules in increasing numeric priority. Each matching rule applies its action, often a routing-table lookup.
  2. A table lookup chooses among eligible routes using destination specificity and relevant attributes, including metric for otherwise comparable routes. Multipath routes can supply more than one next hop.

A matching throw route or absent result permits the RPDB to continue. A terminal result, including a rejecting route, stops it. A default route in an early selected table can therefore prevent a more specific route in a later table from ever being considered.

Insertion order is not a general routing design rule. Use explicit, unique policy priorities and inspect the result for the actual destination, source, mark, and other selectors involved.

ip route Command Examples

The following snippets are templates, not a configuration to paste as a group. Replace the capitalized placeholders, confirm gateway and interface prerequisites, and choose the intended table. For a concrete bounded example, follow the steps under Creating Additional Routing Tables.

Point Destination to Gateway Router

A gateway is the next-hop router for the destination prefix:

ip -4 route add DESTINATION/PREFIX_LENGTH via GATEWAY_ADDRESS dev INTERFACE

The prefix belongs to the destination; via takes only the gateway address.

Set Default Route to Gateway Router

For destinations without a more specific eligible route in that table:

ip -4 route add default via GATEWAY_ADDRESS dev INTERFACE

The gateway must be reachable. An existing conflicting default can cause add to fail; inspect the current routes before deciding whether to change one.

Point Destination to Next Hop

“Next hop” and “gateway” describe the same role here. For one destination host, make the host prefix explicit:

ip -4 route add DESTINATION_ADDRESS/32 via NEXT_HOP_ADDRESS dev INTERFACE

Assign Route to an Interface

A route without via treats the destination as directly reachable through the interface:

ip -4 route add DESTINATION/PREFIX_LENGTH dev INTERFACE

On Ethernet, that generally requires direct link-layer reachability. A device-only route is not a substitute for a necessary gateway.

Set Default Route to Specific Interface

A device-only default is appropriate for some point-to-point links, such as a suitably configured tunnel:

ip -4 route add default dev POINT_TO_POINT_INTERFACE table TABLE_ID

Do not apply this blindly to Ethernet. It treats all selected destinations as directly reachable on that device and does not discover an upstream router.

Policy Routing (Routing Tables)

Putting a route in another table is only part of policy routing. A rule must select that table, and the table must contain the routes needed by the selected traffic. A default route in a table named vpn does not establish a VPN, create an interface, or automatically select traffic.

Use numeric table IDs when an alias has not been configured. Inspect both routes and rules, not just the contents of rt_tables.

Port-Based Selection Is Not Automatic Failover

Marking TCP port 22 traffic for one table and TCP port 80 traffic for another is port-based policy selection. It does not detect a failed VPN or implement automatic failover.

For traffic arriving on a router’s LAN interface, the conceptual sequence is:

  1. Classify the intended forwarded packets in mangle/PREROUTING and set a packet mark.
  2. Use explicit-priority ip rule fwmark selectors to look up the corresponding tables.
  3. Supply the tunnel routes and the required return path, forwarding policy, and any NAT configuration.

A missing route can allow lookup to continue to main, including an ordinary Internet default. A route still present through a failed tunnel can instead continue selecting a path that does not work. Neither outcome is health detection. A complete design must explicitly choose failure behavior, prevent unwanted fallback, and test disconnect and reconnect handling.

For a single-host destination-based example with a fallback guard, see the Split Gateway Tutorial. The two-tunnel idea here is an explanation of selection, not a tested multi-VPN deployment procedure.

Deleting Routes

Delete the intended route from the intended table using enough attributes to identify it. For the default in the bounded example:

ip -4 route del default via 192.168.1.100 dev eth1 table 200

If its policy rule is active, remove that rule first as shown in the rollback below. Do not flush unrelated routes.

A successful ip route command changes the running routing configuration. You do not need to reboot or routinely run ip route flush cache to activate the IPv4 route changes shown here.

Check the affected routing table and the route selected for your intended destination. Then test a new connection while keeping your recovery access available. A successful command confirms that the change was accepted; it does not establish that your connections will work as intended.

Making the change survive a reboot is a separate step. Test the running configuration before making it persistent.

Creating Additional Routing Tables

A numeric table ID can be used as soon as a route is added to it. Adding an alias to rt_tables is optional and does not populate the kernel table. Removing an alias does not remove its routes or policy rules.

If ordinary destination routing in main meets your needs, a separate table may be unnecessary. Use a separate table when the policy needs a different set of routes. The practical steps are to choose an unused ID, populate the routes, and add the intended policy rule. Naming the ID is a convenience.

Best Practices When Adding New Routing Tables

Choose a table ID not used by the system’s existing configuration. Do not repurpose reserved IDs 0, 253, 254, or 255. If you add a name, keep it unambiguous and consistent with the numeric ID.

Table names need not be lowercase, and gaps between table numbers do not reserve policy priority. Unique, explicit rule priorities determine lookup order. Save the current rules and routes for comparison and prepare exact rollback before changing traffic selection.

How to Create a New Routing Table

This IPv4 illustration assumes eth1 already has 192.168.1.10/24, gateway 192.168.1.100 is directly reachable there, table 200 is unused, and rule priority 10000 is unused. It selects traffic whose source is 192.168.1.10. It does not configure the interface, enable forwarding, install a firewall, or establish a VPN.

Inspect current state first:

ip -4 address show dev eth1
ip -4 rule show
ip -4 route show table all

Keep recovery access available. Perform the route-addition step only once, including if you used the earlier example.

Step 1. Choose a Table ID and Optional Name

You may use numeric table 200 throughout. If you want the name custom, inspect the installed iproute2 configuration and add one unambiguous mapping in its administrator name file:

200 custom

Do not blindly append duplicate mappings. Blank lines and comments are valid. The mapping’s position in the file and the number 200 do not establish lookup priority.

Step 2. Populate Your New Table with One or More Routes.

Populate the connected prefix before adding the route through its gateway:

ip -4 route add 192.168.1.0/24 dev eth1 scope link src 192.168.1.10 table 200
ip -4 route add default via 192.168.1.100 dev eth1 table 200
ip -4 route show table 200

The preferred source belongs to this host. It is not the gateway’s address. Inspect command errors; do not proceed on the assumption that a failed addition took effect.

Step 3. Create at Least One Rule That Points to Your New Table.

Under the stated prerequisites, add the explicit-priority source rule and inspect the selected route:

ip -4 rule add priority 10000 from 192.168.1.10/32 lookup 200
ip -4 rule show
ip -4 route get 198.51.100.1 from 192.168.1.10

The documentation address in route get requests a kernel lookup; it is not a remote reachability test. With the ordinary initial rules and no intervening matching rule, this source is looked up in table 200 before main. Test an appropriate real destination and return path separately.

To undo only this example, remove the selector before its routes:

ip -4 rule del priority 10000 from 192.168.1.10/32 lookup 200
ip -4 route del default via 192.168.1.100 dev eth1 table 200
ip -4 route del 192.168.1.0/24 dev eth1 scope link src 192.168.1.10 table 200

If you added an alias solely for the test, remove only that alias line afterward. Persistent configuration is separate from these running changes. See Persistence.

Endnotes

[1] Routing-table capacity depends on kernel implementation, configuration, and available resources. The 32-bit identifier range is not a promise that a host can populate that many tables or routes.

[2] The old IPv4 route cache was removed in Linux kernel 3.6. Active routing changes and startup persistence are separate concerns: a startup service, script, or network configuration must recreate any custom rules and routes needed after a reboot.

References: ip-route(8), ip-rule(8), route(8), and the iproute2 name-file parser.