How to Add Route in Ubuntu: A Step-by-Step Guide

Add Route in Ubuntu

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

When managing multiple Linux servers or setting configurations for a custom network setup, controlling traffic flows is crucial. In Ubuntu, roles generally define the paths network packets take to reach their destination. Whether you are setting up a static route to a specific subnet or fine-tuning traffic to pass through a particular gateway, knowing how to add and manage routes is a key networking skill. 

This guide walks you through the importance of routes in Ubuntu; including how to add route in Ubuntu and how to use them to their maximum capacity.

Understanding Routing Basics in Linux

In Linux, routing usually refers to the process of determining where you need to send a network packet based on their destination IP address. The system uses a routing table, which is a set of rules that define how traffic should flow between two networks and interfaces. 

Each entry generally includes: 

  • Destination: The target network or IP address.
  • Gateway: The next-hop IP address where the traffic should be sent.
  • Netmask/Prefix: Specifies the range of IPs in the destination.
  • Interface: The network interface used to send traffic (e.g., eth0, ens160).
  • Metric: The priority of the route—lower values have higher priority.

Linux uses this routing table to decide whether to send traffic directly to a device on the same subnet or forward it through a gateway to reach an external network.

You can view the active routing table using the command:

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

ip route

Understanding this table is essential before making any changes to ensure you’re not disrupting existing traffic or creating conflicts.

Related Article: Enabling SSH on Ubuntu: A Comprehensive Guide for Secure Remote Access

Viewing Existing Routes

Before you add route in Ubuntu, it is important to understand the current routing configuration on your system. Ubuntu provides multiple commands to inspect the routing table. 

View with ip route (modern method):

ip route

This command displays all routing entries, including default gateways and custom routes. 

Example output:

default via 192.168.1.1 dev eth0

192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

View with route (older command):

route -n

The -n flag shows the IP addresses instead of resolving hostnames, which is much faster and readable in most of the cases. 

Use these tools to verify if a route already exists or to troubleshoot connectivity issues.

Temporarily Add Route In Ubuntu Using IP route

To add a route that only lasts until the next reboot, use the ip route add command. This is useful for testing or temporary network changes.

Syntax:

sudo ip route add <destination-network> via <gateway-ip> dev <interface>

Example:

sudo ip route add 10.10.20.0/24 via 192.168.1.1 dev eth0

This command sends a signal to Ubuntu to send traffic destined for 10.10.20.0/24 through the gateway 192.168.1.1 using the eth0 interface.

To remove the route:

sudo ip route del 10.10.20.0/24

Temporary routes are not persistent and will be removed after a reboot. For permanent routing, configuration files must be edited—which we’ll cover next.

How To Permanently Add Route in Ubuntu

Temporarily adding routes with an ip route are lost on reboot, whereas permanent routes make sure that your configuration persists across reboots. How you set a permanent route depends on your Ubuntu version. 

Using /etc/netplan/ (Ubuntu 18.04 and Later)

Ubuntu versions 18.04 and later typically use Netplan for network configuration. You can define static routes in the Netplan YAML files. Here is how you can do it:

  1. Open the Netplan configuration file (e.g., /etc/netplan/01-netcfg.yaml):

sudo nano /etc/netplan/01-netcfg.yaml

  1. Add the routes: section under the appropriate interface:

network:

  version: 2

  ethernets:

    eth0:

      addresses:

        – 192.168.1.100/24

      gateway4: 192.168.1.1

      routes:

        – to: 10.10.20.0/24

          via: 192.168.1.1

  1. Apply the configuration:

sudo netplan apply

Using /etc/network/interfaces (Older Ubuntu Versions)

Using older versions of Ubuntu releases (pre-18.04) use the /etc/network/interfaces file.

  1. Open the interfaces file:

sudo nano /etc/network/interfaces

  1. Add the static route under the desired interface:

auto eth0

iface eth0 inet static

  address 192.168.1.100

  netmask 255.255.255.0

  gateway 192.168.1.1

  up route add -net 10.10.20.0 netmask 255.255.255.0 gw 192.168.1.1

  1. Restart networking:

sudo systemctl restart networking

Deleting a Route

If you no longer need a route or was added in the incorrect manner, you can delete it using the ip route del command. 

Syntax:

sudo ip route del <destination-network> via <gateway-ip> dev <interface>

Example:

sudo ip route del 10.10.20.0/24 via 192.168.1.1 dev eth0

This command removes a specific route from your routing table. 

To confirm if the deletion was successful, use the following command: 

ip route

Troubleshooting Routing Issues

IssuePossible CauseSuggested Fix
No connectivity after adding a routeIncorrect gateway or interface specifiedDouble-check gateway IP and interface name
Traffic not using the new routeMore specific route existsUse ip route to inspect route priorities and conflicts
Route disappears after rebootRoute was added temporarilyMake it permanent using Netplan or /etc/network/interfaces
Unable to reach the destination networkFirewall rules or security groups blocking trafficCheck ufw, iptables, or cloud provider security settings
Route added but still no responseGateway unreachablePing the gateway to verify connectivity
Intermittent connectivityNetwork flapping or conflicting DHCP/static settingsEnsure no overlapping network settings or IP conflicts
Slow response or timeoutRouting loop or misconfigured DNSUse traceroute to trace the path and validate DNS configs

Best Practices for Managing Routes

To maintain a clean and efficient network configuration, follow these best practices:

  1. Use Temporary Routes for Testing

Before adding a new route permanently, test it temporarily using ip route add to test its behaviour. 

  1. Make Routes Persistent Only After Validation

Once a route has been tested vigorously, configure it permanently using Netplan or /etc/network/interfaces to avoid losing it on reboot.

  1. Keep Configurations Clean and Organized

Avoid clutter in configuration files by grouping related routes and use comments to explain why each route has been added to the system. 

  1. Document All Custom Routes

Main precise documentation in a README or internal wiki file for all non-default routes, including its main purpose, network ranges, and associated gateways. 

  1. Monitor Network Performance

Use tools like ping, traceroute, or mtr to continuously monitor the effectiveness of your routing setup. 

  1. Avoid Overlapping Routes

Conflicting or overlapping subnets can cause traffic misdirection, therefore, always validate subnet boundaries. 

  1. Backup Before Making Changes

Before you start applying or editing persistent route configurations, back up relevant files to prevent accidental misconfigurations. 

  1. Use Consistent Naming and Interfaces

Stick to predictable interface names and ensure they match your routing definitions, like eth0, ens33. 

  1. Reapply Network Configurations Gracefully

Use command lines like sudo netplan apply or restart networking services properly to avoid downtime. 

Wrapping Up – Add Route In Ubuntu

Adding and managing routes in Ubuntu does not have to be that complex. Whether you are configuring a temporary testing route or setting up a permanent one using Netplan or other methods. 

From basic route viewing to handling routing issues, this guide helps you navigate through without any issues. 

Why would I need to add a route in Ubuntu?

Adding a route lets you control network traffic paths manually, often used for custom networking setups, testing, or multi-network environments.

How do I make a route permanent in Ubuntu?

To add a permanent route, edit network configuration files like /etc/network/interfaces or create custom route files depending on your Ubuntu version.

What command checks existing routes in Ubuntu?

Use the ip route show or route -n command to view all current network routes.

Marium Fahim
Hi! I am Marium, and I am a full-time content marketer fueled by an iced coffee. I mainly write about tech, and I absolutely love doing opinion-based pieces. Hit me up at [email protected].
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!