How to Add Host, Network, and Default Routes in Linux Using the route Command
This guide explains how to configure Linux routing by adding host, network, and default routes with the route command, detailing the command syntax, example commands, route table fields, and how to delete routes when they are no longer needed.
The article demonstrates configuring Linux routing across different subnets by using the route command to add host, network, and default routes.
Route command syntax
The general form is:
route [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]add : add a routing rule
del : delete a routing rule
-net : target is a network
-host : target is a single host
target : destination network or host
netmask : subnet mask for the destination
gw : gateway through which packets are sent
dev : network interface (e.g., eth0)
1. Adding a host route
To enable host 192.168.2.10 to ping host 192.168.0.8 via router 2, run:
route add -host 192.168.0.8 gw 192.168.2.1 dev eth0This directs all traffic for 192.168.0.8 through gateway 192.168.2.1. Verify with route -n (or simply route) to see the new entry.
2. Adding a network route
When a host needs to reach an entire subnet (e.g., the 0.0/24 network), adding a single network route is more efficient than adding many host routes. Use:
route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.2.1 dev eth0This tells the system that any destination in 192.168.0.0/24 should be sent to gateway 192.168.2.1. The route table will show the entry with the appropriate netmask.
3. Adding a default route
To forward all traffic destined for networks outside the current subnet, add a default route: route add default gw 192.168.2.1 dev eth0 The default route (Destination 0.0.0.0) ensures that any non‑local traffic is sent to the specified gateway.
Understanding the route table fields
Destination : target network or host; 0.0.0.0 denotes the default gateway.
Gateway : address of the next hop; 0.0.0.0 means the destination is on the same LAN.
Genmask : subnet mask; 255.255.255.255 for a host route, 0.0.0.0 for the default route.
Flags :
U – route is up
G – uses a gateway
H – host route
R – dynamically created (redirect)
D – dynamically installed by a daemon
M – modified by a daemon
! – reject route
Metric : hop count (not used by the Linux kernel).
Ref : reference count (informational only).
Use : number of lookups for this entry.
Iface : network interface name, e.g., eth0.
Deleting routes
To remove a specific host route: route del 192.168.0.8 To delete a network route: route del -net 192.168.0.0/24 gw 192.168.2.1 To delete the default route: route del default After deletion, verify the routing table again to ensure the entry has been removed.
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.
