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.

$ 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:

$ 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

Advantech AOM-2721 is now Ubuntu Certified

Canonical announces that the Advantech AOM-2721 is officially joining the list of Ubuntu Certified Hardware.…

5 hours ago

Ruby and Rails Performance Roundup: The Backlog Edition

Yet again instead of tweets, a blog post. The backlog got out of hand -…

6 hours ago

How to Deploy OpenProject on Ubuntu VPS

This article provides a guide demonstrating how to deploy OpenProject on Ubuntu VPS. What is…

6 hours ago

Canonical integrates NVIDIA Nemotron 3.5 Lightning with Ubuntu for always-on AI agents

Canonical is pleased to announce that  NVIDIA’s newly introduced NVIDIA Nemotron 3.5 Lightning, an open,…

2 days ago

Xfwl4’s Second Preview Release

I’m pleased to announce the second preview release of xfwl4, Xfce’s Wayland compositor, version 4.21.1.…

2 days ago

How to Install, Secure, and Set Up MySQL on Ubuntu 26.04 LTS

If you have been using an ecommerce store to place online orders or have been…

3 days ago