How to Add Route on Linux?

Learn via video courses
Topics Covered

What is Route?

A route is a key element of networking in Linux that governs the way network packets are sent between various networks or subnets. It is a crucial component of the networking subsystem of the kernel and aids in the correct transmission of data among networked systems.

A route is fundamentally the path or series of routers that network packets use to go from one place to another. Each router serves as an intermediate device that decides how to forward packets by looking at their destination IP address. Routes and their related properties are listed in routing tables, which are maintained by routers.

Proper management of routes in Linux is essential for efficient network communication. By configuring routes correctly, packets can be directed along the optimal path, ensuring timely and accurate delivery between networks. Linux provides a flexible and powerful routing infrastructure, enabling administrators to tailor network connectivity to meet specific requirements and optimize performance.

How to Add Route on Linux?

Adding a route in linux entails setting up the routing table in the kernel to specify how network packets should be sent between various networks or subnets. The more contemporary "ip" command from the iproute2 package is another option for adding a route, in addition to the more conventional "route" command.

Here is a detailed tutorial on adding a route on Linux using both approaches:

Using the "route" Command

  • Open a terminal or shell session with administrative privileges (root or sudo access).
  • Determine the destination network or host IP address that you want to add a route for.
  • Identify the next hop or gateway IP address, which is the router's IP address responsible for forwarding packets toward the destination network.
  • Determine the network interface that the route should use. You can list the available interfaces using the "ifconfig" or "ip link show" command.
  • Execute the following command to add the route, replacing the placeholders with the appropriate values:
  1. If the route is for a network, use the "-net" option. If it is for a specific host, use the "-host" option.
  2. Replace <destination> with the destination network or host IP address.
  3. Specify the <netmask> to match the network's subnet mask.
  4. Provide <gateway> with the IP address of the next hop or gateway.
  5. Set <interface> to the name of the network interface.
  • Verify that the route has been added successfully by executing the "route" command with the "-n" or "--numeric" option. It will display the routing table, and you should see the newly added route.

Using the "ip" Command

  • Open a terminal or shell session with administrative privileges (root or sudo access).
  • Determine the destination network or host IP address that you want to add a route for.
  • Identify the next hop or gateway IP address.
  • Determine the network interface to be used. You can list the available interfaces using the "ifconfig" or "ip link show" command.
  • Execute the following command to add the route, replacing the placeholders with the appropriate values:
  1. Replace <destination> with the destination network or host IP address.
  2. Specify <gateway> as the IP address of the next hop or gateway.
  3. Set <interface> to the name of the network interface.
  • Confirm the successful addition of the route by executing the "ip route show" command. It will display the routing table, and you should see the newly added route.

Add Route on Linux Using ip

Adding a permanent route configuration on Ubuntu can be done using different methods, depending on the network configuration tool employed. In this guide, we will cover three common methods: using Network Manager, Netplan, and the /etc/network/interfaces file.

Adding Permanent Route Configuration on Ubuntu

Using Network Manager

  • Network Manager is a popular network configuration tool used in Ubuntu and other Linux distributions with graphical interfaces.

  • Open a terminal or shell session with administrative privileges (root or sudo access).

  • Identify the network interface for which you want to add the route using the "ip link show" or "ifconfig" command.

  • Execute the following command to add a permanent route using Network Manager:

  1. Replace <connection_name> with the name of the connection managed by the Network Manager (e.g., "eth0" or "wlan0").
  2. Specify <destination_network> as the IP address of the destination network or host.
  3. Set <subnet_mask> to match the network's subnet mask.
  4. Provide <gateway_ip> as the IP address of the next hop or gateway.
  • Restart the Network Manager service for the changes to take effect:

Using Netplan

  • Netplan is the default network configuration tool starting from Ubuntu 18.04 and newer versions.

  • Open a terminal or shell session with administrative privileges (root or sudo access).

  • Locate the Netplan configuration file for your network interface under the "/etc/netplan/" directory. It typically has a ".yaml" extension.

  • Edit the configuration file using a text editor, such as "nano" or "vi".

  • Add the following YAML code block under the network interface configuration section:

  1. Replace <destination_network> with the destination network or host IP address.
  2. Specify <subnet_mask> to match the network's subnet mask.
  3. Provide <gateway_ip> as the IP address of the next hop or gateway.
  • Save the configuration file and exit the text editor.

  • Apply the changes by executing the following command:

