How to check if a network route is configured in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a network route is configured in Linux. Understanding network routing is fundamental for diagnosing connectivity issues. You will explore different methods to display and verify the routing table, including using the modern ip route show command and the older route -n utility. Finally, you will learn how to inspect network configuration files in /etc/network to understand how routes are persistently configured on your system. By the end of this lab, you will be equipped with the essential skills to examine and understand network routing on a Linux machine.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/RemoteAccessandNetworkingGroup(["Remote Access and Networking"]) linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/RemoteAccessandNetworkingGroup -.-> linux/ip("IP Managing") subgraph Lab Skills linux/cat -.-> lab-558743{{"How to check if a network route is configured in Linux"}} linux/ip -.-> lab-558743{{"How to check if a network route is configured in Linux"}} end

Display routes with ip route show

In this step, you will learn how to display the routing table on your Linux system using the ip route show command. The routing table is a set of rules that determines where network traffic is directed. Understanding the routing table is crucial for troubleshooting network connectivity issues.

The ip command is a powerful utility for network configuration in Linux. It's a modern replacement for older tools like ifconfig and route.

To display the routing table, open your terminal and type the following command:

ip route show

Press Enter.

You will see output similar to this:

default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2

Let's break down this output:

  • default: This is the default route. If the system doesn't know how to reach a specific destination network, it sends the traffic to the gateway specified by the default route.
  • via 172.17.0.1: This indicates the gateway IP address for the default route.
  • dev eth0: This specifies the network interface (eth0) that the traffic will be sent through.
  • 172.17.0.0/16: This represents a network range (a subnet). The /16 is CIDR notation, indicating the subnet mask.
  • proto kernel: This means the route was added by the kernel.
  • scope link: This indicates that the destination is directly connected on the link.
  • src 172.17.0.2: This shows the source IP address that will be used for traffic going through this route.

Your output might look slightly different depending on the network configuration of your LabEx environment, but the general structure will be similar.

The ip route show command is essential for understanding how your system routes network traffic. It helps you see which interface is used for different destinations and which gateway is used for the default route.

Practice running this command a few times to get comfortable with it.

Click Continue to proceed to the next step.

Verify routes with route -n

In the previous step, you used the ip route show command to display the routing table. Another common command for viewing the routing table is route. While ip is the more modern tool, route is still widely used and useful to know.

The route command, when used with the -n option, displays the routing table using numerical addresses instead of trying to resolve hostnames. This is often faster and avoids potential DNS issues.

Let's use the route -n command to view the routing table. Open your terminal and type:

route -n

Press Enter.

You will see output similar to this:

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    0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0

Let's compare this output to the ip route show output:

  • Destination: This column shows the destination network or host. 0.0.0.0 represents the default route.
  • Gateway: This is the gateway IP address for the route. 0.0.0.0 in this column usually means the destination is directly connected.
  • Genmask: This is the network mask (subnet mask) for the destination network. 0.0.0.0 for the default route means it matches all destinations.
  • Flags: These indicate the characteristics of the route.
    • U: The route is Up.
    • G: The route uses a Gateway.
  • Iface: This is the network interface used for the route.

Notice how route -n presents the information in a table format, which some users find easier to read at a glance. Both ip route show and route -n provide similar information about the routing table, but they display it differently.

Understanding both commands will give you more flexibility when working with network configurations in Linux.

Click Continue to move on.

Inspect network config in /etc/network

In the previous steps, you learned how to view the active routing table using ip route show and route -n. Now, let's look at where some of the network configuration might be stored on a Debian-based system like Ubuntu.

Network configuration files are often located in the /etc directory, which is where system configuration files are typically stored. A common location for network interface configuration is the /etc/network/interfaces file.

We will use the cat command to display the contents of this file. cat is a simple command that reads files sequentially and prints them to the standard output.

Open your terminal and type the following command:

cat /etc/network/interfaces

Press Enter.

You will see output similar to this:

## interfaces(5) file used by ifup(8) and ifdown(8)
## Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

Let's examine the contents:

  • Lines starting with # are comments and are ignored.
  • source-directory /etc/network/interfaces.d: This line indicates that configuration files in the /etc/network/interfaces.d directory should also be included. This allows for modular network configuration.
  • auto lo: This line tells the system to automatically bring up the lo interface (the loopback interface) during boot.
  • iface lo inet loopback: This configures the lo interface as a loopback interface using the inet (IPv4) address family.
  • auto eth0: This tells the system to automatically bring up the eth0 interface during boot. eth0 is a common name for the first Ethernet network interface.
  • iface eth0 inet dhcp: This configures the eth0 interface to obtain its IP address and other network settings automatically using DHCP (Dynamic Host Configuration Protocol).

This file provides a static configuration for network interfaces. While modern systems often use tools like Netplan or NetworkManager for more dynamic configuration, understanding the /etc/network/interfaces file is still valuable as it's present on many systems and provides a clear view of basic interface setup.

You have now seen how to view the active routing table and inspect a common network configuration file. This gives you a basic understanding of how network settings are managed in Linux.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check if a network route is configured in Linux. You started by using the ip route show command to display the system's routing table, understanding the different components of the output such as default routes, gateways, network interfaces, and subnets. This command is a modern and powerful tool for network configuration and troubleshooting.

You also learned how to verify routes using the route -n command, which provides a more traditional view of the routing table in a numerical format. Finally, you explored how to inspect the network configuration files located in /etc/network to understand how network interfaces and routes are persistently configured on the system. These steps provide essential skills for understanding and troubleshooting network connectivity in Linux environments.