Different ways to configure static routing on Linux

In “Different Methods of Configuring Static Routes in Linux” in this guide, we will learn different methods of configuring static routes on Linux systems, whether it be Ubuntu, Kali Linux, CentOS, Fedora, Linux Mint or any other Linux system . If you have a newly installed Linux system, it is recommended that you read any of the following articles based on the Linux distribution you are running:

Main things to do after a fresh installation of CentOS 7 Minimum

Things to do after a fresh install of Fedora 23

What to do after installing Ubuntu 14.04, 12.04, 13.0

A static route can be defined as a predetermined path that network information must follow to reach a particular host or network. Static routing is often important for traffic that must pass through an encrypted VPN tunnel or traffic that should be routed for cost or security reasons.
In most cases, the default gateway is used for all non-local networks and the preferred route is not specified in the routing table. Traditionally, the default gateway is a private network router.

You can configure static routes in several ways, one of which is to manually specify the route in the network configuration script. Other methods include using the following command:

# route add
# ip route

One thing to keep in mind when using the “route add” and “ip route” commands is that they configure routing at runtime and do not retain the configuration after a restart. We will discuss different methods of setting up static routes. Both methods are temporary and set up permanent static routes. Use the route add command to add a static route. The syntax is:

route add -net /  dev 

Let’s add routes to the interface eth1,The internet

# route add -net netmask gw dev eth1

Use the ip route command to add a static route. grammar:

ip route add / via  dev 


# ip route add via dev eth1

Adding static persistent routes

Ubuntu / Debian based systems:

vim /etc/network/interfaces


auto eth1
iface eth1 inet static
up route add -net netmask gw
up route add -net netmask gw


auto eth1 iface eth1 inet static
up ip route add via || true


To access the networks and, use the default gw

Static routing configuration can be stored per interface in / etc / sysconfig / network-scripts / route-interface file. For example, the static route for the eth1 interface will be stored in / etc / sysconfig / network-scripts / route-eth1

# vim /etc/sysconfig/network-scripts/route-eth1




Save and close the file.

Restart the network:

service network restart

You can also use:

ifdown eth1;ifup eth1

Note: Subsequent static routes must be numbered sequentially and no value can be skipped. For example, ADDRESS0, ADDRESS1, ADDRESS2, and so on.
Check static routes:

ip route show
netstat -nr
route -n

Different ways to configure static routing on Linux

We have summarized the different ways to configure static routing on Linux. Please let me know if you run into any issues and we will be happy to assist you.

Easy way to configure a static IP address on CentOS 6.x and CentOS 7 servers

How to install the latest Kamailio SIP server on CentOS 7