Using /etc/network/interfaces

  • The /etc/network/interfaces file is the traditional method for configuring network interfaces in Ubuntu.
  • Open a terminal or shell session with administrative privileges (root or sudo access).
  • Locate the "/etc/network/interfaces" file and open it using a text editor.
  • Add the following line under the network interface configuration section:
  1. Replace <destination_network> with the destination network or host IP address.
  2. Specify <subnet_mask> to match the network's subnet mask.
  3. Provide <gateway_ip> as the IP address of the next hop or gateway.
  • Save the file and exit the text editor.
  • Restart the networking service for the changes to take effect:

Adding Permanent Route Configuration on RHEL

Adding a permanent route configuration on RHEL (Red Hat Enterprise Linux) can be accomplished using the "ip" command, which offers more advanced configuration options. Here's a step-by-step guide on how to add a permanent route on RHEL:

  • Open a terminal or shell session with administrative privileges (root or sudo access).
  • Identify the network interface for which you want to add the route using the "ip link show" or "ifconfig" command.
  • Determine the destination network or host IP address for which you want to add the route.
  • Identify the next hop or gateway IP address, which is the router responsible for forwarding packets toward the destination network.
  • Execute the following command to add a permanent route:
  1. Replace <destination_network> with the destination network or host IP address.
  2. Specify <subnet_mask> to match the network's subnet mask.
  3. Provide <gateway_ip> as the IP address of the next hop or gateway.
  4. Set <interface> to the name of the network interface.
  5. Set <metric> to a numerical value representing the route's metric or cost. The lower the value, the higher the priority.
  • To make the route persistent across system reboots, you need to add the route configuration to the appropriate configuration file.
  1. For RHEL 7 and earlier versions, modify the "/etc/sysconfig/network-scripts/route-<interface>" file. Replace "<interface>" with the name of the network interface. Add the following line to the file:
  2. For RHEL 8 and newer versions, modify the "/etc/sysconfig/network-scripts/route-<interface>.route" file. Again, replace "<interface>" with the name of the network interface. Add the following line to the file:
    • Save the file and exit the text editor.
    • Restart the networking service for the changes to take effect:

Add Route on Linux Using nmcli

Adding a route on Linux using nmcli (Network Manager Command-Line Interface) is a convenient way to configure network routes when Network Manager is used as the network management tool. nmcli provides a command-line interface for managing Network Manager connections, including adding and modifying routes. Here's a step-by-step guide on how to add a route using nmcli:

  • Open a terminal or shell session with administrative privileges (root or sudo access).
  • Identify the name of the Network Manager connection to which you want to add the route. You can list the available connections using the following command:
  • Determine the destination network or host IP address for which you want to add the route.
  • Identify the next hop or gateway IP address, which is the router responsible for forwarding packets toward the destination network.
  • Execute the following command to add a route using nmcli:
  1. Replace <connection_name> with the name of the Network Manager connection to which you want to add the route.
  2. Specify <destination_network> as the IP address of the destination network or host.
  3. Set <subnet_mask> to match the network's subnet mask.
  4. Provide <gateway_ip> as the IP address of the next hop or gateway.
  • If the connection you modified is currently active, restart the Network Manager service for the changes to take effect:

Now, the route will be added to the specified Network Manager connection. The route will persist across system reboots, ensuring that packets destined for the specified network or host are correctly forwarded.

To verify that the route has been added successfully, you can use the following command to display the details of the modified connection:

Look for the "IP4.ROUTE[1]" entry in the output, which should contain the added route information.

Adding a Route in Linux Using the Network Graphical Interface

Adding a route using the graphical interface in Linux provides a user-friendly way to configure network routes without relying on command-line tools. Here's a step-by-step guide on how to add a route using the network graphical interface on Linux:

  • Open the network settings or network manager graphical interface on your Linux distribution. The exact method for accessing this interface may vary depending on your Linux distribution and desktop environment. In popular desktop environments like GNOME, you can usually find it in the system settings menu or the system tray.

  • Navigate to the network settings section that allows you to configure network connections. This section might be labeled as "Network," "Connections," or "Network Settings," depending on the Linux distribution and desktop environment.

  • Identify the network connection for which you want to add the route. This could be a wired connection (Ethernet) or a wireless connection (Wi-Fi). Click on the appropriate connection to access its settings.

  • Look for an option related to routing or routes in the connection settings. The exact labeling and placement of this option may vary depending on the network manager interface used by your Linux distribution. It could be labeled as "Routes," "Routing," "Advanced," or something similar.

  • Click on the "Add" or "+" button to add a new route.

  • In the route configuration dialog, you will need to provide the necessary details for the route:

    1. Destination: Enter the destination network or host IP address for which you want to add the route
    2. Subnet Mask: Specify the subnet mask for the destination network. This determines the network range covered by the route.
    3. Gateway: Enter the IP address of the next hop or gateway that should be used for forwarding packets to the destination network.
    4. Metric: Optionally, you may have the option to set the metric or cost for the route. The metric determines the priority of the route when multiple routes are available.
  • Once you have entered the required information, click "OK" or "Apply" to save the route configuration.

  • If the network connection is currently active, the changes may take effect immediately. Otherwise, you might need to restart the network service or reconnect to the network for the new route to be applied.

