Linux Route Command - Network Routing Table Management

Learn to manage Linux network routing tables using the

Linux Route Command

The route command in Linux is a powerful utility used to view and manipulate the IP routing table. The routing table is essential for directing network traffic, determining the path packets take to reach their destination. Understanding and effectively using the route command is crucial for network administrators and developers working with network configurations.

Displaying Network Routing Table

To view the current routing table, you can use the route command with the -n option. This option ensures that IP addresses are displayed numerically rather than trying to resolve them into hostnames, which is often faster and more reliable.

route -n

Adding and Deleting Network Routes

The route command allows for dynamic manipulation of the routing table. You can add new routes or delete existing ones to control network traffic flow.

Adding a Default Gateway

A default gateway is the router that your system sends traffic to when it doesn't have a specific route for the destination network. This is a common configuration for most home and office networks.

route add default gateway 192.168.0.1

Adding a Specific Network Route

You can add a route to a specific network, specifying the network address, netmask, and the interface or gateway to use.

# To add the normal loopback entry, using netmask 255.0.0.0 and associated with
# the "lo" device (assuming this device was previously set up correctly with
# ifconfig(8)):
route add -net 127.0.0.0 netmask 255.0.0.0 dev lo

# To add a route to the local network 192.56.76.x via "eth0". The word "dev" can be omitted here:
route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0

Deleting Routes

Removing routes is as straightforward as adding them. This is useful for troubleshooting or reconfiguring network paths.

# To delete the current default route, which is labeled "default" or 0.0.0.0 in the destination field of the current routing table:
route del default

Advanced Routing Scenarios

The route command supports more complex routing configurations, including gateway-based routes and routes for specific devices or interfaces.

Gateway-Based Default Route

This command adds a default route that will be used if no other specific route matches the destination. Packets will be sent through the specified gateway.

# To add a default route (which will be used if no other route matches). All
# packets using this route will be gatewayed through "mango-gw". The device
# which will actually be used for that route depends on how we can reach
# "mango-gw" - the static route to "mango-gw" will have to be set up before:
route add default gw mango-gw

Routing via Specific Interfaces

You can define routes that use specific network interfaces, such as SLIP interfaces.

# To add the route to the "ipx4" host via the SLIP interface (assuming that "ipx4" is the SLIP host):
route add ipx4 sl0

Rejecting Routes

A rejecting route is used to explicitly block traffic to a certain network. This can be useful for security purposes.

# To install a rejecting route for the private network "10.x.x.x.":
route add -net 10.0.0.0 netmask 255.0.0.0 reject

Multicast Routing

The route command can also be used to configure multicast routing, directing multicast traffic to specific interfaces.

# This is an obscure one documented so people know how to do it. This sets all
# of the class D (multicast) IP routes to go via "eth0". This is the correct
# normal configuration line with a multicasting kernel:
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

External Resources