Operations 7 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Routing: Add Host, Network, and Default Routes Step‑by‑Step

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 eth0

This 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 default

5. 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

Network diagram showing 0 and 2 subnets and routers
Network diagram showing 0 and 2 subnets and routers
Route command output after adding a host route
Route command output after adding a host route
Route command output after adding a network route
Route command output after adding a network route
Route command output after adding a default route
Route command output after adding a default route
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxroutingsubnetroute
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.