LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

ip-route

manages the kernel routing table

TLDR

Show routing table
$ ip route
copy
Add default gateway
$ sudo ip route add default via [gateway_ip]
copy
Add default via interface
$ sudo ip route add default dev [eth0]
copy
Add static route
$ sudo ip route add [10.0.0.0/24] via [gateway_ip] dev [eth0]
copy
Add a route with a metric and preferred source address
$ sudo ip route add [10.0.0.0/24] via [gateway_ip] metric [100] src [local_ip]
copy
Replace a route, adding it if it does not exist (idempotent)
$ sudo ip route replace default via [gateway_ip] dev [eth0]
copy
Delete route
$ sudo ip route delete [10.0.0.0/24] dev [eth0]
copy
Change route
$ sudo ip route change [10.0.0.0/24] via [gateway_ip] dev [eth0]
copy
Add a blackhole route that silently drops traffic
$ sudo ip route add blackhole [203.0.113.0/24]
copy
Add a multipath (ECMP) default route
$ sudo ip route add default nexthop via [gw1] weight 1 nexthop via [gw2] weight 1
copy
Get route to destination
$ ip route get [destination_ip]
copy
Show specific table
$ ip route list table [100]
copy
Flush all routes of a device
$ sudo ip route flush dev [eth0]
copy
Save and restore the routing table
$ ip route save > [routes.bin] && sudo ip route restore < [routes.bin]
copy

SYNOPSIS

ip [options] route { command | help }ip route { show | flush } SELECTORip route { add | del | change | append | replace } ROUTEip route get ADDRESS [options]ip route { save | restore }

DESCRIPTION

ip route manages the kernel routing table. It can add, delete, and modify routes, as well as query which route the kernel will use for a specific destination.

PARAMETERS

list (or no command)

Display the routing table
add
Add a new route
delete
Remove a route
change
Modify an existing route
replace
Change or add if not exists
append
Add a route even if one with the same key exists (for IPv6, adds a nexthop to a multipath route)
flush SELECTOR
Remove all routes matching the selector
save, restore
Dump routes as raw netlink data to stdout, or restore them from stdin
get address
Show route for a specific destination
default
Default gateway route
via gateway
Specify next-hop gateway
dev interface
Specify output interface
table id
Work with a specific routing table
src address
Preferred source address for traffic to this destination
metric number
Route priority; lower values are preferred
proto rtproto
Routing protocol identifier: kernel, boot, static, dhcp, or a number
scope scope
Route scope: global, link, or host
onlink
Pretend the next hop is directly attached even if it does not match any prefix
nexthop NH
Define a next hop of a multipath route (can be repeated, with optional weight)
mtu number
Set the path MTU for the route
type
Route type: unicast (default), local, broadcast, multicast, blackhole, unreachable, prohibit, throw

INSTALL

sudo apt install iproute2
copy
sudo pacman -S iproute2
copy
sudo apk add iproute2-minimal
copy
sudo zypper install iproute2
copy
brew install iproute2
copy
nix profile install nixpkgs#iproute2
copy

CAVEATS

Routes added are not persistent; use network configuration files for persistence. Multiple routing tables can be used with policy routing. The default table is "main".

HISTORY

ip route is part of iproute2, written by Alexey Kuznetsov in the late 1990s, replacing the deprecated route command from net-tools.

SEE ALSO

RESOURCES

Copied to clipboard
Kai