How to Add Host, Network, and Default Routes in Linux Using the route Command
Learn step-by-step how to configure Linux routing for different subnets by adding host routes, network routes, and default routes with the 'route' command, including syntax details, example commands, and explanations of routing table fields such as Destination, Gateway, Netmask, and Flags.
Understanding Linux Routing Commands
Linux uses the route command to add or delete routing entries, allowing communication between different subnets.
Command Syntax
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 host
target : destination network or host
netmask : network mask of the destination
gw : gateway through which packets are sent
dev : network interface
1. Adding a Host Route
To enable host 192.168.2.10 to ping host 192.168.0.8 via router 2, add the following rule on the source host: route add -host 192.168.0.8 gw 192.168.2.1 dev eth0 This directs traffic for 192.168.0.8 to the gateway 192.168.2.1. Use route to verify the entry.
2. Adding a Network Route
When a host needs to reach an entire subnet, add a network route instead of individual host routes:
route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.2.1 dev eth0This routes all traffic to the 0‑segment through the same gateway. The routing table shows the entry with appropriate flags.
3. Adding a Default Route
To forward all non‑local traffic to a default gateway, use: route add default gw 192.168.2.1 dev eth0 The default route directs any destination outside the 2‑segment to the specified gateway.
Routing Table Fields Explained
Destination : target network or host; 0.0.0.0 denotes the default gateway.
Gateway : address of the gateway; 0.0.0.0 means the destination is on the same subnet.
Genmask : netmask for the destination; 255.255.255.255 for a host, 0.0.0.0 for default.
Flags : indicators such as U (up), G (gateway), H (host), R (reinstated), D (dynamic), M (modified), ! (reject).
Metric : route distance (used in large LAN/WAN).
Iface : network interface name, e.g., eth0.
To remove routes, use route del with the same parameters, e.g., route del 192.168.0.8, route del -net 192.168.0.0/24 gw 192.168.2.1, or route del default.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
