Ubuntu Linux Add Static Route

by on October 25, 2012 · 6 comments· LAST UPDATED October 25, 2012

in ,

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?

Tutorial details
DifficultyIntermediate (rss)
Root privilegesYes
RequirementsUbuntu
Estimated completion timeN/A

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 cyberciti.biz

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 6 comments… read them below or add one }

1 Balakrishnan B October 25, 2012 at 5:33 pm

Hi
I am trying to do something similar but not the same. I have two interfaces
eth0 -> wired has connectivity to internet (tikona broadband)
eth1 -> wireless connecting to a local Beetel 450TC1 modem configured in bridge mode
I have two issues :
1) If I connect to wireless router. I am not able to access to internet (most of the times). How do I fix this?
2) I have a android tablet connecting to same wireless router. I want to setup the rules such that I am able to acess internet from my tablet. I first tried to create a wifi hot spot directly from laptop but it is not recognized in the tablet. So trying if I can do something with the wireless router. Tablet is able to connect to the router and also access the web server hosted in the laptop

Reply

2 Alexander October 27, 2012 at 5:40 am

At you the mistake in an example seems. It seems to me the correct address of a router comes to an end on 61 or I am mistaken?

post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.70.201.6 (1)

Reply

3 Govind Tiwari February 17, 2013 at 1:25 pm

Respected Sir,
I try following thing please guide me .
at this time i am using ubuntu 12.04 server edition and i have two network interface in my server. i want using one for direct internet static ip or second for LAN network
——————–
My server Eth0 interface ip is :- 117.239.18.213 , subnet 255.255.255.248, gat- 117.239.18.209, dns – 218.248.255.193,218.248.240.179
And the second eth1 ip is none :-
but i want use 192.168.100.1 to 192.168.100.255 class ip for My LAN network
so please guide me how can i do this . please tell me. also i want use my eth1 is realising ip for local network user accordingly like dhcp. so please guide me the configuration .

Reply

4 james Cordell February 20, 2013 at 11:33 am

Hi

I wanted to add additionally that deleting specific routes can be done like so.

x.x.x.22 x.x.x.173 255.255.255.255 UGH 0 0 0 bond0
x.x.x.21 x.x.x.12 255.255.255.255 UGH 0 0 0 bond0

route del -net x.x.x.22 netmask 255.255.255.255 gw x.x.x.173

Reply

5 Sibin October 29, 2013 at 5:02 am

Hi, Im not able to access internet when i change my linux(rhel6) in static mode from dhcp..can anyone tell me whats the issue…??
I did put the same ip (192.168.1.147) in static mode when it was in dhcp..
gw 192.168.1.1, mask 255.255.255.0.
restarted network…but im not able to access interent..

Reply

6 Tim Macking November 6, 2013 at 7:02 pm

I think this should be: post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.70.201.6 (1)

Reply

Leave a Comment

Tagged as: , , , , ,

Previous Faq:

Next Faq: