MC, 2025
Ilustracja do artykułu: Command linux route: A Guide to Managing Routing Tables

Command linux route: A Guide to Managing Routing Tables

When working with Linux, networking is one of the most critical aspects to master. Whether you're managing servers, configuring networks, or troubleshooting issues, knowing how to manage network routes can make a significant difference. One of the key tools available for this task is the **Linux route command**. In this article, we will explore what the **route command** is, how it works, and provide practical examples to help you understand its usage better. Let's dive in!

What is the Linux Route Command?

The **route** command in Linux is used to display and modify the network routing table. The routing table is essentially a map that tells the operating system how to forward network packets to their destination. It specifies the available routes, their network addresses, and which gateway to use for packet forwarding.

The **route command** allows you to view, add, delete, or modify these routes. It’s particularly useful when you need to manually manage routing, troubleshoot networking issues, or configure advanced network setups. In fact, even though **ip route** has become the preferred tool for handling routing in modern Linux systems, the **route command** is still widely used and supported, especially on older systems.

Understanding Routing Tables

Before jumping into examples, it’s essential to understand what a routing table is and how it works. A routing table is a data structure that stores the routes for network communication. Each entry in this table defines a destination network, the network mask (or prefix), the gateway (if any), and the interface to use for the communication.

In a typical routing table, you’ll find the following information:

  • **Destination**: The network or IP address to which packets are being sent.
  • **Gateway**: The gateway IP address used to reach the destination network. If there's no gateway, the system communicates directly.
  • **Genmask**: The network mask that defines the range of IP addresses for the destination network.
  • **Interface**: The network interface used to reach the destination, such as eth0, wlan0, etc.

With this basic understanding, let’s look at how the **route command** interacts with the routing table.

Using the Route Command

The basic syntax for the **route** command is:

route [options]

When executed without any options, **route** will display the current routing table:

route

This command will return a list of routes, including the destination, gateway, genmask, and the interface. The table will look something like this:

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

Route Command Options

There are several options you can use with the **route** command to manage routing tables more effectively. Here are some of the most common ones:

  • route add: Add a new route to the routing table.
  • route del: Delete an existing route from the routing table.
  • route -n: Show the routing table with numerical addresses (this skips DNS resolution for faster output).
  • route -C: Show the routing table for a specific network namespace (useful for containerized environments).
  • route -v: Show verbose output, including additional information about each route.

Adding Routes with the Route Command

Adding routes is one of the most common tasks performed with the **route** command. To add a new route, use the following syntax:

sudo route add -net  netmask  gw  

For example, if you want to add a route to the 10.0.0.0 network with a subnet mask of 255.255.255.0 and use the gateway 192.168.1.1, you would run:

sudo route add -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.1.1 eth0

This command will add a route to the **10.0.0.0/24** network using the gateway **192.168.1.1** and interface **eth0**.

Deleting Routes

If you need to remove a route, use the route del command. The syntax for deleting a route is:

sudo route del -net  netmask  gw 

For instance, to delete the route we added earlier to the 10.0.0.0 network, you would execute:

sudo route del -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.1.1

This removes the specified route from the routing table.

Viewing the Routing Table

To view the current routing table, simply run:

route

If you prefer to view the table with numerical IP addresses instead of hostnames (which can be faster and more precise), use the -n option:

route -n

This will display the routing table in numerical form, which is especially useful when troubleshooting network issues.

Advanced Route Command Examples

Let’s explore some more advanced usage of the **route command** with a few practical examples:

Example 1: Adding a default gateway

If your network setup changes and you need to set a new default gateway, you can do so with the following command:

sudo route add default gw 192.168.1.1

This sets the default gateway to **192.168.1.1** for all outgoing traffic.

Example 2: Adding a route to a specific subnet

Suppose you want to route traffic destined for the **192.168.2.0/24** network through a different gateway. You can add this route with:

sudo route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.254

This command will send all traffic destined for the **192.168.2.0/24** network through the gateway **192.168.1.254**.

Example 3: Changing the route metric

Sometimes you may want to modify the priority of a specific route. The metric value is used to determine the priority of the route. Lower metric values have higher priority. You can set a custom metric with:

sudo route add -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.1.1 metric 10

This command adds a route with a custom metric of **10** for the **10.0.0.0/24** network.

Conclusion

The **Linux route command** is a powerful tool for managing your system’s network routing. It allows you to view, add, and remove routes from the routing table, helping you optimize and troubleshoot your network configuration. While newer tools like **ip route** may eventually replace it, the **route command** remains an essential tool for network administrators and Linux enthusiasts alike.

We’ve covered the basics of the **route command**, along with practical examples of how to add and delete routes, view the routing table, and work with advanced options. Now it’s your turn to experiment with the **route command** and see how it can help you manage your network routes more effectively!

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!

Imię:
Treść: