jump to navigation

How to add static route in Linux February 9, 2011

Posted by Tournas Dimitrios in Linux.

A routing table is used by TCP/IP network routers (and your home computer) to calculate the destination of packets they are responsible for forwarding . Home network routers utilize a very small routing table because they simply forward all outbound traffic to the ISP gateway which takes care of all other routing steps . Home routers set up their routing tables automatically (dynamic routing ) when connected to the ISP . They generate one routing table entry for each of the ISP’s DNS server (first , second ,third ….. DNS server)  , also a few additional routes are generated for other special cases including multicast and broadcast routes .

Most home network routers prevent you from manually overriding or changing the routing table . However , business routers typically allow network administrators to manually update or manipulate routing tables  ( static routing)  , especially if the corporate computer network is utilised by more than one subnet  .

A desktop / laptop computer has usually one network card , to get connected to the LAN network , and his router table contains only an entry to the default gateway (router ) . The routing table is generated automatically , based on the current TCP/IP configuration of the computer .

In some cases we have to add a second or third network card to our computer , to make it acting as an router . In these cases we have to add manual entries in the routing table .

To view your current default route simply use :

  • netstat -r -n
  • route -n

Kernel IP routing table
Destination Gateway      Genmask        Flags Metric   Ref   Use Iface     U        0         0     0    eth0         UG        0       0     0      lo          UG       0       0     0     eth0

To add a route you must first know the network address of the network you wish to add, and the gateway to that network. Let us say we need to add the network of with netmask to be routed to  as  its  default gateway. here is how to do it:

# route add  -net  netmask  gw

This would add the network with a netmask of and a gateway routed to

Basically that’s it. You can also try an alternative route command : ip route

Read also: Configuring Routes on Linux , with a practical graphical example .

The output of the kernel routing table is organized in the following columns :
Destination The destination network or destination host.
Gateway The gateway address or ’*’ if none set.
Genmask The netmask for the destination net; for a host destination and for the default route.
Flags Possible flags include 

  • U (route is up)
  • H (target is a host)
  • G (use gateway)
  • R (reinstate route for dynamic routing)
  • D (dynamically installed by daemon or redirect)
  • M (modified from routing daemon or redirect)
  • A (installed by addrconf)
  • C (cache entry)
  • ! (reject route)
Metric The distance to the target (usually counted in hops). It is not used by recent kernels, but may be needed by routing daemons.
Ref Number of references to this route. (Not used in the Linux kernel.)
Use Count of lookups for the route. Depending on the use of -F and -C this will be either route cache misses (-F) or hits (-C).
Iface Interface to which packets for this route will be sent.


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s