Master Linux Routing: Add Host, Network, and Default Routes Step‑by‑Step
This guide explains how to configure Linux routing for multiple subnets by using the route command to add host routes, network routes, and default routes, detailing each option, required parameters, and how to verify or delete the entries.
Overview
The article demonstrates how to enable communication between different IP subnets on Linux by manually adding routing entries with the route command. It covers host routes, network routes, and default routes, explains each command option, and shows how to view and remove routes.
Route Command Syntax
The basic syntax is:
route [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]add : create 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 to use (e.g., eth0)
1. Adding a Host Route
To allow a host in the 192.168.2.0/24 subnet (e.g., 192.168.2.10) to reach a host in the 192.168.0.0/24 subnet (e.g., 192.168.0.8) via router 192.168.2.1, run on the source host: route add -host 192.168.0.8 gw 192.168.2.1 dev eth0 This directs all traffic destined for 192.168.0.8 through the gateway 192.168.2.1. The current routing table can be displayed with route -n to verify the entry.
2. Adding a Network Route
When a host needs to reach an entire subnet, adding a separate host route for each address is impractical. Instead, add a network route that covers the whole subnet:
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 within 192.168.0.0/24 should be sent to gateway 192.168.2.1. The route can be verified with route -n.
3. Adding a Default Route
If a host in the 192.168.2.0/24 network should forward all traffic for unknown destinations to the same gateway, add a default route: route add default gw 192.168.2.1 dev eth0 The default route directs any non‑local traffic to the specified gateway.
4. Deleting Routes
Delete a host route: route del 192.168.0.8 Delete a network route: route del -net 192.168.0.0/24 gw 192.168.2.1 Delete the default route:
route del default5. Understanding the Routing Table Fields
The output of route -n includes several columns:
Destination : target network or host (e.g., 0.0.0.0 for the default gateway)
Gateway : next‑hop address; 0.0.0.0 means the destination is on the same LAN
Genmask : subnet mask for the destination (e.g., 255.255.255.255 for a host, 0.0.0.0 for default)
Flags : status bits such as U (up), G (gateway), H (host), etc.
Metric : route cost (used in larger networks)
Ref and Use : internal kernel counters (generally not used)
Iface : network interface name (e.g., eth0)
Illustrations
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.
