Skip to content

Adrian Giacometti

  • Home
  • Useful links
  • About Me

How to configure 1 Linux host with 2 NICs and 2 Default Gateways

5th August 2021 by Adrian Giacometti

Historically, it was not possible to have 2 default gateways in a Linux host. 

You can SEND traffic to both NICs, but the reply will always come from the NIC which has the default gateway.

This will generate asymmetric traffic and it will be denied in the network or by the originating host. Asymmetric traffic means that the packets will go back using a different path. In this case by ping to NIC1 the return will be sent by NIC2.

But with iproute2, it can be done with 2 commands.

Let’s do this!

This is the basic scenario:

Initial setup:

  • Network: if you don’t have a firewall like in the diagram, then be sure to enable “unicast reverse path forwarding” at the router. This will block any asymmetric reply (in firewalls is the default behavior).
  • Linux host: I’m using Ubuntu. Setup the NICs by editing the file /etc/network/interfaces, in this case:
iface eth0 inet static
	address 10.10.0.10
	netmask 255.255.255.0
	gateway 10.10.0.1
	up echo nameserver 8.8.8.8 > /etc/resolv.conf
auto eth0

iface eth1 inet static
	address 10.20.0.10
	netmask 255.255.255.0
auto eth1

Setup iproute2 in the Linux host

In order to enable the use of 2 default gateways, we need to set up a new route table and a route rules. This will make the traffic entering NIC2 go back using the same NIC2.

sudo ip route add default via 10.20.0.1 dev eth1 table 1001
sudo ip rule add from 10.20.0.0/24 table 1001

Done, now I can externally access both NICs independently. 

Check by doing some pings from a remote network and verify with tcpdump -i eth0 and/or eth1.

Should see that each packet flows by the NIC it should.

Finally, to make the changes permanent to add these lines to /etc/network/interfaces.

iface eth1 inet static
    address 10.20.0.10
    netmask 255.255.255.0
    
    post-up ip route add default via 10.20.0.1 dev eth1 table 1001
    post-up ip rule add from 10.20.0.0/24 table 1001

You can check the resulting config with these commands:

/# sudo ip route show table main
default via 10.10.0.1 dev eth0 
10.10.0.0/24 dev eth0 proto kernel scope link src 10.10.0.10 
10.20.0.0/24 dev eth1 proto kernel scope link src 10.20.0.10 

/# sudo ip route show table 1001
default via 10.20.0.1 dev eth1 

/# sudo ip rule show
0:	from all lookup local
32765:	from 10.20.0.0/24 lookup 1001
32766:	from all lookup main
32767:	from all lookup default

It was way more easy than expected right?

I’ve seen other configurations using iptables to select the routing tables by using traffic marking. But for something this simple it doesn’t make any sense to get into iptables.

In the next post, I will extend the scenario, to support 2 network isolated Docker containers (front-end and back-end) in 1 Linux host, with 2 NICs and 2 default gateway.

Cheers!

Share on Social Media
linkedin twitter email

Post navigation

Previous Post:

The power of simplicity applied to monitoring

Next Post:

How to set up 2 network isolated Docker containers (front and back-end)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Translate to your language

Site search

Tags

ansible automation aws bcp books chatbot chatops cisco cisco aci cloud automation desing devops drp enterprise f5 fastapi free GCP gitlab ci gitops high availability internet iproute2 isp learning linux monitoring nat netdevops netmiko netops network network automation network backup opensource oracle oci python redundancy saltstack slack slackops terraform vpn vrf webinar

Blog Stats

  • 21,120 hits

RSS ipSpace.net blog

  • Network Digital Twins: Between PowerPoint and Reality
  • Dear Vendors, EVPN Route Attributes Matter
  • Public Videos: Whole IPv6 Curriculum
  • netlab 25.06: Fixing Nokia SR-OS Configuration Templates
  • Finding Source Routing Paths

RSS Potaroo blog

  • A QUIC Progress Report
  • A Day in the Life of BGP
  • Resilience in the RPKI
  • Analysis of a Route Leak
  • Jevons Paradox and Internet Centrality

Archives

  • 2024 (1)
    • January (1)
  • 2023 (1)
    • August (1)
    • July (1)
  • 2021 (1)
    • November (1)
    • August (2)
    • July (1)
    • May (2)
    • April (3)
    • February (1)
  • 2020 (1)
    • December (2)
    • October (1)
    • September (1)
    • June (1)
    • April (5)
    • March (1)
    • February (2)
    • January (2)
  • 2019 (3)
    • December (3)

Follow & Contact info

  • LinkedIn
  • GitHub
  • Twitter
  • YouTube
  • Email
  • RSS Feed

Subscribe to stay updated

Loading
© 2025 Adrian Giacometti