How To Use Linux Route Add Command
As per OSI (Open System Interconnection) routing is happens at network layer. Routing is fundamental to the design of the Internet Protocol. Main function of Internet Protocol is getting packet to the right machines. If source system and destination system is directly connected, that is on the same network, then routing is simple. What Happen when source system and destination system are not in the same network, at that situation you need router.
A router is device, acts like traffic controller sends packet to the destination network. You have to added routes in the routing table by manually or automatic.
Example: 1
[root@localhost ~]# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
Using Netstat command gives information about routing table.
Example: 2
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
Also route command gives information about routing table. Using route command it is possible to add static routes in the system, routes will be saved till next reboot.
Example: 3
[root@localhost ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.100 eth0
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 192.168.1.100 255.255.255.0 UG 0 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
10.0.0.0 192.168.1.254 255.0.0.0 UG 0 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
In above example 192.168.1.0 network has to be preceded with a –net switch and the subnet mask and gateway values also have to be preceded by the netmask and gw switches respectively.
Example: 3
[root@localhost ~]# route add -host 192.168.1.200 gw 192.168.1.1 eth0
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.200 192.168.1.1 255.255.255.255 UGH 0 0 0 eth0
Administrator can add a route to an individual machine, by using –host switch without netmask value. Note: The route command finds automatically the value of netmask.
Persistent static routes (default gateway) for any linux distribution can be added to /etc/rc.local
Category: Linux Administration
