6

I have a two network cards, configured like this:

eth0      Link encap:Ethernet  HWaddr 00:19:d1:31:08:e8  
          inet addr:192.168.5.104  Bcast:192.168.5.255  Mask:255.255.255.0
          inet6 addr: fe80::219:d1ff:fe31:8e8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4564126 errors:590 dropped:0 overruns:0 frame:329
          TX packets:9707383 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1151788275 (1.0 GiB)  TX bytes:189318786 (180.5 MiB)
          Interrupt:20 Memory:e0300000-e0320000 

eth1      Link encap:Ethernet  HWaddr 00:e0:4c:51:0d:55  
          inet addr:85.255.103.4  Bcast:85.255.103.255  Mask:255.255.255.0
          inet6 addr: fe80::2e0:4cff:fe51:d55/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5466 errors:0 dropped:0 overruns:0 frame:0
          TX packets:499 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:518961 (506.7 KiB)  TX bytes:34236 (33.4 KiB)
          Interrupt:22 Base address:0x1000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:136 errors:0 dropped:0 overruns:0 frame:0
          TX packets:136 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:15556 (15.1 KiB)  TX bytes:15556 (15.1 KiB)

The ip route:

$ ip route
192.168.5.0/24 dev eth0  proto kernel  scope link  src 192.168.5.104 
85.255.103.0/24 dev eth1  proto kernel  scope link  src 85.255.103.4 
default via 192.168.5.1 dev eth0 
default via 85.255.103.1 dev eth1

When I ping the ip address of eth1, i get no response.

With tcpdump I figured that all the ICMP echo requests sent to eth1 are answered through the eth0 interface.

How can I achieve that both interfaces work correctly? If I ping eth1 it should return answer on eth1 as well.

eth0 has a faster internet connection, I just want to keep eth1 around as well and go through it when the application binds to that specific ip.

4
  • 6
    The first thing that jumps out is the fact that you gave configured two default gateways. Typically there is only one.
    – HBruijn
    Oct 10, 2014 at 16:19
  • I removed eth1 as the default gateway (because i want to go everything by default through eth1). However, I am still unable to receive pings from eth1 or eth0. Oct 10, 2014 at 17:47
  • Are both interfaces connected to the same physical network, or are they different? If they are on different networks, to ping one of them from the other leg, you have to enable routing in your kernel as well (echo 1 > /proc/sys/net/ipv4/ip_forward) Oct 10, 2014 at 22:44
  • They are seperate. eth0 is a local network with a connection to the the internet and eth1 is a directly connected to the internet. Oct 11, 2014 at 10:39

3 Answers 3

6

Remove the gateway defined for eth0 (192.168.5.1). If you need multiple default routes you'll have to use iproute2 to create a policy for it.

3
  • But if I remove that gateway, I won't be able to connect through eth0, right? Oct 10, 2014 at 16:32
  • 2
    You'll be able to communicate with anything on 192.168.5.0/24 because of the first route that is created (thanks to the netmask you set). If you need access to more subnets through eth0 than that then you'll either need a special policy for it or just define a route for all the other subnets you need to access through that interface.
    – Gene
    Oct 10, 2014 at 16:35
  • eth0 has a faster internet connection. Unless otherwise specified, I want to route everything through eth0. I removed the second default route (for eth1) and I still can't ping or ssh into that machine through that interface. Oct 10, 2014 at 17:49
3

It seems you have to remove a second default gateway:

  • route del default 192.168.5.1

After that the 85.255.103.4 IP will work, but you'll have troubles with the 192.168.5.0/24 subnet. To fix that you need to route all packets with a source IP 192.168.5.10 via 192.168.5.1. To do that use policy routing:

  • echo '300 eth0tbl' >> /etc/iproute2/rt_tables
  • ip route add default via 192.168.5.1 table eth0tbl
  • ip rule add from 192.168.5.10 table eth0tbl
5
  • They don't need a policy created for 192.168.5.0/24 since there is a route configured for that already (the first route shown in the ip route command). However, if they need to route to other 192.168.0.0/8 subnets through that interface they can use a policy or define a route explicitly for each subnet the system needs access to.
    – Gene
    Oct 10, 2014 at 16:30
  • "if they need to route to other 192.168.0.0/8" - that's what I predicted and advised to add policy routing for. But yes, can be skipped.
    – Glueon
    Oct 10, 2014 at 16:54
  • That's not what you wrote. "...but you'll have troubles with the 192.168.5.0/24 subnet."
    – Gene
    Oct 10, 2014 at 16:59
  • I don't want to route through eth1, I want to route through eth0 by default. However, I want to be able to connect through eth1 as well (ping/ssh). Oct 10, 2014 at 17:50
  • In that case you will need to set up policy based routing, using iproute2 like @Glueon indicated.
    – Gene
    Oct 10, 2014 at 17:54
1

In situations like this it's best to be explicit.

There are many reasons why you might not be able to ping or ssh to the IP on eth1 but for starters you should configure policy based routing that forces traffic to use the same interface for TX that was used for RX.

You want traffic to use eth0 for the faster connection so we'll leave that for THE default route.

Next we'll define the tables, create the rules and then create the routes.

  1. Define two tables in /etc/iproute2/rt_tables like so:

    100 eth0if
    101 eth1if
    
  2. Create two rules (TX traffic from 192.168.5.104 is forced to use table eth0if, etc...):

    from 192.168.5.104 table eth0if
    from 85.255.103.4 table eth1if
    
  3. Then Create the routes for each interface:

    • For eth0:

      default via 192.168.5.1 dev eth0 table eth0if
      192.168.5.0/24 via 192.168.5.104 dev eth0 table eth0if
      192.168.5.0/24 via 192.168.5.104 dev eth0 table main
      
    • For eth1:

      default via 85.255.103.1 dev eth1 table eth1if
      85.255.103.0/24 via 85.255.103.4 dev eth1 table eth1if
      85.255.103.0/24 via 85.255.103.4 dev eth1 table main
      

Now any socket connection made to eth1 should reply from eth1 and be successful. However, any remote connection made from this box that isn't destined for 85.255.103.0/24 will still use the default route (eth0).

RULE: (If a route doesn't exist, the default is used)

If you need to use eth1 for outgoing connections where you're using software that doesn't allow you to define the source interface/IP then you'll have to create the routes for it.

  • For example, if you need a host route to a specific google server, you would use:

    74.125.224.194/32 via 85.255.103.1 dev eth1 src 85.255.103.4 table eth1if
    
  • Or if you wanted to do the same with the subnet you would:

    74.125.224.0/24 via 85.255.103.1 dev eth1 src 85.255.103.4 table eth1if
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.