Ubuntu Linux Add Static Route

I‘ve two network interfaces. One connected to eth0 and another connected to eth1 as follows:


eth0 => private network with private IP in 10.x.x.x/8 range.
eth1 => ISP router with public IP 205.153.203.98 via 205.153.203.97.

How do I setup persistent static route so that 10.0.0.0/255.0.0.0 traffic route via 10.70.201.61 router only?

You need to edit /etc/network/interfaces file. This file describes the network interfaces available on your system and how to activate them including ubuntu Linux server static routing.

Syntax

The syntax is as follows:

route add -net $NET netmask $MASK gw $GATEWAY
route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254

You need to add above syntax to post-up command as follows:

post-up command
post-up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254

Given command will run after bringing the interface up.

Example

Edit /etc/network/interfaces, enter:
$ sudo vi /etc/network/interfaces

#---------------------------------------#
# Feel free to change IP and gateway    #
# as per your local setup and routing   #
# policy                                #
# Last edited by root @ 23/Oct/2012     #
#---------------------------------------#
 
#--------------------------------------------#
# Setup the loopback network interface (lo0) #
#--------------------------------------------#
auto lo
iface lo inet loopback
 
#--------------------------------------------#
# Setup eth0 - connected to private LAN/VLAN #
#--------------------------------------------# 
auto eth0
allow-hotplug eth0
iface eth0 inet static
        address 10.70.201.5
        netmask 255.255.255.192
        ### Ubuntu Linux add persistent route command ###
        post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.70.201.6
 
#----------------------------------------#
# Setup eth1 - connected to the Internet #
#----------------------------------------#
auto eth1
allow-hotplug eth1
iface eth1 inet static
        address 205.153.203.98
        netmask 255.255.255.248
        ### Ubuntu Linux - This is your default gateway ###
        gateway 205.153.203.97

Save and close the file. You need to restart the networking as follows:
$ sudo service networking restart
OR as root user:
# service networking restart

How do I verify that my Ubuntu Linux interfaces static routing working correctly?

Type the following command to see current routing table, run:
$ /sbin/route -n
OR
$ ip route show
Send ICMP ECHO_REQUEST to network hosts on your lan/vlan:
$ ping lan-ip-address
$ ping 10.70.201.6

Send ICMP ECHO_REQUEST to network hosts using the ISP gateway (ping requests to public sites such as Google/Yahoo):
$ ping google.com
$ ping sxi.io

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.