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.
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 eth0This 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.82. 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 eth0This 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.13. 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).
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.
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.
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.
