Friday, Jun 23rd

Last update12:59:40 PM GMT

IP routing – Finding the way

Write e-mail

When we drive somewhere, we need to know about the destination address & the route to reach there and later we remember some of the important places on the way from the source to destination. When there is more than one way to go, then there are signboards to direct you how to reach the destination. Like this in the networking world, when a packet travels from a source to a destination, it also requires a destination address & a route to follow. It also needs to remember the IP address & MAC address of the machines on the way and when there are multiple connections on some machine, it is also given the direction to go through particular interface to reach destination. Let’s discuss it with a very simple example below.



If 2 machines are directly connected to each other & have the IP address on the same network, they can communicate to each other.  Here Linux 1 & Linux 2 are connected to each other on network, so they can communicate to each other. Like in the above diagram, if we have to send some packet from Linux1 machine to Linux3 or vice versa, which are connected through other machines, then we have to add some route on each machine. In the route, we only remember the next hop to reach destination.

Routing on different machines will be as follows

Linux 1 – route add gw

Linux 2 – route add gw

Window 1 – route add

Linux 3 – route add gw

NOTE: The syntax to add the route on different operating systems is different. All commands run on Command Prompt (window) or Terminal (Linux).

Syntax of route command is

route <add/del> [-net] <destination ip address> [netmask <subnet>] gw <next hop>


route – is the actual command to add or delete IP routing

add/del – is used to add or delete IP routing

net – is used to set the whole subnet as destination

destination IP address – is the IP address of other side of the network

netmask <subnet> – is used with net to specify the subnet

gw <next hop> – to specify the gateway IP address, it could be any router or computer.

For example:

  • If we want to add a route for the whole network then

route add –net netmask gw

  • If we want to delete a route for the whole network then

route del –net netmask gw

  • To check the routing table or the routes added in the machine, command is

netstat –nr

route PRINT (only in windows)

Routing is nothing but a way to reach the destination by using some intermediate nodes. Here in the diagram, machines are directly connected. But in a real scenario, machines are connected through switches/routers/bridges/firewall/gatewayWhen we add a route, it stores in the routing table of computer or interconnect device & on delete, the entry is removed from the routing table. Every node just stores the MAC address of directly connected node to forward the packets because actually packets are forwarded on MAC address. L2 switches work on Layer 2 of OSI (open system Interconnection) i.e. Data Link layer & L3 switch called router as well works on Layer3 i.e. network layer. Sometimes, we have to use routing protocols like OSPF (Open Shortest Path First), RIP (Routing Information Protocol) as well to find the optimised route.

A few important points to keep in mind

  • We normally add a route, which is called the default route. The purpose of this route is that, if we are giving any dedicated route, then the packet will pass through default gateway. e.g.

route add default gw

  • On reboot the machine, the routes get deleted. We can add the routes permanently. To add the routes permanently –
    • In Window : route add –p –net netmask
    • In Linux : We need to add the routes in /etc/rc.local,
  • If we add the route to same destination with different netmask or gateway or interface, packet will get drop, because it just get confused.
  • Packets normally transferred between the different interfaces of same machine. If it is not happening, check the IP forwarding & if it is not 1, then set it to 1.

sysctl –a | grep net.ipv4.ip_forward      (to check)

sysctl –w net.ipv4.ip_forward=1             (to set)

  • Two machines directly connected contain each other MAC address. To get the MAC address, there is ARP (Address Resolution Protocol). According to this protocol, machine broadcast a special request packet to all connected nodes. Then the node, who has the IP address of same subnet, replies back with its MAC or Ethernet address. Machine store the MAC address in arp table & use MAC address to communicate with directly connected node. To check the arp table, command is

arp –a

  • Some routes are added automatically, when we give the IP address to any interface. In the routing table, it is added with gateway
  • To route a packet, interface should be active or up
    • mii-tool   (to check the list of interface with configuration &status)
    • ethtool eth0 up   (to bring the interface active, if it inactive)
    • ethtool eth0 down    (to bring down the interface, if it is up)
  • Two interface connected to each other should have same duplex, speed & negotiation configuration. Commands for linux are
    • mii-tool    (to check the configuration of all interfaces)
    • ethtool –s eth0 speed [100/1000] duplex [half/full] autoneg [on/off]   (to change the configuration)


Share this post

Add comment

Please refrain from using slang or abusive language in the comments.
To avoid waiting for your comment to be published after being reviewed, please log in as a registered user.

Security code

Web Hosting