Routing table in Linux

Viplove Bansal
4 min readMar 14, 2021

This article provides basic understanding of routing table and a practical task on the same.

To understand routing, first let’s understand what a router is.

Router

In simple terms, a router is networking device which is used to connect different networks together. It forwards data packets between networks.

When a data packet is received by the router it reads its address information and transmits the packet to the desired location. Router forward packets to destination based on the entires of the Routing table. Each router has its own routing table which is stored in the device’s RAM.

Routing

The process of selecting/choosing a path for network traffic is known as routing.

As we have discussed above, the device used to perform this operation is called router. Although are several types of routing and it is a broad topic but we wont discuss it in this article as it is beyond its scope.

Routing Table

A routing table is a set of rules, often viewed in table format, that is used to determine where data packets traveling over an Internet Protocol (IP) network will be directed. All IP-enabled devices, including routers and switches, use routing tables.

📑 To view routing table in linux we use ‘route’ command.

route command will not show the destination network and gateway of default route. To view these use ‘route -n’ command instead.

As you can see routing table consists of several fields namely- destination, gateway, genmask or netmask, flags, metric, ref, use and iface or interface.

Destination is the IP of the destination host.

Gateway defines the IP through which you can reach corressponding destination.

Genmask shows netmask which is used to match a destination IP to network id.

In Flags column, U flag means that the route is up. G flag means that the specified gateway should be used.There are other flags also but won’t see them unless you are using dynamic routing.

Iface shows the network interfaces connected to a system.

Metric indicates the associated cost of using the indicated route. A common use of the metric is to indicate the minimum number of hops (routers crossed) to the network ID.

The Ref column of shows the number of references to this route, that is, how many other routes (e.g. through gateways) rely on the presence of this route. The last two columns show the number of times the routing entry has been used, and the interface that datagrams are passed to for delivery.

📑 If you want to add new routes in routing table use ‘route add’ command

Here i have added a network 192.168.2.0 with netmask 255.255.255.0 and gateway 192.168.1.1 on interface enp0s3. This way you can add your custom routes and check using route -n command.

📑 To delete existing routes use ‘route del’ command.

Now that we know how to add and delete entries in routing table let’s perform a simple task to understand the significance of routing table.

Create a setup so that you can ping to google but you cannot ping facebook from the same system.

To perform this task you need to remove 0.0.0.0 destination network because it is used to connect to internet. And add Google’s destination IP. This way you will be able to ping google but not facebook.

  • Step 1 — Delete 0.0.0.0 destination network so that you are disconnected from internet.
  • Step 2 — You need to add google’s destination IP. To determine that you can you nslookup command.
  • Step 3 — Now you can successfully ping google.
  • Step 4 — If you try to ping facebook you will see an error instead because in routing table there is no destination route for facebook.

Voila!

--

--