What is routing: Building routing tables in Linux

What is routing: Building routing tables in Linux

06.10.2022
Author: HostZealot Team
2 min.
1102

On the Internet, all information is transmitted in the form of small blocks of data – the so-called packets. The packet itself consists of the start bits, header, trailer, and payload – each such block with data is transmitted along a specific route, which, in turn, is determined by the router. The network route is "laid" based on the information received from the routing tables according to the routing protocols and instructions of the network administrator.

Routing tables contain the parameters necessary for the correct identification and reading of the network route. They contain the following sections:

  • Destination (Target). The destination network IP address is the final destination for data packets.
  • Netmask (Genmask). The network mask.
  • Gateway. The IP address of the gateway.
  • Interface. The address of the network interface.
  • Metric. This parameter determines the priority of the route.

Also, optionally, the sender's address, the size of the TCP window, and the maximum packet size can be specified in the routing table. All this makes it possible to optimize data transmission within the network. Later in the article, we will tell you how to manage Linux routing and share useful configuration recommendations.

Commands for viewing the routing table

Routing Management for Linux OS is carried out with three commands:

  • route – allows you to view the routing table that is currently functioning;
  • netstat – displays more detailed information, including the IP address of the target, gateway, and sender, and also shows the data transfer protocol used and the network interface;
  • ip – this tool is used for the deep configuration of network interfaces.

Let's look at some basic examples of using these commands in Linux.

route -n 
netstat -rn
ip route show

cat /proc/net/route

These commands allow you to view the routing table or some of its individual elements.

In this case, the route command can only be used to get information in full numeric form, but it will not be possible to configure routing policies with it. The ip command should be used for this purpose. For example:

ip route add 172.16.10.0/24 via 192.168.1.1 – add a route through the gateway;
ip route add 172.16.10.0/24 dev eth0 – add a route through the interface;
ip route add 172.16.10.0/24 dev eth0 metric 100 – a route with a metric.

There is also the possibility of creating a "zeroed" route, and packets passing through it will be automatically deleted due to "No route to host". To do this, use the command:

ip route add blackhole 10.56.50.0/27

 It is recommended to use it for DOS attacks from the host.

what is routing: building routing tables in linux

Commands for actions with routes

As we noted above, the add command allows you to add routes. But this is not the only possible operation, there are three others:

  • del — to delete the route.
  • replace — allows one route to be replaced by another.
  • change — to change the route parameters.

Examples of static routing

Imagine that you have two offices connected to each other via an IP tunnel. One office has an IP address of 192.168.1.4, and the second one has an IP address of 192.168.1.6. To connect a local network between them, you need to register the command on the second router:

route add -net 172.16.10.0/24 gw 192.168.1.4

As a result, you will connect the gateway "192.168.1.4" for the network "172.16.10.0/24". After that, we prescribe the following on the first router:

route add -net 172.20.0.0/24 gw 192.168.1.6

As you can see, everything is quite simple and transparent.

To change the LAN of the second router, we simply delete the old entry:

route del -net 172.20.0.0/24 gw 192.168.1.6

And then add a new route on the first router:

route add -net 172.20.0.0/24 gw 192.168.1.6

Where are the configuration files located

Static routing configs will be located in different directories, depending on the distribution used:

  • Debian GNU/Linux: /etc/network/interfaces
  • RHEL/CentOS/Scientifix: etc/sysconfig/network-scripts/route-<interface name>
  • Gentoo: /etc/conf.d/net

In them, you can also set certain settings and parameters for routing. If you have any questions, please contact our specialists via Livechat.

Related Articles