It is common to have interconnected networks. For example, different departments at work or school may be on separate networks. When a device on one network wants to communicate with a device on the other network, it needs to send packets through a device which is common to both networks. This device is called a gateway and its function is to route packets to and from different networks.
The operating system maintains a table called the routing table, which contains the information on how packets are to be forwarded through machines on the network. The route command displays the routing table:
$ route Kernel IP routing table Destination Gateway GenmaskFlags Metric Ref UseIface 192.168.0.0 * 255.255.252.0 U 2 0 0wlan0 link-local * 255.255.0.0 U 1000 0 0wlan0 default p4.local 0.0.0.0 UG 0 0 0wlan0
Alternatively, you can also use this:
$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref UseIface 192.168.0.0 0.0.0.0 255.255.252.0 U 2 0 0 wlan0 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 wlan0 0.0.0.0 192.168.0.4 0.0.0.0 UG 0 0 0 wlan0
Using -n specifies to display the numeric addresses. By default, route will map the numeric address to a name.
When your system does not know the route to a destination, it sends the packet to a default gateway. The default gateway may be the link to the Internet or an inter-departmental router.
The route add command can add a default gateway:
# route add default gw IP_ADDRESS INTERFACE_NAME
Consider this example:
# route add default gw 192.168.0.1 wlan0