how to configure 2 network interfaces with different gateways

Here is what is in my /etc/network/interfaces file:

auto lo    
iface lo inet loopback

auto eth0    
iface eth0 inet static    
address 172.168.10.252    
netmask 255.255.255.0    
network 172.168.10.0    
broadcast 172.168.10.255    
gateway 172.168.10.1    

iface eth1 inet static    
address 172.168.10.251    
netmask 255.255.255.0    
network 172.168.10.0    
broadcast 172.168.10.255    
gateway 172.168.10.10

I would like to use eth0 as for local network and eth1 for internet. Thanks


Solution 1:

To configure two interfaces say eth0 and eth1 to use two networks 192.168.0.0/24 and 10.10.0.0/24 a tool iproute2 can be used to achieve this.

Steps:

  1. Edit your /etc/network/interfaces:

    auto lo
    iface lo inet loopback
    
    # The primary network interface
    
    allow-hotplug eth0
    iface eth0 inet static
        address 192.168.0.10
        netmask 255.255.255.0
        gateway 192.168.0.1
    
    # The secondary network interface
    allow-hotplug eth1
        iface eth1 inet static
        address 10.10.0.10
        netmask 255.255.255.0
    
  2. Add second routing table by editing `/etc/iproute2/rt_tables:

    #
    # reserved values
    #
    255     local
    254     main
    253     default
    0       unspec
    #
    # local
    #
    #1      inr.ruhep
    1 rt2
    
  3. Populate new routing table:

    ip route add 10.10.0.0/24 dev eth1 src 10.10.0.10 table rt2
    ip route add default via 10.10.0.1 dev eth1 table rt2
    
    # The first command says that the network, 10.10.0.0/24, can be reached through the eth1 interface.
    # The second command sets the default gateway.
    
  4. Add routing rules:

    ip rule add from 10.10.0.10/32 table rt2
    ip rule add to 10.10.0.10/32 table rt2
    
    # These rules say that both traffic from the IP address, 10.10.0.10, as well as traffic directed to 
    # or through this IP address, should use the rt2 routing table
    
  5. Making the Configuration permanent by adding it to /etc/network/interfaces:

    iface eth1 inet static
        address 10.10.0.10
        netmask 255.255.255.0
        post-up ip route add 10.10.0.0/24 dev eth1 src 10.10.0.10 table rt2
        post-up ip route add default via 10.10.0.1 dev eth1 table rt2
        post-up ip rule add from 10.10.0.10/32 table rt2
        post-up ip rule add to 10.10.0.10/32 table rt2
    

Source:

https://www.thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System