Categories: TutorialsUbuntu

adding persistent static routes on ubuntu

When you need to access networks located on a different network segment, you need to have a route set up so the PC knows how to get to the other network segment. This generally just points to your main gateway, but you may want to set up additional static routes, where you don’t want the traffic going through your main default gateway.
(Note: For Ubuntu 18.04 and higher, this is now done using netplan configuration. Please see “Adding persistent static routes on Ubuntu 18.04 and higher using netplan for more info)
To set up a static route on Ubuntu so that it automatically gets created every time the PC starts, you need to modify the /etc/network/interfaces file.

$

Sponsored
sudo nano /etc/network/interfaces

The route will get set up when one of the interfaces comes up. Find the section that corresponds to the interface that this route will be set up on. Eg. eth0 or em1.
At the bottom of this section, add the following line:

up route add -net 192.168.40.0/24 gw 192.168.30.1 dev em1

Modify the above to suit the route you want to add.
Your file should now look something like this:

# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto em1
iface em1 inet static
address 192.168.30.2
netmask 255.255.255.0
broadcast 192.168.30.255
gateway 192.168.30.254
#persistent static routes
up route add -net 192.168.40.0/24 gw 192.168.30.1 dev em1

To make these changes take effect, you can run the following command to bring down the interface, and bring it back up again:

Sponsored
$ sudo ifdown em1 && sudo ifup em1

or simply try a reboot.
Run the following command to ensure the route has been set up correctly:

$ route -n

The post adding persistent static routes on ubuntu appeared first on Networking HowTos.

Ubuntu Server Admin

Recent Posts

Install FreePBX and Asterisk on Ubuntu 24.04 LTS for security patches until 2036

Deploying FreePBX and Asterisk on a single Ubuntu virtual machine in a public cloud is…

2 hours ago

Certification as a strategy: How Ubuntu & SystemReady boost hardware competitiveness

Canonical and MediaTek enhance reliability, accelerate market entry and reduce Total Cost of Ownership (TCO)…

2 hours ago

Ubuntu 20.04 LTS on Azure: how to stay secure after standard support ends

As Ubuntu 20.04 LTS (Focal Fossa) standard support ends on May 31, 2025, Azure users…

2 hours ago

Ubuntu Weekly Newsletter Issue 881

Welcome to the Ubuntu Weekly Newsletter, Issue 881 for the week of February 23 –…

18 hours ago

Experiment Tracking with MLFlow in Canonical’s Data Science Stack

Welcome back, data scientists! In my previous post, we explored how easy it is to…

1 day ago

How to Install vLLM on Linux Using 4 Easy Steps

In this article, we will see how to install vLLM on Linux using 4 easy…

1 day ago