How to add static route in Linux February 9, 2011Posted 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
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
127.0.0.0 127.0.0.1 255.0.0.0 UG 0 0 0 lo
0.0.0.0 192.168.1.1 0.0.0.0 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 10.0.0.0 with netmask 255.255.255.0 to be routed to 192.168.1.254 as its default gateway. here is how to do it:
# route add -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.1.254
This would add the network 10.0.0.0 with a netmask of 255.255.255.0 and a gateway routed to 192.168.1.254.
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; 255.255.255.255 for a host destination and 0.0.0.0 for the default route.|
|Flags||Possible flags include
|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.|