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.
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
- Q1: How can I view the current routing table in Linux?
A1: You can use the modern command ip route show or legacy commands like route -n and netstat -rn to view your system’s routing table. - Q2: How do I add a static route in Linux?
A2: Use the command:ip route add <network>/<mask> via <gateway> dev <interface>For example:
ip route add 192.168.10.0/24 via 192.168.1.1 dev eth0 - Q3: How do I delete a route?
A3: Use:ip route del <network>/<mask> via <gateway> dev <interface> - Q4: How do I set a default gateway?
A4: Run:ip route add default via <gateway> - Q5: How can I make routes persistent after a reboot?
A5: Edit the network configuration files:
For Debian/Ubuntu: /etc/network/interfaces or Netplan YAML files.
For CentOS/RHEL: /etc/sysconfig/network-scripts/ or use NetworkManager (nmcli/nmtui). - Q6: What is policy-based routing and when should I use it?
A6: Policy-based routing allows you to route traffic differently based on source IP, interface, or other criteria. It’s useful for servers with multiple network interfaces or providers. - Q7: Can Linux handle dynamic routing protocols?
A7: Yes, Linux can run dynamic routing daemons such as Quagga or BIRD to handle OSPF, BGP, and other protocols for advanced network setups. - Q8: How do I apply Netplan changes?
A8: After editing the YAML file, run:netplan generatenetplan apply - Q9: Is ip route preferred over route?
A9: Yes, ip route is the modern and recommended method, offering more flexibility and better integration with current Linux networking tools. - Q10: Can I combine static and policy routing on the same server?
A10: Yes, static routes can coexist with policy-based rules, allowing fine-grained control over network traffic for complex environments.