Follow the (ip) Rules

Topics · Published 2019-08-24 · Revised 2026-11-23
Reviewed corrections applied locally; original wording preserved separately · Joomla source

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

Hopefully, you have already read and/or understand these concepts:

Now, it’s time to talk about ip rules. The Routing Policy Database (RPDB) selects an action, often a lookup in a routing table. The usual IPv4 rules consult local, main, and default. Those lookups choose routes; they do not grant firewall permission to all traffic.

Routes and rules added at the command line affect the live kernel state. Save or reapply them through the system’s network configuration if they should survive reboot. Firewall-rule persistence is a separate concern; see Persistence: Making iptables Changes Stick.

New rules are created with the ip rule add command.

ip rule Syntax

Like almost everything on this site, I’m not going to delve into every possible parameter and switch in ip rule, but I shall go over the most common variants. The syntax of a rule looks like this:

ip rule add [priority NUMBER] SELECTOR ACTION

Sounds simple enough, eh? But, what is a selector? What is an action?

The add command inserts a rule. Its selector determines which lookups match, and its action determines what happens next. from and to are source and destination selectors; they do not mean incoming and outgoing traffic. The following is a subset of the available syntax.

Address Selectors

from {addr} | to {addr}

Selector

to {addr} | priority {#} | fwmark {fwmark} | iif {name} | oif {name} | tos {value}

Predicate

{ blackhole | prohibit | unreachable } | lookup {table id}

It’s helpful to break these down a bit further.

An address selector accepts an individual address or a network prefix. A rule may combine from and to; both conditions must match.

from

An address or range of addresses with the given source IPv4 address or range.*

to

An address or range of addresses with the given destination IPv4 address or range.*

* Note: “all” is an acceptable wildcard, and means “all addresses.”

from checks the source address, and to checks the destination address. Either selector can apply to locally generated or forwarded traffic. Direction is a different question; an incoming-interface selector such as iif eth0 can distinguish traffic arriving on that interface.

Other selectors can be combined with the address selectors. Priority controls the order in which rules are examined; it is not a packet field.

Selector Description
fwmark Match the packet mark, optionally using a mask; this does not set a mark.
iif Incoming interface name; iif lo can select traffic originating on this host.
oif Outgoing device for traffic from local sockets bound to a device; not a general test of a future forwarding interface.
priority Set an explicit, distinct rule priority; lower numbers are examined first.
tos ToS=Type of Service; rarely used by home users; for more info see ToS

These are four common actions. Other actions, such as goto, are documented in ip-rule(8).

Predicate Description
lookup Look up the selected table; a lookup with no result can continue to a later rule.
blackhole drop packet silently
prohibit reject packet and error, "Communication is administratively prohibited"
unreachable drop packet and return error, "network unreachable"

ip rule Examples

These examples illustrate individual rules, not a script to install as a set. Names such as custom, mytable, and test123 must already map to table IDs in iproute2’s table-name configuration; numeric table IDs need no alias. A lookup also needs suitable routes in its target table to select a path. Use unused priorities appropriate to your existing rules.

ip rule add priority 100 to 192.168.1.200 lookup custom

Let’s pick apart this example. The “to” prefixing an IP address means it is a destination address. “Lookup custom” means the corresponding action is to lookup or read the routing table named custom. So, an English reading of the corresponding new rule would be something like,

“If the current packet destination address is 192.168.1.200, lookup and execute a route in the table named custom.”

Here are more examples of rules you could create:

# match a source network and direct its traffic to a particular table
ip rule add priority 110 from 192.168.1.0/24 lookup mytable

# prohibit traffic from a source network
ip rule add priority 120 from 192.168.1.0/24 prohibit

# blackhole any traffic headed for a particular sub-net
ip rule add priority 130 to 10.10.10.0/24 blackhole

# direct traffic addressed from a specific address to a specific sub-net
ip rule add priority 140 from 192.168.1.33/32 to 192.168.2.0/24 lookup default

# direct packet from specific IP to specific sub-net to table named test123
ip rule add priority 150 from 192.168.1.33 to 192.168.1.0/255.255.255.0 lookup test123

# silently drop any traffic attempting to reach between two sub-nets
ip rule add priority 160 blackhole from 192.168.1.0/24 to 172.16.11.0/24

# direct marked packets to table test123
ip rule add priority 170 from 192.168.1.0/24 fwmark 1 lookup test123

# set priority and filter specific addr to specific addr
ip rule add priority 180 from 192.168.1.33 to 192.168.1.200 lookup default

Rule Priority

Rule priorities are processed in order from lowest to highest number. Take a look at your current rules.

ip rule show

A basic IPv4 policy-rule list usually looks like this; network managers and VPN software may add other rules:

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

Rules are examined in priority order. A narrower selector at a larger priority number does not override an earlier matching rule whose action finds a route. Rule length has no role.

The policy rule determines which action to run. If that action is a routing-table lookup, destination longest-prefix selection occurs within that table, not across rules.

  1. Check rules from lower priority number to higher.
  2. Continue past selectors that do not match.
  3. For a matching lookup, return a usable route if found. An empty lookup or a throw route can continue to a later rule.
  4. Do not continue after an explicit terminal action such as an unreachable or prohibit result.

The same three default IPv4 rules are shown again below. Inspect your own system before choosing priorities.

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

For example, consider a broad priority-100 rule that finds a default route in one table, and a narrower priority-200 rule that points to another table. Traffic that matches both rules uses the earlier table’s default route. It reaches the later lookup only if the first lookup does not produce a terminal result. This is why the ordering of broad and narrow rules matters.

Don’t forget to pay attention to the order of your rules and the order of when/how each table is called.

If your server doesn’t have custom tables yet you may view your current RPDB rule collection using this command:

ip rule show

Changes to rules or routes update live kernel state. To persist them across reboot, save them in your network configuration or have a manager reapply them. The old global IPv4 route cache was removed in Linux 3.6 (ip-route(8)); flushing a cache is not a general prerequisite for applying changes.

How Default RPDB Rules Function

While reading the Rule Priority section above, you might have wondered how the default ip rules can possibly be useful. After all, they are simply three “from” rules. Let’s review them.

ip rule show

Displays:

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

The default rules show from all, meaning their selectors do not restrict the source prefix. Each lookup still looks for a route to the destination. The local, main and default tables are consulted in that order when earlier lookups do not provide a terminal result; the kernel does not compare every route across all three tables.

Routing Marked Packets with fwmark

A packet mark is local kernel metadata. iptables can set it, and so can other interfaces, including nftables. ip rule can match it to select a routing action.

A mark value may be written in decimal or hexadecimal. Hexadecimal uses the 0x prefix. These are alternative spellings of the same rule; install only one:

ip rule add priority 190 fwmark 32 lookup mytable
ip rule add priority 190 fwmark 0x20 lookup mytable

Either statement selects table mytable for a mark value of 32. Whether that lookup returns a route depends on the table contents and any earlier rules. The iproute2 parser accepts the mark as a numeric value; x20 without the leading zero is invalid.