Routing in Linux is the process by which the system decides where to send network packets. The routing table is responsible for this. You can use it to specify which gateway to send traffic to a specific network through, as well as configure default routes.
You can view the current routes using the command:
This is a modern and recommended way. If you are more familiar with the old utilities, you can use:
netstat -rn
To add a static route, use the command:
And you can delete a route like this:
If you want to set a default route, the command will look like this:
By default, all added routes are valid only until reboot. To save them, you need to make changes to the configuration files. In Debian and Ubuntu, this can be done in /etc/network/interfaces:
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
up ip route add 10.10.0.0/16 via 192.168.1.254
Sometimes a server has several network interfaces, and it is required that traffic through each of them goes its own way. For this purpose, the so-called Policy Routing is used. First, you need to add your own route tables in the /etc/iproute2/rt_tables file, for example:
701 table2
Then, through Netplan, you can configure separate rules for each interface.:
version: 2
ethernets:
enp0s5:
addresses: [192.0.2.10/24]
gateway4: 192.0.2.1
routes:
- to: 0.0.0.0/0
via: 192.0.2.1
table: 700
routing-policy:
- from: 192.0.2.10
table: 700
priority: 100
enp0s6:
addresses: [198.51.100.10/24]
routes:
- to: 0.0.0.0/0
via: 198.51.100.1
table: 701
routing-policy:
- from: 198.51.100.10
table: 701
priority: 200
After that, you need to apply the settings.:
netplan apply
This way, the server will respond from each public IP via its own interface, which is especially useful when working with multiple providers.
In CentOS, configuration follows the same principle, but using NetworkManager (nmcli or nmtui) or direct file editing in /etc/sysconfig/network-scripts/.
Routing in Linux is a flexible tool. For simple tasks, just a couple of ip route commands are enough, and for complex scenarios with multiple networks and interfaces, you can connect political routing and even dynamic protocols (OSPF, BGP) using specialized daemons like Quagga or BIRD.