CommandsLinux

Route Command on Linux with Examples

Route Command on Linux

Network routing forms the backbone of communication between systems in Linux environments. The route command serves as a fundamental tool for displaying and manipulating IP routing tables, enabling administrators to control how network traffic flows through their infrastructure. Understanding this command is essential for effective network management and troubleshooting connectivity issues.

This comprehensive guide explores the route command’s functionality, syntax, and practical applications. You’ll learn to view routing tables, add static routes, remove unnecessary entries, and troubleshoot common networking problems. Whether you’re managing a single server or complex enterprise networks, mastering the route command enhances your ability to maintain optimal network performance.

Understanding Linux Routing Fundamentals

What is IP Routing

IP routing represents the process by which data packets travel from source to destination across networks. The Linux kernel maintains a routing table that determines the best path for network traffic based on destination addresses. This table contains entries specifying which interface and gateway to use for reaching particular networks or hosts.

Static routing involves manually configured routes that remain persistent until explicitly changed. Dynamic routing relies on protocols that automatically adjust routes based on network conditions. The route command primarily handles static routing configurations, providing administrators with direct control over traffic flow patterns.

Route Command Basics

The route command interacts directly with the kernel’s routing table, allowing you to display current routes and modify routing behavior. This utility requires administrative privileges for making changes, though viewing routing information is available to standard users. The command integrates seamlessly with the Linux networking stack, ensuring changes take effect immediately.

Modern Linux distributions include the route command as part of the net-tools package. While newer alternatives like ip route exist, many administrators continue using route due to its straightforward syntax and widespread familiarity. Understanding both commands proves valuable for comprehensive network management.

Route Command Syntax and Options

Basic Syntax Structure

The route command follows a consistent structure: route [options] [command] [arguments]. Basic operations include viewing the routing table, adding new routes, and deleting existing entries. Optional parameters modify command behavior and output formatting.

Common command operations include:

  • route – Display current routing table
  • route add – Add new route entry
  • route del – Remove existing route
  • route -n – Show numerical addresses

Essential Command Options

The -n flag displays numerical IP addresses instead of resolving hostnames, providing faster output and avoiding DNS lookup delays. This option proves particularly useful when troubleshooting network connectivity issues or working with systems that have DNS resolution problems.

The -e flag produces extended output format with additional routing information. Network administrators use -A to specify address families, while -net and -host parameters distinguish between network and host-specific routes. Understanding these options enables precise routing table management.

Viewing and Understanding Routing Tables

Displaying Current Routes

Execute route without arguments to view the current routing table. The output displays destination networks, gateways, netmasks, flags, and associated interfaces. This information helps you understand how your system routes different types of network traffic.

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

Using route -n provides numerical output without hostname resolution:

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

Routing Table Column Explanation

The Destination column identifies target networks or hosts. Entries showing 0.0.0.0 represent the default route, matching any destination not explicitly listed in the routing table. Network addresses use CIDR notation or traditional subnet masks.

Gateway columns specify the next-hop router for reaching destinations. A gateway value of * or 0.0.0.0 indicates direct connectivity to the destination network. Actual IP addresses represent intermediate routers that forward packets toward their final destinations.

The Genmask column displays subnet masks in dotted decimal notation. These masks determine which portion of the destination address represents the network and which part identifies specific hosts. Flags provide additional routing information:

  • U: Route is up and active
  • G: Route uses a gateway
  • H: Target is a host, not a network
  • D: Route created by ICMP redirect
  • M: Route modified by ICMP redirect

Adding Static Routes

Adding Network Routes

Network routes direct traffic destined for entire subnets through specific gateways. The basic syntax for adding network routes is:

route add -net <network_address> netmask <subnet_mask> gw <gateway_ip> [interface]

For example, to route traffic for the 10.0.2.0/24 network through gateway 192.168.0.1:

sudo route add -net 10.0.2.0 netmask 255.255.255.0 gw 192.168.0.1 eth0

This command creates a routing entry directing all traffic destined for IP addresses between 10.0.2.1 and 10.0.2.254 through the specified gateway. The interface parameter is optional when the system can automatically determine the appropriate network interface.

Adding Host Routes

Host routes target specific IP addresses rather than entire networks. These routes prove useful for directing traffic to particular servers through dedicated paths or avoiding problematic network segments.

sudo route add -host 10.0.2.15 gw 192.168.0.1 eth0

This example creates a host-specific route for IP address 10.0.2.15. Host routes take precedence over network routes during routing table lookups, allowing fine-grained traffic control for critical systems.

Default Route Configuration

Default routes handle traffic when no specific route matches the destination address. Most systems require a default route for internet connectivity and general network access.

sudo route add default gw 192.168.1.1 eth0

This command establishes 192.168.1.1 as the default gateway. Multiple default routes can exist with different metrics, where lower values indicate higher priority during route selection.

Removing and Modifying Routes

Deleting Specific Routes

Remove routing entries using the route del command with syntax mirroring the add operation. Specify the exact destination, netmask, and gateway used during route creation:

sudo route del -net 10.0.2.0 netmask 255.255.255.0 gw 192.168.0.1

For host routes, use similar syntax:

sudo route del -host 10.0.2.15 gw 192.168.0.1

Delete default routes by specifying the default destination:

sudo route del default

Modifying Existing Routes

Route modifications typically involve deleting the existing entry and adding a replacement with updated parameters. Direct route modification isn’t supported through the route command, requiring a two-step process:

