How to manipulate the routing table in Unix

The routing table of a station can be modified or seen thanks to the route command. It may be used to add or remove entries in the table. The routing table can be displayed by “netstat -r” command (For more information on netstat command please follow: http://computerandu.wordpress.com/2011/03/03/how-to-use-netstat-a-unix-networking-command/).

1. Route command presentation:

route add|delete [-net|-host] []

command keywords signification :
add|delete : add will add an entry in the routing table , delete will remove one ;
-net|-host : -net allows to specify an entry towards a network, -host allows to specify an entry towards a host ;
destination : The destination can be a host station address or a network address followed by the subnet mask length, /20 ; The destination may also be the keyword default. In this case, the packet will be sent to the matching router if the destination network address is not specified in the routing table ;
gateway : the router address which relays the packets from one network to another;
netmask : the subnet mask of the destination network.

2. Add a path in a routing table: Use the add option, and specify in the following order : the -net option if you need to enter a network route or -host if it is a host route, the destination IP address (network or station) and the destination internet address (network or station) and the IP address of the router that will relay the traffic. For instance :
# route add -net 192.168.0 192.168.1.2
add net 192.168.0: gateway 192.168.1.2
will add a path in the routing table : to reach the network 192.168.0/24, you must go through the 192.168.1.2 router.

3. Remove an entry in the routing table: Use the delete option and specify host or network options like in the addition process. For instance :
# route delete -net 192.168.0/24
will remove the previous entry added in the routing table.

4. Delete all the routing table entries: The flush command will remove all the entries added with the add command. If you use this command remotely, it may suspend all your current communications with this router and make this station unreachable.

Remark : do not remove the loopback entry, 127.0.0.1 or localhost, which enables the application to communicate localy.