Linux route Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux route command to manage static and dynamic routing in your network. The lab covers the purpose and syntax of the route command, how to configure static routes, and how to manage dynamic routing. You will explore examples of viewing the routing table, adding new static routes, and removing existing routes. This lab provides practical knowledge for network administrators and developers working with Linux networking.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux/RemoteAccessandNetworkingGroup -.-> linux/ifconfig("`Network Configuring`") linux/RemoteAccessandNetworkingGroup -.-> linux/ip("`IP Managing`") subgraph Lab Skills linux/ifconfig -.-> lab-422896{{"`Linux route Command with Practical Examples`"}} linux/ip -.-> lab-422896{{"`Linux route Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the route Command

In this step, you will learn about the purpose and syntax of the route command in Linux. The route command is used to view and manipulate the IP routing table, which is responsible for determining the path that network traffic will take to reach its destination.

To begin, let's explore the basic syntax of the route command:

sudo route [command] [destination] [gateway] [metric]

Here's what each parameter means:

  • command: The action to perform, such as add, del, or show.
  • destination: The network or host to which the route applies.
  • gateway: The gateway or next-hop router to which packets should be forwarded.
  • metric: The cost or priority of the route, used for routing decisions.

Now, let's try some examples to see the route command in action.

To view the current routing table, use the show command:

sudo route -n show

Example output:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.17.0.1      0.0.0.0         UG    100    0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0

This shows the default gateway (0.0.0.0) and the local network route (172.17.0.0).

To add a new static route, use the add command:

sudo route add -net 192.168.1.0 netmask 255.255.255.0 gw 10.0.0.1

This adds a route to the 192.168.1.0/24 network, with a gateway of 10.0.0.1.

To remove a route, use the del command:

sudo route del -net 192.168.1.0 netmask 255.255.255.0

This removes the route to the 192.168.1.0/24 network.

Configure Static Routes Using the route Command

In this step, you will learn how to configure static routes using the route command. Static routes are manually defined routes that are added to the routing table and take precedence over dynamic routes.

Let's start by adding a static route to the routing table:

sudo route add -net 192.168.2.0 netmask 255.255.255.0 gw 10.0.0.2

This command adds a static route to the 192.168.2.0/24 network, with a gateway of 10.0.0.2.

You can verify the new route by running the route -n command:

sudo route -n

Example output:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.17.0.1      0.0.0.0         UG    100    0        0 eth0
10.0.0.2        0.0.0.0         255.255.255.255 UH    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     10.0.0.2        255.255.255.0   UG    0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0

You can see the new static route to the 192.168.2.0/24 network with the gateway 10.0.0.2.

To make the static route persistent across reboots, you can add it to the /etc/network/interfaces file:

sudo nano /etc/network/interfaces

Add the following lines to the file:

up route add -net 192.168.2.0 netmask 255.255.255.0 gw 10.0.0.2

This will ensure that the static route is added automatically when the network interface is brought up.

Manage Dynamic Routing with the route Command

In this step, you will learn how to manage dynamic routing using the route command. Dynamic routing protocols, such as OSPF or BGP, automatically update the routing table based on network changes, making it easier to maintain complex network topologies.

Let's start by adding a default route using the add default command:

sudo route add default gw 10.0.0.1

This command adds a default route, which will be used for any traffic that doesn't match a more specific route in the routing table.

You can verify the new default route by running the route -n command:

sudo route -n

Example output:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.1        0.0.0.0         UG    0      0        0 eth0
10.0.0.2        0.0.0.0         255.255.255.255 UH    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     10.0.0.2        255.255.255.0   UG    0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0

You can see the new default route with the gateway 10.0.0.1.

To remove a dynamic route, you can use the del command:

sudo route del default

This removes the default route from the routing table.

Summary

In this lab, you first learned about the purpose and syntax of the route command in Linux, which is used to view and manipulate the IP routing table. You explored the basic command structure and learned how to view the current routing table using the show command. You also learned how to add and remove static routes using the add and del commands, respectively. Next, you focused on configuring static routes, which are manually defined routes that take precedence over dynamic routes. You practiced adding, modifying, and removing static routes using the route command. Finally, you explored managing dynamic routing, where you learned how to use the route command to interact with dynamic routing protocols and update the routing table accordingly.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like