How to Add Host, Network, and Default Routes in Linux with the route Command
This guide explains how to configure inter‑subnet communication on Linux by adding host, network, and default routes using the route command, covering syntax, required parameters, example commands, and how to verify or delete the routes.
Two subnets (0.0.0.0/24 and 2.0.0.0/24) are connected by Router 1 (0‑net) and Router 2 (2‑net). To enable direct communication between hosts on different subnets, static routes must be added on the Linux hosts.
Route command syntax
The generic form of the Linux route command is:
route [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]add : create a new routing entry
del : remove an existing routing entry
-net : target is a network
-host : target is a single host
target : destination network or host address
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 host 192.168.2.10 to ping host 192.168.0.8 via Router 2, run on the source host:
route add -host 192.168.0.8 gw 192.168.2.1 dev eth0This directs traffic for 192.168.0.8 to the gateway 192.168.2.1. Verify with:
route -nThe routing table fields are:
Destination : target network or host (e.g., 0.0.0.0 for default gateway)
Gateway : gateway address; 0.0.0.0 means the destination is on the same LAN
Genmask : subnet mask (255.255.255.255 for a host, 0.0.0.0 for default)
Flags :
U – route is up
G – uses a gateway
H – target is a host
R – restored from a dynamic route
D – installed by a daemon
M – modified by a daemon
! – reject route
Metric : cost of the route (used in larger networks)
Ref and Use : internal kernel counters
Iface : network interface name (e.g., eth0)
To delete the host route:
route del 192.168.0.82. Adding a network route
If 192.168.2.10 needs to reach all hosts in the 0‑net, add a single network route on the host:
route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.2.1 dev eth0This tells the kernel that any address in 192.168.0.0/24 should be sent to gateway 192.168.2.1.
To delete the network route:
route del -net 192.168.0.0/24 gw 192.168.2.13. Adding a default route
To allow any traffic destined for networks other than the 2‑net to be forwarded via Router 2, set a default route:
route add default gw 192.168.2.1 dev eth0The default route means all non‑local traffic is sent to 192.168.2.1. To remove it: route del default These commands enable precise control over inter‑subnet communication in Linux environments.
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.
