Master Linux ‘route’ Command: Display, Add, Delete, and Persist Routes
This guide explains how to install the net-tools package, use the Linux route command to view the kernel IP routing table, add or delete static routes, make routes persistent, and interpret the output fields with practical examples.
Introduction
The route command manipulates the kernel IP routing table. It can display the current routes or add/delete static routes. Routes created from the command line are volatile and disappear after a network‑interface or system reboot unless they are saved (e.g., in /etc/rc.local).
Installation
yum install net-toolsSyntax
route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]Common options
-f: flush all routing entries -p: make the route permanent when used with
add -n: numeric output (do not resolve names) -v: verbose output -c: show more information -F: display sent‑packet statistics -C: display route cache
Sub‑commands
add: add a new route del: delete an existing route -net: destination is a network -host: destination is a host netmask: required when adding a network route gw: specify the gateway (must be reachable) metric: set the route metric (hop count)
Output columns
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.209.2 0.0.0.0 UG 100 0 0 ens33
10.10.0.0 0.0.0.0 255.255.255.0 ! 0 0 0 0
192.168.209.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33Column meanings:
Destination : target network or host
Gateway : next‑hop address or ‘*’ if none
Genmask : subnet mask (255.255.255.255 for host, 0.0.0.0 for default)
Flags : U (up), H (host), G (gateway), R (reinstate), D (dynamic), M (modified), A (addrconf), C (cache), ! (reject)
Metric : distance to the target (used by routing daemons)
Ref : reference count (unused in Linux kernel)
Use : number of lookups (helps identify cache hits/misses)
Iface : outgoing interface
Examples
Query the routing table
# route -nAdd a static route
# route add -net 10.10.10.0 gw 192.168.209.2 netmask 255.255.255.0 dev ens33The new entry appears with the UG flag.
Delete a route
# route del -net 10.10.10.0 gw 192.168.209.2 netmask 255.255.255.0 dev ens33The entry disappears from the table.
Add and remove a reject (blackhole) route
# route add -net 10.10.10.0 netmask 255.255.255.0 reject
# route -n # shows flag ! for the reject entry
# route del -net 10.10.10.0 netmask 255.255.255.0 rejectThe ! flag indicates traffic to that network will be dropped.
Reference
Original tutorial URL: https://www.cnblogs.com/du-z/p/16332641.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
