31.08.2025

How to Set Up Routing in Linux: Add, View, and Configure Routes

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:

ip route show

This is a modern and recommended way. If you are more familiar with the old utilities, you can use:

route -n
netstat -rn

To add a static route, use the command:

ip route add 192.168.10.0/24 via 192.168.1.1 dev eth0

And you can delete a route like this:

ip route del 192.168.10.0/24 via 192.168.1.1 dev eth0

If you want to set a default route, the command will look like this:

ip route add default via 192.168.1.1

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:

auto eth0
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:

700 table1
701 table2

Then, through Netplan, you can configure separate rules for each interface.:

network:
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 generate
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.

Conclusion

Routing in Linux is an essential skill for managing network traffic efficiently. By understanding the routing table, you can control where packets are sent, set static and default routes, and ensure network traffic flows correctly. For simple networks, using ip route commands may be sufficient, while more complex setups with multiple interfaces or providers benefit from policy-based routing. Linux also supports advanced dynamic routing protocols such as OSPF or BGP through daemons like Quagga or BIRD. Mastering these techniques ensures your system maintains reliable connectivity, even in multi-network or multi-provider environments.

FAQ