≡ Menu

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 via

How do I setup persistent static route so that traffic route via 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.


The syntax is as follows:

route add -net $NET netmask $MASK gw $GATEWAY
route add -net netmask gw

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

post-up command
post-up route add -net netmask gw

Given command will run after bringing the interface up.


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
        ### Ubuntu Linux add persistent route command ###
        post-up route add -net netmask gw
# Setup eth1 - connected to the Internet #
auto eth1
allow-hotplug eth1
iface eth1 inet static
        ### Ubuntu Linux - This is your default gateway ###

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
$ ip route show
Send ICMP ECHO_REQUEST to network hosts on your lan/vlan:
$ ping lan-ip-address
$ ping

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

{ 7 comments… add one }

  • Balakrishnan B October 25, 2012, 5:33 pm

    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

  • Alexander October 27, 2012, 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 netmask gw (1)

  • Govind Tiwari February 17, 2013, 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 :- , subnet, gat-, dns –,
    And the second eth1 ip is none :-
    but i want use to 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 .

  • james Cordell February 20, 2013, 11:33 am


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

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

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

  • Sibin October 29, 2013, 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 ( in static mode when it was in dhcp..
    gw, mask
    restarted network…but im not able to access interent..

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

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

  • Nitin Gupta May 10, 2015, 4:44 am

    I am not able to access the outside network ….
    root@gethost:/etc/network/if-up.d# /sbin/route -n
    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface U 0 0 0 venet0

Leave a Comment

   Tagged with: , , , , ,