Operations 6 min read

How to Add and Manage Linux Network Routes for Different Subnets

This guide explains how to configure host, network, and default routes on Linux using the route command, covering syntax, option meanings, example commands, and how to view or delete routes for communication across multiple subnets.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Add and Manage Linux Network Routes for Different Subnets

Two subnets (0 and 2) are connected via Router 1 and Router 2; to enable direct communication between them you must add appropriate routes on Linux.

The basic syntax of the Linux route command is:

route [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]

Key options:

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 : network mask for the destination

gw : gateway through which packets are sent

dev : network interface to use

1. Adding a Host Route

To allow host 192.168.2.10 to ping 192.168.0.8 via Router 2, add the following route on the source host:

route add -host 192.168.0.8 gw 192.168.2.1 dev eth0

This directs all traffic for 192.168.0.8 through gateway 192.168.2.1. Use route -n to view the entry. Delete it with:

route del 192.168.0.8

2. Adding a Network Route

Instead of adding a host route for each device, add a network route to reach the entire 0‑segment:

route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.2.1 dev eth0

This routes all traffic for the 192.168.0.0/24 network via the same gateway. Verify with route -n. Remove it with:

route del -net 192.168.0.0/24 gw 192.168.2.1

3. Adding a Default Route

To enable a host in the 2‑segment to reach any other network, set a default route: route add default gw 192.168.2.1 dev eth0 This forwards all non‑local traffic through 192.168.2.1. Delete the default route with: route del default Routing table fields explained:

Destination : target network or host (e.g., 0.0.0.0 for default gateway)

Gateway : next‑hop address; 0.0.0.0 means direct connection

Genmask : subnet mask (e.g., 255.255.255.255 for host, 0.0.0.0 for default)

Flags : U (up), G (gateway), H (host), R (reinstate), D (dynamic), M (modified), ! (reject)

Metric : route distance (used in large LAN/WAN)

Ref and Use : reference and usage counts (not used by Linux kernel)

Iface : interface name (e.g., eth0)

Images illustrating the routing tables and commands:

Original article: https://www.cnblogs.com/YYFaGe/p/16599675.html (copyright belongs to the original author).

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.

OperationsnetworkLinuxcommand-linesubnet
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.