sudo route del -net 10.0.2.0 netmask 255.255.255.0 gw 192.168.0.1
sudo route add -net 10.0.2.0 netmask 255.255.255.0 gw 192.168.0.2

This approach ensures accurate routing table updates and prevents conflicts between old and new route entries.

Advanced Route Command Usage

Working with Multiple Interfaces

Multi-homed systems with multiple network interfaces require careful route management to ensure traffic uses appropriate paths. Specify interfaces explicitly when adding routes to systems with complex networking configurations:

sudo route add -net 172.16.0.0 netmask 255.255.0.0 gw 172.16.1.1 eth1
sudo route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.0.0.1 eth0

These commands direct different network ranges through specific interfaces, enabling efficient traffic segregation and load distribution.

Route Metrics and Priorities

Route metrics influence path selection when multiple routes exist for the same destination. Lower metric values indicate preferred routes during the selection process. While the route command has limited metric support, understanding this concept helps with route prioritization:

sudo route add default gw 192.168.1.1 metric 100

Persistent Route Configuration

Standard route commands create temporary entries that disappear after system reboots. Create persistent routes by modifying system configuration files or using distribution-specific tools.

On Red Hat-based systems, create route configuration files in /etc/sysconfig/network-scripts/:

sudo vi /etc/sysconfig/network-scripts/route-eth0

Add route entries in the format:

192.168.2.0/24 via 192.168.1.254 dev eth0
10.0.0.0/8 via 192.168.1.1 dev eth0

On Debian-based systems, modify /etc/network/interfaces or use the /etc/network/if-up.d/ directory for custom route scripts.

Practical Examples and Use Cases

Common Networking Scenarios

Scenario 1: Connecting Branch Offices

A company needs to connect its main office (192.168.1.0/24) with a branch office (192.168.100.0/24) through a VPN gateway at 192.168.1.254:

sudo route add -net 192.168.100.0 netmask 255.255.255.0 gw 192.168.1.254

Scenario 2: Database Server Access

Direct critical database traffic through a dedicated network path for improved performance and security:

sudo route add -host 192.168.50.10 gw 192.168.50.1 eth1

Scenario 3: Internet Gateway Redundancy

Configure backup internet connectivity through a secondary gateway:

sudo route add default gw 192.168.1.1 metric 10
sudo route add default gw 192.168.1.2 metric 20

Enterprise Network Examples

VLAN Routing Configuration

Large organizations often segment networks using VLANs. Configure routing between VLAN subnets:

sudo route add -net 192.168.10.0 netmask 255.255.255.0 gw 192.168.1.1
sudo route add -net 192.168.20.0 netmask 255.255.255.0 gw 192.168.1.1
sudo route add -net 192.168.30.0 netmask 255.255.255.0 gw 192.168.1.1

DMZ Access Control

Route DMZ traffic through security appliances for monitoring and filtering:

sudo route add -net 172.16.100.0 netmask 255.255.255.0 gw 172.16.1.1

Troubleshooting Route Command Issues

Common Error Messages

Network is Unreachable Error

This error occurs when attempting to add routes to networks not accessible from the current system. The gateway must be reachable through existing routes:

$ sudo route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.1.1
SIOCADDRT: Network is unreachable

Solution: Verify gateway connectivity and ensure the gateway IP address exists on a directly connected network.

No Such Process Error

This error appears when attempting to delete non-existent routes:

$ sudo route del -net 192.168.3.0
SIOCDELRT: No such process

Solution: Verify the route exists using route -n before attempting deletion.

Debugging Techniques

Use ping to test connectivity after adding routes:

ping -c 3 192.168.100.1

Employ traceroute to verify packet paths:

traceroute 192.168.100.1

Monitor system logs for routing-related messages:

sudo tail -f /var/log/messages | grep route

Modern Alternatives: Route vs ip route

Introduction to iproute2

The iproute2 package provides modern networking utilities including the ip route command. This tool offers enhanced functionality and improved performance compared to traditional net-tools utilities.

Key advantages of ip route include:

  • Faster execution and lower resource usage
  • Support for advanced routing features
  • Better integration with modern kernel networking
  • More intuitive command syntax

Command Equivalents

Viewing Routes:

# Traditional route command
route -n

# Modern ip route equivalent
ip route show

Adding Routes:

# Traditional route command
route add -net 10.0.2.0/24 gw 192.168.0.1 eth0

# Modern ip route equivalent
ip route add 10.0.2.0/24 via 192.168.0.1 dev eth0

Deleting Routes:

# Traditional route command
route del -net 10.0.2.0/24 gw 192.168.0.1

# Modern ip route equivalent
ip route del 10.0.2.0/24 via 192.168.0.1

The ip route command provides clearer syntax with the via keyword explicitly indicating gateways. This reduces syntax errors and improves command readability.

Best Practices and Security Considerations

Route Management Best Practices

Document all routing changes in network configuration management systems. Test route modifications in development environments before implementing in production. Maintain backup configurations enabling quick recovery from routing mistakes.

Monitor routing table changes through logging and alerting systems. Regular routing table audits help identify unauthorized modifications or configuration drift. Use version control for routing configuration files to track changes over time.

Security Implications

Unauthorized routing changes can redirect traffic through malicious systems or create denial-of-service conditions. Implement proper access controls limiting route modification privileges to authorized administrators. Monitor routing table modifications through system auditing tools.

Consider the security implications of routing traffic through third-party networks. Evaluate encryption requirements for sensitive data traversing untrusted network segments. Implement network segmentation to limit the impact of routing compromises.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button