What is routing: Building routing tables in Linux

What is routing: Building routing tables in Linux

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        

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.
ip route add blackhole 10.56.50.0/27        

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

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
route add -net 172.20.0.0/24 gw 192.168.1.6        

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
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.

要查看或添加评论,请登录

Vladimir Lebedenko的更多文章

社区洞察

其他会员也浏览了