Skip to content
Bots!
Bots!
  • About
    • Myself
    • আমার দোয়া
  • Bookmarks
    • Bookmarks
    • My OCI Bookmarks
    • Useful Proxmox Commands & Links
    • Learning Nano
    • Useful Sites
    • Useful Virtualbox Command
    • Useful MySQL Command
    • Useful Linux Command
    • BTT-CAS
  • Resources
    • Webinar on Cloud Adoption for Project Managers
  • Photos
  • Videos
  • Downloads
Bots!

Permanently add Static Route in Linux

Rumi, September 4, 2020September 4, 2020

Static routing is the process of manually entering the routes to the routing table to reach a particular destination. Basically two commands are used in Linux to add routes. The first command is the old traditional route add and second is the IP route command.

Earlier we learned how to route add in Linux, in this tutorial, here I will show you how to add permanent static routes in Linux distributions such as Centos and Ubuntu.

Adding temporary static routes

In order to add a static route temporarily on your Linux machine, you can use either route or ip command.

You can list the current routing table as follows.

$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.206.1 0.0.0.0 UG 100 0 0 eno1
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eno1
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
192.168.206.0 0.0.0.0 255.255.255.0 U 100 0 0 eno1

route -n command

If you want to add a route to the network 198.161.1.0 through gateway 192.168.206.1, you can execute the following command.

$ sudo route add -net 198.161.1.0 netmask 255.255.255.0 gw 192.168.206.1 eno1

Alternatively, you can use ip command as follows:

$ sudo ip route add 198.161.1.0/24 via 198.168.206.1 dev eno1

Adding permanent static routes

In the above section, we saw how to add routes in Linux. But the system will forget those routes on next reboot.

So we have to add routes to config file to make it persistent.

On RHEL or CentOS, you need to modify the interface file in ‘/etc/sysconfig/network-scripts’.

For example, here, we have to add routes on network interface ens192. Hence, the file we need to modify will be ‘/etc/sysconfig/network-scripts/route-ens192’.

The basic syntax of static route in this file is:

<target-address> via <gateway-address> dev <interface>

Where,

target-address – Destination network address.

gateway-address – Router address to reach the destination address.

dev – Indicates which interface to reach the destination, interface naming schemes are eno[1-N], ens[1-N], and or eth[0-N].

So, in order to add the above route in ‘route-ens192’ file, append the following line.

‘10.9.8.0/24 via 10.9.8.100 dev ens192’

Then, you need to restart the network service.

$ sudo service network restart

Finally, verify that the new routes are visible in the routing table using the following command.

$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.9.8.100 0.0.0.0 UG 100 0 0 ens192
10.0.0.0 0.0.0.0 255.0.0.0 U 100 0 0 ens192
10.9.8.0 10.9.8.100 255.255.255.0 UG 100 0 0 ens192

On Ubuntu Linux, if you want to add a permanent static route to your system, you have to add route entry to the ‘/etc/network/interfaces’. Using your favorite editor (nano, vim,…) to open the network interface file and insert the following lines to it.

‘up route add -net 10.9.7.0/24 gw 10.9.8.100 dev ens160’. For example:

source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto ens160
iface ens160 inet static
address 10.9.8.41
netmask 255.255.255.0
gateway 10.9.8.100
up route add -net 10.9.7.0/24 gw 10.9.8.100 dev ens160

In order to make the change take effect, you need to restart the networking service by running:

$ sudo /etc/init.d/networking restart

Verify that the route has been configured correctly:

$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.9.8.100 0.0.0.0 UG 0 0 0 ens160
10.9.7.0 10.9.8.100 255.255.255.0 UG 0 0 0 ens160
10.9.8.0 0.0.0.0 255.255.255.0 U 0 0 0 ens160
10.233.70.0 10.9.8.45 255.255.255.0 UG 0 0 0 tunl0
10.233.90.0 0.0.0.0 255.255.255.0 U 0 0 0 *

From Ubuntu 18.04 LTS and higher version, the operating system uses the netplan YAML files for network configuration – it is located in the ‘/etc/netplan’ directory. For example:

$ ls /etc/netplan
01-network-manager-all.yaml

In order to add persistent static route, you have to add the following lines to the netplan configuration file ‘/etc/netplan/01-network-manager-all.yaml’

routes:
- to: 192.168.205.0/24
via: 192.168.206.1

For example:

# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
ethernets:
eno1: 
dhcp4: true
routes:
- to: 192.168.205.0/24
via: 192.168.206.1

Apply the change by running:

$ sudo netplan apply

You can verify the route configuration by running command route -n or ip route. Here is the result:

The result after apply a new static route

Conclusion
In this tutorial, we learned how to add a permanent route by adding to respective config files in Linux. Thanks for reading and please leave your suggestion in the below comment section.

Administrations Configurations (Linux) Route

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Myself…

Hi, I am Hasan T. Emdad Rumi, an IT Project Manager & Consultant, Virtualization & Cloud Savvyfrom Dhaka, Bangladesh. I have prior experience in managing numerous local and international projects in the area of Telco VAS & NMC, National Data Center & PKI Naitonal Root and CA Infrastructure. Also engaged with several Offshore Software Development Team.

Worked with Orascom Telecom-Banglalink, Network Elites as VAS partner, BTRC, BTT (Turkey) , Mango Teleservices Limited and Access to Informaiton (A2I-UNDP)

Currently working at Oracle Corporation as Principal Technology Solution and Cloud Architect.

You can reach me [h.t.emdad at gmail.com] and I will be delighted to exchange my views.

Tags

Apache Bind Cacti CentOS CentOS 6 CentOS 7 Debain Debian Debian 10 Debian 11 Debian 12 DKIM Docker endian icinga iptables Jitsi LAMP Letsencrypt Linux Munin MySQL Nagios Nextcloud NFS nginx pfsense php Postfix powerdns Proxmox RDP squid SSH SSL Ubuntu Ubuntu 16 Ubuntu 18 Ubuntu 20 Varnish virtualbox vpn Webmin XCP-NG zimbra

Topics

Recent Posts

  • Install Jitsi on Ubuntu 22.04 / 22.10 April 30, 2025
  • Key Lessons in life April 26, 2025
  • Create Proxmox Backup Server (PBS) on Debian 12 April 19, 2025
  • Add Physical Drive in Proxmox VM Guest April 19, 2025
  • Mount a drive permanently with fstab in Linux April 16, 2025
  • Proxmox 1:1 NAT routing March 30, 2025
  • Installation steps of WSL – Windows Subsystem for Linux March 8, 2025
  • Enabling Nested Virtualization In Proxmox March 8, 2025
  • How to Modify/Change console/SSH login banner for Proxmox Virtual Environment (Proxmox VE / PVE) March 3, 2025
  • Install Proxmox Backup Server on Debian 12 February 12, 2025

Archives

Top Posts & Pages

  • Install Jitsi on Ubuntu 22.04 / 22.10
©2025 Bots! | WordPress Theme by SuperbThemes