Troubleshooting Internet Issues on Linux

Troubleshooting internet issues on Linux can be a multi-step process that involves identifying the problem, isolating the cause, and implementing appropriate solutions. Here is a comprehensive guide to troubleshooting common internet issues on Linux:

Check Network Connectivity

  • Ensure that your network cable is properly connected (if using a wired connection) or that your Wi-Fi is enabled and connected to the correct network (if using wireless).

  • Test connectivity by pinging a known working IP address or domain name.

    For example:

    ping 8.8.8.8
    ping google.com
    
  • If the ping is successful, it indicates that your Linux system is connected to the network. If not, proceed to the next step.

Verify IP Configuration

  • Check if your Linux system has obtained an IP address using DHCP. Execute the following command:

  • Look for the network interface (e.g., eth0, wlan0) and verify that it has an assigned IP address. If not, try restarting the network manager or restarting the system.

Check DNS Resolution

  • DNS resolution is crucial for resolving domain names to IP addresses. Test DNS resolution using the "nslookup" command:
    If the command fails to resolve the domain name, there might be an issue with your DNS settings. Check your DNS configuration in the "/etc/resolv.conf" file, and consider using alternative DNS servers like Google DNS (8.8.8.8 and 8.8.4.4) or Cloudflare DNS (1.1.1.1 and 1.0.0.1).

Verify Firewall Settings

  • Temporarily disable the firewall to check if it is blocking network traffic. Use the following command to disable the firewall temporarily:
  • If the internet connection starts working after disabling the firewall, review your firewall rules to allow necessary inbound and outbound traffic.

Reset Network Manager

  • Restart the Network Manager service to reset the network configuration. Use the following command:

Update Network Drivers

  • Outdated or incompatible network drivers can cause connectivity issues. Ensure that your Linux system has the latest network drivers installed. Check the manufacturer's website or your Linux distribution's documentation for instructions on updating drivers.

Check Network Hardware

  • Test the network connection using another device (e.g., a different computer or smartphone) to determine if the issue is specific to your Linux system or the entire network.
  • Restart your modem/router to rule out any temporary glitches.
  • If using a wired connection, try a different Ethernet cable and port on the router.

Inspect Network Configuration Files

  • Review network configuration files to ensure they are correctly set up. Common configuration files include "/etc/network/interfaces" (for older Ubuntu versions) and "/etc/netplan/*.yaml" (for newer Ubuntu versions). Validate the configuration syntax and settings.

Restart Network Services

  • Restart various network services to ensure they are functioning properly. Execute the following commands:

Check DHCP Server

  • If you are using DHCP for IP address assignment, verify that the DHCP server is running and has available IP addresses. Contact your network administrator or service provider for assistance if needed.

Update the Linux Kernel

  • If you are experiencing persistent issues, consider updating your Linux kernel to the latest stable version. Newer kernel versions often include bug fixes and improved hardware compatibility.

Seek Community Support

  • Consult online forums, Linux community websites, or the official support channels of your Linux distribution for additional assistance. Provide detailed information about your issue, including any error messages or log files, to receive targeted help.

Conclusion

  • Adding a route on Linux involves configuring network settings to enable proper packet forwarding.
  • Various methods can be used, including the "ip" command, Network Manager, Netplan, or editing configuration files like "/etc/network/interfaces" (for older Ubuntu versions).
  • Network Manager offers a user-friendly graphical interface for managing network connections and adding routes.
  • Netplan is a YAML-based configuration tool used in newer Ubuntu versions for network configuration, including adding routes.
  • Editing the "/etc/network/interfaces" file is an option for older Ubuntu versions or systems not using Network Manager.
  • When using the "ip" command, identify the network interface, destination network or host IP address, and the next hop or gateway IP address.
  • Ensure that the network interface is properly connected and configured with an IP address.
  • Verify network connectivity by pinging a known working IP address or domain name.
  • Adapt the instructions based on your Linux distribution, version, and network configuration.
  • For permanent routes, modify the appropriate configuration files and restart the networking service.