To set a route, you can use the route command. It is used to add or delete route entries to the kernel routing table. Its general form would be:
$route [add|del] [-net|-host] target [if]
The first parameter, add or del, is used, of course, to add or delete the target into the routing table. –net tells the route command that the target is network, while –host tells that the target is host. –host is the default option. if option is optional, and directs to which network interface the route should be directed. But in most cases, this is not needed, because the kernel can make a very good guess.
Now, lets look at few examples.
Setting a route on a local loopback interface:
$route add 127.0.0.1
The kernel will know that the route should use the lo network interface, and the –net option is not needed.
But what if we want to set a route to an interface, which represents a network? So, lets get the IP 188.8.131.52, which has Ethernet, eth0, interface configured. First:
$ifconfig eth0 eth0 Link encap 10Mps Ethernet HWaddr 00:00:C0:90:B3:42 inet addr 184.108.40.206 Bcast 220.127.116.11 Mask 255.255.255.0 UP BROADCAST RUNNING MTU 1500 Metric 1 RX packets 0 errors 0 dropped 0 overrun 0 TX packets 0 errors 0 dropped 0 overrun 0
To add entry to the routing table, we would have to do the following:
$route add –net 18.104.22.168
So, it will be known that through the eth0 interface, a network can be reached.
But, you might wonder, how the kernel knows that this route should go through the eth0 interface? The procedure is: First, the kernel checks all interfaces which are configured so far, including eth0. Then, it compares the destination address (22.214.171.124 in this case) to the network part of the interface address. The only interface which matches is eth0.
It is also possible to define entries into the /etc/networks file. Then, the –net option can be omitted, because route will know that the entry denotes a network. So, lets assume that we have entry directed to 126.96.36.199, named home-net. Setting a route would go as following:
$route add home-net
So far, we have looked at examples of one, single Ethernet. But, gateways exist to connect two or more Ethernets, or to connect to the Internet. Let assume that we have two networks, home1-net and home2-net. Between them, we have a gateway called home-gateway. So, we have to add a new entry to the kernel routing table, in order home1-net computers to be able to communicate with home2-net computers, through the gateway home-gateway. So:
$route add home2-net gw home-gateway
Of course, all hosts of host2-net , which are supposed to communicate with the host1-net computers, must have routing tables directing to the host1-net computers. Otherwise, they will recieve data from home1-net devices, but will never be able to send back to them.
But, what if the gateway connects to the Internet? That is a very common situation, so setting a route table, directing to any destination network, would look like:
$route add default gw home-gateway
The key word default is a substitute for 0.0.0.0, which represents a default destination network. Also, it doesn’t have to be specified in /etc/networks file, because it is built in the route utility.