How to Configure Multiple Network Interfaces and Source-Based Routing on CentOS 7
To ensure server availability across multiple network interfaces and public IP addresses, simply configuring each network card is not enough. Since the system allows only one default gateway, all outgoing packets will route through that single gateway, regardless of the interface they arrived on. This limitation stems from the default destination-based routing policy. In this tutorial, we will demonstrate how to configure multiple network interfaces on CentOS 7 with source-based routing, enabling outbound traffic to be routed based on its source address for improved reliability and network performance.
Configuring network interfaces
If you use a server in Serverspace, then the parameters of network interfaces are configured automatically when you add them or create a server. Otherwise, it is necessary to bring the parameters to the form below. The above example can be copied by substituting your values for the interface name (eth0), gateway (GATEWAY), MAC address (HWADDR), IP address (IPADDR) and in some cases the subnet mask (NETMASK):
DEFROUTE=yes
DEVICE=eth0
GATEWAY=33.44.55.1
HWADDR=aa:11:bb:22:cc:33
IPADDR=33.44.55.66
MTU=1500
NETMASK=255.255.255.0
ONBOOT=yes
STARTMODE=auto
TYPE=Ethernet
The GATEWAY is set up only for one network interface, since there should only be one in the system. Configuring the second interface:
DEVICE=eth1
HWADDR=bb:cc:dd:ee:ff:gg
IPADDR=66.77.88.99
MTU=1500
NETMASK=255.255.255.0
ONBOOT=yes
STARTMODE=auto
TYPE=Ethernet
Similarly, you can configure the number of interfaces that are connected to the system.
Setting up source based routing
Just in case, it is worth checking if the iproute package is installed and install if not:
Now open the following file:
Add the following lines to the end:
201 table201
The record must be in the format number space table name. Both values can be arbitrary. The only condition is their uniqueness relative to the values of other records in the file. The number of lines must correspond to the number of network interfaces for which we are configuring accessibility.
Now let's create files with routing tables parameters. You need to create a separate such file for each interface, replacing eth0 in the file name with the actual name of the network connection.
Below is the contents of the file, where instead of 33.44.55.66 you need to put the IP address of the network interface, and instead of table200 - the tables added above, one for each interface:
And the corresponding files with routing rules. Created in the same way as routing table parameter files.
It is necessary to substitute the actual values of the subnet address, gateway, interface name and the table value corresponding to the value from the previous file.
default dev eth0 via 33.44.55.1 table table200
Such file pairs are created for each network interface in the system with the corresponding values.
Restart the network service for the changes to take effect:
Conclusion
Configuring multiple network interfaces on CentOS 7 with source-based routing ensures that outbound traffic is routed through the correct interface based on its source IP address. This setup overcomes the limitation of having only one default gateway in traditional destination-based routing, thereby improving server availability and network reliability across different public IP addresses. Following this guide allows system administrators to efficiently manage complex network configurations and optimize traffic flow in multi-interface environments.
FAQ
- Q: Why can't I set multiple default gateways for different network interfaces?
A: Linux supports only one default gateway at a time. Source-based routing solves this by using multiple routing tables to direct traffic based on its source address rather than destination alone. - Q: What is source-based routing and why is it important?
A: Source-based routing directs outbound packets according to their source IP address. This allows traffic to leave through the interface it was received on, which is critical for multi-homed servers with multiple public IPs. - Q: How do I verify if source-based routing is working correctly?
A: You can use commands like ip route show table <table_name> to inspect routing tables and ip rule show to view routing rules. Additionally, testing connectivity from each interface’s IP address confirms proper routing. - Q: What if the iproute package is not installed?
A: The iproute package provides the ip command required for advanced routing. Install it using yum install iproute on CentOS 7 before configuring source-based routing. - Q: Can I configure source-based routing for more than two interfaces?
A: Yes, you can create as many routing tables and rules as needed, one for each network interface, by following the pattern shown in this guide. - Q: Do I need to restart the server after configuring source-based routing?
A: Restarting the network service with systemctl restart network is sufficient for the changes to take effect; a full server reboot is usually not necessary. - Q: What happens if I misconfigure routing tables or rules?
A: Misconfiguration can cause loss of connectivity or traffic misrouting. Always backup existing configurations before changes and test connectivity carefully after applying new rules.