Routes and paths
ip route
Shows the routing table, asks which route a destination takes, and adds or removes routes.
Every packet leaves by the most specific route that matches its destination; default matches
everything else. ip route get ADDRESS is the most useful form in troubleshooting: it does the lookup for you and
prints the next hop, the card and the source address the kernel would use. A route via a gateway must point at an
address on one of your own networks.
ip route command usage
ip [-6] route [show] ip route get ADDRESS ip route add|replace|del PREFIX via GATEWAY [dev CARD] [metric N] ip route help
Options
-
help - print every form of the command and the words a route takes, and stop
-
get 10.0.2.10 - which route, next hop and card a packet to that address takes
-
add PREFIX via GW - send PREFIX to the neighbour GW
-
replace - add, or change the route if it already exists
-
metric 100 - the route's cost: of two routes for the same prefix, the lower metric is used
-
del PREFIX via GW - delete a route; via says which, when the prefix has several
-
show default - only the default route
-
-6 - the IPv6 table
ip route examples
-
ip route - Prints the routing table: one line per destination network and where it goes.
-
ip route get 10.0.2.10 - Shows which next hop, card and source address a packet to 10.0.2.10 would use.
-
ip route show default - Shows only the default route, the gateway for everything not listed.
-
ip route add default via 10.0.1.2 - Sends every packet with no better route to the router at 10.0.1.2.
-
ip route add 10.0.3.0/24 via 10.0.12.2 - Sends packets for 10.0.3.0/24 to the next router, 10.0.12.2.
-
ip route del 10.0.3.0/24 - Removes the route for 10.0.3.0/24.
-
ip -6 route - Prints the IPv6 routing table.
-
ip route add 10.0.3.0/24 via 10.0.12.2 dev eth1 - Adds a route and names the card, for a next hop reachable on more than one.
-
ip route replace default via 10.0.1.3 - Changes the default gateway in one step, adding the route if there was none.
-
ip route add 10.0.3.0/24 via 10.0.13.2 metric 200 - Adds a backup route: the lower metric wins while both exist.
-
ip route add default nexthop via 10.0.1.2 weight 1 nexthop via 10.0.1.3 weight 1 - Spreads traffic over two gateways (ECMP), per flow.
-
ip route add blackhole 192.0.2.0/24 - Drops every packet for 192.0.2.0/24 without an answer.
-
ip route add 10.0.3.0/24 via 10.0.12.2 table 100 - Adds the route to table 100, used only for packets that an ip rule sends there.
-
ip route show table all - Prints every table, local and policy ones included, not only main.
-
ip route show proto bgp - Lists only the routes a BGP daemon installed: proto says who added a route (kernel, static, dhcp, ospf, bgp).
Reading the output
$ ip route; ip route get 10.0.2.10
default via 10.0.1.2 dev eth0
10.0.1.0/24 dev eth0 proto kernel scope link src 10.0.1.10
10.0.2.10 via 10.0.1.2 dev eth0 src 10.0.1.10 uid 0
cache default via 10.0.1.2: anything not matched below goes to the router at 10.0.1.2. No such line: only your own network is reachable.10.0.1.0/24 dev eth0 proto kernel scope link: your own network, reached directly. The kernel adds it when the address is set.ip route getanswersvia 10.0.1.2 dev eth0 src 10.0.1.10: the next hop, the card, and the source address replies will come back to.
Pitfalls
- A router needs routes back as well: the reply to 10.0.1.10 is routed from the far side, by other tables.
Nexthop has invalid gateway: theviaaddress is not on any of your own networks.- On a router running OSPF or BGP, routes also come from the daemon: see them with
vtysh -c 'show ip route'.