Methods of payment Abuse

How to set up routing in Linux

14.03.2021, 18:59

The routes that are necessary to deliver packages to the 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 IP addresses of destinations and the addresses of interfaces and routers in the home network that can send the packet to the desired address. If no route is specified for the target, then the so-called default gateway or default route is used first. Exactly the same pattern is observed on the router. The device looks at the destination IP address and compares it with its routing table, and then sends it on. Next, let's take an example of how to set up certain routes and create new ones.

Looking at the routing table

Before you start changing anything, you need to understand which rules are already in use. To do this, several commands are provided in the Linux operating system. To do this, we use a special route command:

$ route

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. To get more detailed information, you need to use the routel command:

$ routel

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

$ ip route

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

default - the default option. The target's ip address or subnet mask should be here;

via - indicates through which gateway we can get to this goal, we have it;
dev enp2s0 is the network interface through which this gateway will be accessible;
proto static - means that the route was set by the administrator, the value of kernel means that it was set by the kernel;
metric is the priority of the route, the lower the value, the higher the priority.

Next, let's look at how Linux routes are configured.

Setting up routes

The user still has the option to configure the routing table using the ip command. For example, to change the default route, it is enough to do:

$ ip route add default via

So you can add a route for any IP address, for example, for

$ sudo ip route add via

So, we specify the IP address of the target, and then the gateway in the local network through which this address can be reached. The only problem is that such routes are active until the first reboot of the computer. After a reboot, they are automatically deleted. To fix this flaw, routes need to 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 several routes, for example:


This configuration file uses gateway, this is the gateway for the interface, netmask is the network mask, and ipaddr is the ip address of the interface. In Debian and distributions based on it, 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 netmask gw

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

So we looked at how routing works in Linux, how routing is configured in linux, and also why it is needed.