# ip-rule(8) - man - phpman

> **TLDR:** IP routing policy database management.
>
- Display the routing policy:
  `ip {{ru|rule}}`
- Create a new generic routing rule with a higher priority than `main`:
  `sudo ip {{ru|rule}} {{a|add}} from all lookup {{table_id}}`
- Add a new rule based on packet source addresses:
  `sudo ip {{ru|rule}} {{a|add}} from {{192.168.178.2/32}} lookup {{table_id}}`
- Add a new rule based on packet destination addresses:
  `sudo ip {{ru|rule}} {{a|add}} to {{192.168.178.2/32}} lookup {{table_id}}`
- Delete a rule based on packet source addresses:
  `sudo ip {{ru|rule}} {{d|delete}} from {{192.168.178.2/32}}`
- Remove all routing rules:
  `sudo ip {{ru|rule}} {{f|flush}}`
- Save all rules to a file:
  `ip {{ru|rule}} {{s|save}} > {{path/to/ip_rules.dat}}`
- Restore all rules from a file:
  `sudo ip < {{path/to/ip_rules.dat}} {{ru|rule}} {{r|restore}}`

*Source: tldr-pages*

---

[IP-RULE(8)](https://www.chedong.com/phpMan.php/man/IP-RULE/8/markdown)                                      Linux                                     [IP-RULE(8)](https://www.chedong.com/phpMan.php/man/IP-RULE/8/markdown)



## NAME
       ip-rule - routing policy database management

## SYNOPSIS
       **ip** [ _OPTIONS_ ] **rule** { _COMMAND_ | **help** }


       **ip** **rule** [ **list** [ _SELECTOR_ ]]

       **ip** **rule** { **add** | **del** } _SELECTOR_ _ACTION_

       **ip** **rule** { **flush** | **save** | **restore** }

       _SELECTOR_ := [ **not** ] [ **from** _PREFIX_ ] [ **to** _PREFIX_ ] [ **tos** _TOS_ ] [ **fwmark** _FWMARK_[**/**_MASK_] ] [ **iif**
               _STRING_ ] [ **oif** _STRING_ ] [ **pref** _NUMBER_ ] [ _l3mdev_ ] [ **uidrange** _NUMBER_-_NUMBER_ ] [ **ip**‐‐
               **proto** _PROTOCOL_ ] [ **sport** [ _NUMBER_ | _NUMBER_-_NUMBER_ ] ] [ **dport** [ _NUMBER_ | _NUMBER_-_NUM__‐
               _BER_ ] ] [ **tun**___**id** _TUN_ID_ ]


       _ACTION_ := [ **table** _TABLE_ID_ ] [ **protocol** _PROTO_ ] [ **nat** _ADDRESS_ ] [ **realms** [_SRCREALM_**/**]_DSTREALM_
               ] [ **goto** _NUMBER_ ] _SUPPRESSOR_

       _SUPPRESSOR_ := [ **suppress**___**prefixlength** _NUMBER_ ] [ **suppress**___**ifgroup** _GROUP_ ]

       _TABLE_ID_ := [ **local** | **main** | **default** | _NUMBER_ ]


## DESCRIPTION
       _ip_ _rule_ manipulates rules in the routing policy database control the route selection algo‐
       rithm.


       Classic routing algorithms used in the Internet make routing decisions based only on the des‐
       tination address of packets (and in theory, but not in practice, on the TOS field).


       In some circumstances we want to route packets differently depending not only on destination
       addresses, but also on other packet fields: source address, IP protocol, transport protocol
       ports or even packet payload.  This task is called 'policy routing'.


       To solve this task, the conventional destination based routing table, ordered according to
       the longest match rule, is replaced with a 'routing policy database' (or RPDB), which selects
       routes by executing some set of rules.


       Each policy routing rule consists of a **selector** and an **action** **predicate.**  The RPDB is scanned
       in order of decreasing priority (note that lower number means higher priority, see the de‐
       scription of _PREFERENCE_ below). The selector of each rule is applied to {source address, des‐
       tination address, incoming interface, tos, fwmark} and, if the selector matches the packet,
       the action is performed. The action predicate may return with success.  In this case, it will
       either give a route or failure indication and the RPDB lookup is terminated. Otherwise, the
       RPDB program continues with the next rule.


       Semantically, the natural action is to select the nexthop and the output device.


       At startup time the kernel configures the default RPDB consisting of three rules:


       1.     Priority: 0, Selector: match anything, Action: lookup routing table **local** (ID 255).
              The **local** table is a special routing table containing high priority control routes for
              local and broadcast addresses.


       2.     Priority: 32766, Selector: match anything, Action: lookup routing table **main** (ID 254).
              The **main** table is the normal routing table containing all non-policy routes. This rule
              may be deleted and/or overridden with other ones by the administrator.


       3.     Priority: 32767, Selector: match anything, Action: lookup routing table **default** (ID
              253).  The **default** table is empty. It is reserved for some post-processing if no pre‐
              vious default rules selected the packet.  This rule may also be deleted.


       Each RPDB entry has additional attributes. F.e. each rule has a pointer to some routing ta‐
       ble. NAT and masquerading rules have an attribute to select new IP address to translate/mas‐
       querade. Besides that, rules have some optional attributes, which routes have, namely **realms**.
       These values do not override those contained in the routing tables. They are only used if the
       route did not select any attributes.


       The RPDB may contain rules of the following types:

              **unicast** - the rule prescribes to return the route found in the routing table refer‐
              enced by the rule.

              **blackhole** - the rule prescribes to silently drop the packet.

              **unreachable** - the rule prescribes to generate a 'Network is unreachable' error.

              **prohibit** - the rule prescribes to generate 'Communication is administratively prohib‐
              ited' error.

              **nat** - the rule prescribes to translate the source address of the IP packet into some
              other value.


### ip rule add - insert a new rule

### ip rule delete - delete a rule

              **type** _TYPE_ **(default)**
                     the type of this rule. The list of valid types was given in the previous sub‐
                     section.


              **from** _PREFIX_
                     select the source prefix to match.


              **to** _PREFIX_
                     select the destination prefix to match.


              **iif** _NAME_
                     select the incoming device to match. If the interface is loopback, the rule
                     only matches packets originating from this host. This means that you may create
                     separate routing tables for forwarded and local packets and, hence, completely
                     segregate them.


              **oif** _NAME_
                     select the outgoing device to match. The outgoing interface is only available
                     for packets originating from local sockets that are bound to a device.


              **tos** _TOS_

              **dsfield** _TOS_
                     select the TOS value to match.


              **fwmark** _MARK_
                     select the **fwmark** value to match.


              **uidrange** _NUMBER-NUMBER_
                     select the **uid** value to match.


              **ipproto** _PROTOCOL_
                     select the ip protocol value to match.


              **sport** _NUMBER_ _|_ _NUMBER-NUMBER_
                     select the source port value to match. supports port range.


              **dport** _NUMBER_ _|_ _NUMBER-NUMBER_
                     select the destination port value to match. supports port range.


              **priority** _PREFERENCE_
                     the priority of this rule.  _PREFERENCE_ is an unsigned integer value, higher
                     number means lower priority, and rules get processed in order of increasing
                     number. Each rule should have an explicitly set _unique_ priority value.  The op‐
                     tions preference and order are synonyms with priority.


              **table** _TABLEID_
                     the routing table identifier to lookup if the rule selector matches.  It is
                     also possible to use lookup instead of table.


              **protocol** _PROTO_
                     the routing protocol who installed the rule in question.  As an example when
                     zebra installs a rule it would get RTPROT_ZEBRA as the installing protocol.


              **suppress**___**prefixlength** _NUMBER_
                     reject routing decisions that have a prefix length of NUMBER or less.


              **suppress**___**ifgroup** _GROUP_
                     reject routing decisions that use a device belonging to the interface group
                     GROUP.


              **realms** _FROM/TO_
                     Realms to select if the rule matched and the routing table lookup succeeded.
                     Realm _TO_ is only used if the route did not select any realm.


              **nat** _ADDRESS_
                     The base of the IP address block to translate (for source addresses).  The _AD__‐
                     _DRESS_ may be either the start of the block of NAT addresses (selected by NAT
                     routes) or a local host address (or even zero).  In the last case the router
                     does not translate the packets, but masquerades them to this address.  Using
                     map-to instead of nat means the same thing.

                     **Warning:** Changes to the RPDB made with these commands do not become active im‐
                     mediately. It is assumed that after a script finishes a batch of updates, it
                     flushes the routing cache with **ip** **route** **flush** **cache**.

### ip rule flush - also dumps all the deleted rules.

              **protocol** _PROTO_
                     Select the originating protocol.

### ip rule show - list rules
              This command has no arguments.  The options list or lst are synonyms with show.


### ip rule save

              **protocol** _PROTO_
                     Select the originating protocol.

       save rules table information to stdout
              This command behaves like **ip** **rule** **show** except that the output is raw data suitable for
              passing to **ip** **rule** **restore**.


### ip rule restore
              restore rules table information from stdin
              This command expects to read a data stream as returned from **ip** **rule** **save**.  It will at‐
              tempt to restore the rules table information exactly as it was at the time of the
              save. Any rules already in the table are left unchanged, and duplicates are not ig‐
              nored.


## SEE ALSO
       [**ip**(8)](https://www.chedong.com/phpMan.php/man/ip/8/markdown)


## AUTHOR
       Original Manpage by Michail Litvak <<mci@owl.openwall.com>>



iproute2                                     20 Dec 2011                                  [IP-RULE(8)](https://www.chedong.com/phpMan.php/man/IP-RULE/8/markdown)
