How to Configure Multiple Default Gateways and Dual NICs on Linux
This guide explains what a default gateway is, how to set it manually on Linux, and how to handle single, dual, or triple NIC configurations using route and iproute2 commands, including creating separate routing tables, static routes, and bonding for redundancy and load balancing.
Default gateway
A default gateway is the IP address of a router that a host forwards packets to when no more specific route exists in its routing table. It creates a default route (0.0.0.0/0) and is required for communication with external networks.
Single NIC configuration
On a Linux host configure the static address, netmask and gateway in the interface file (e.g. /etc/sysconfig/network-scripts/ifcfg-eth0) using the ADDRESS, NETMASK and GATEWAY directives, then restart the network service ( systemctl restart network).
Multiple NIC scenarios
One gateway : Assign an IP address to each NIC, but configure a gateway on only one of them. Traffic that needs a default route will use the NIC with the gateway, while the other NIC can still communicate on its local subnet.
Two gateways on different subnets : Do not set a default gateway on either NIC. Use the route command to add a default gateway for the preferred subnet and add static routes for the other subnet.
route add default gw 224.224.224.224 eth0
route add -net 192.168.115.0/24 gw 192.168.1.254 eth1Same‑subnet dual NIC : When both NICs belong to the same subnet the ARP table on remote hosts may associate both IPs with the MAC of the default‑gateway NIC, causing communication failures. The remedy is to place each NIC in a separate routing table using iproute2 (policy routing).
Policy routing with iproute2
Create additional routing tables and rules so that traffic originating from a specific source address is routed via the corresponding NIC.
echo "210 local100" >> /etc/iproute2/rt_tables
echo "220 local200" >> /etc/iproute2/rt_tables
ip route add 192.168.1.0/24 dev wlo0 src 192.168.1.11 table local100
ip route add 192.168.1.0/24 dev eno1 src 192.168.1.22 table local200
ip route add default dev wlo0 table local100
ip route add default dev eno1 table local200
ip rule add from 192.168.1.11 table local100
ip rule add from 192.168.1.22 table local200
ip route flush cacheThe kernel selects the routing table with the smallest numeric ID; if one NIC fails, traffic automatically switches to the other.
Linux bonding for redundancy and load balancing
Linux bonding merges several physical interfaces into a single logical interface ( bond0). The driver supports the following modes:
Mode 0 – round‑robin (load balancing)
Mode 1 – active‑backup (failover)
Mode 2 – XOR (based on MAC addresses)
Mode 3 – broadcast (send on all interfaces)
Mode 4 – LACP (802.3ad aggregation)
Mode 5 – transmit load balancing
Mode 6 – adaptive load balancing
CentOS example (bond0 with mode 6)
1. Bring the slave NICs down:
ifdown ens33
ifdown ens382. Create /etc/sysconfig/network-scripts/ifcfg-bond0:
TYPE=Ethernet
BOOTPROTO=static
NAME=bond0
DEVICE=bond0
IPADDR="192.168.10.54"
NETMASK=255.255.255.0
GATEWAY=192.168.10.2
ONBOOT=yes
BONDING_OPTS="miimon=100 mode=6"3. Create slave configuration files:
# ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
MASTER=bond0
SLAVE=yes
# ifcfg-ens38
TYPE=Ethernet
BOOTPROTO=none
NAME=ens38
DEVICE=ens38
ONBOOT=yes
MASTER=bond0
SLAVE=yes4. Load the bonding module and restart networking:
modprobe bonding miimon=100 mode=6
systemctl restart network5. Verify the bond status:
cat /proc/net/bonding/bond0Removing a bond
Bring the bond down: ifdown bond0 Move or delete the bond configuration file (e.g. mv /etc/sysconfig/network-scripts/ifcfg-bond0 /root/).
Remove the bond from the kernel: echo -bond0 > /sys/class/net/bonding_masters Edit the slave NIC files to clear MASTER and SLAVE entries and restore their original static configuration.
Restart networking:
systemctl restart networkAdvanced dual‑NIC routing with separate tables (tel / cnc example)
1. Add custom tables to /etc/iproute2/rt_tables:
251 tel # telecom route table
252 cnc # ChinaNet route table2. Assign addresses to the two NICs:
ip addr add 192.168.0.2/24 dev eth0 # telecom NIC
ip addr add 10.0.0.2/24 dev eth1 # ChinaNet NIC3. Populate each table with a route to the local subnet, the loopback network and a default gateway:
# Telecom table
ip route add 192.168.0.0/24 dev eth0 src 192.168.0.2 table tel
ip route add 127.0.0.0/8 dev lo table tel
ip route add default via 192.168.0.1 dev eth0 table tel
# ChinaNet table
ip route add 10.0.0.0/24 dev eth1 src 10.0.0.2 table cnc
ip route add 127.0.0.0/8 dev lo table cnc
ip route add default via 10.0.0.1 dev eth1 table cnc4. Create policy rules so that packets sourced from each IP use the corresponding table:
ip rule add from 192.168.0.2 table tel
ip rule add from 10.0.0.2 table cncReference
Technical details were originally published at https://www.cnblogs.com/rebrobot/p/17579198.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
