Methods of payment Abuse

How to configure routing in Linux

14.03.2021, 18:59

The routes that are required to deliver packets to their final destination are configured in the interface or router. If the computer is tasked with sending a packet to the network, it looks at the routing table, which lists the destination IP addresses and the addresses of interfaces and routers on the home network that can send the packet to the correct address. If no route is specified for the destination, the so-called default gateway or default route is used first. The exact same pattern is observed on the router. The device looks at the destination IP address and checks it against its routing table and then sends it onward. Next, let's look at how to configure specific routes and create new routes using an example.

Looking at the routing table

Before you start changing anything, you need to understand what rules are already in use. For this purpose, the Linux operating system provides several commands. For this purpose, we use the special route command:

$ route

Маршрутизация в Linux

This is what the linux routing table looks like. It provides simple information, which is not always enough to understand the essence of the matter. For more detailed information, you need to use the routel command:

$ routel

Маршрутизация в Linux

Here you can see the IP address (target), gateway IP address, source IP, protocol, and even the network interface. But there is a more convenient way to see the linux routing table using the IP command:

$ ip route

Маршрутизация в Linux

We see a result that is similar to the output of the previous command. Since the output is not in the most convenient form, you can use ip route add or ip route del as an argument to ip route add or ip route del. Believe me, this is very convenient. This way you will see 192.168.1.1 as the default gateway everywhere. Next let's talk about what the output of this command means:

  • default - default option. This should be the target ip address or subnet mask;
  • via 192.168.1.1.1 - indicates through which gateway we can reach this target, for us it is 192.168.1.1;
  • dev enp2s0 - the network interface through which this gateway will be accessible;
  • proto static means the route was set by the administrator, kernel means it was set by the kernel;
  • metric - this is the priority of the route, the lower the value, the higher the priority.

Next let's see how Linux routes are configured.

Configuring routes

The user still has the ability to customize the routing table using the ip command. For example, to change the default route all you need to do is execute:

$ ip route add default via 192.168.1.1

This way you can add a route for any IP address, such as 243.143.5.25:

$ sudo ip route add 243.143.5.25 via 192.168.1.1

So, we specify the target IP address and then a gateway on the local network through which to reach that address. The only problem is that such routes are active until the first reboot of the computer. After the reboot, they are automatically deleted. To correct this deficiency, the routes must be added to the configuration file.

For example, Red Hat family systems have configuration files /etc/sysconfig/network-scripts/route-ethX. Each file can describe multiple routes, for example:

GATEWAY=10.10.0.0.1
NETMASK=255.0.0.0.0
IPADDR=10.10.0.0.22

In this configuration file, gateway is the gateway for the interface, netmask is the netmask, and ipaddr is the ip address of the interface. In Debian and based distributions, you can configure routes in the /etc/network/interfaces file. Here the route command is added to the iface section. For example:

up route add -net 10.10.0.0 netmask 255.0.0.0 gw 10.10.0.1

Маршрутизация в Linux

With the iface -net we specify the target network, netmask is the network mask and gw is the gateway. There is nothing complicated about this. Now the added routes will remain even after a reboot.

So we have seen how routing works in Linux, how routing is configured in Linux, and why it is necessary.