How To Use Linux Route Add Command

April 23, 2011 | By
| Reply More

As per OSI (Open System Interconnection), routing happens at network layer. Routing is fundamental to the design of the internet protocol. The main function of internet protocol is getting packets to the right machines. If source system and destination system are directly connected on the same network, then routing is simple. What happens when source system and destination system are not in the same network, in this situation you need router.

A router is device that acts like traffic controller and sends packets to the destination network. You have to add routes in the routing table manually or automatically.

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, it 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 preceded with a –net switch and the subnet mask and gateway values also have preceded by the netmask and gw switches respectively.

Example: 4

[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.

Filed Under : HOWTOS, LINUX COMMANDS, LINUX HOWTO

Tagged With : ,

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.