3

My pi is connected to my home router using wlan0, and have access to internet successfully

When I plug my pi with an Ethernet cable, connected to other router, not connected to internet, I cant ping anymore with answer "Destination Net Unreachable"

pi@bar:~ $ sudo ping -c 1 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
From 192.168.100.1 icmp_seq=1 Destination Net Unreachable

--- 8.8.8.8 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms

If I ping using wlan0 it succeed:

pi@bar:~ $ sudo ping -c 1 -I wlan0 8.8.8.8
PING 8.8.8.8 (8.8.8.8) from 192.168.1.151 wlan0: 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=40 time=83.7 ms

--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 83.748/83.748/83.748/0.000 ms

My routing table:

pi@bar:~ $ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.100.1   0.0.0.0         UG        0 0          0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 wlan0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 wlan0
192.168.100.0   0.0.0.0         255.255.255.0   U         0 0          0 eth0

So obviously eth0 is prioritize over wlan0, and with no internet access it cannot find 8.8.8.8.

How can I solve this problem? I want that any app (ping, curl whatever) will be able to reach internet, even if one of the interfaces (wlan0, eth0 or any other) is connected to a router with dead end?

cheers, Bar

BarLesh
  • 31
  • 3

2 Answers2

1

You have two default routes:

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.100.1   0.0.0.0         UG        0 0          0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 wlan0

The default route is used for all ip packages that destination address does not belong to direct connected local networks. So all destination ip addresses to the internet will use the default route. The kernel can only use one default route. What route it uses is determined by the metric. It uses the default route with the lowest metric. You may have a look at details for this issue at In Linux, what metric has a route with no metric?.

The easiest way is to just delete the default route that shouldn't be used so you only have one. If you want the default route on eth0 then just execute:

rpi ~$ sudo ip route del default dev wlan0
Ingo
  • 42,961
  • 20
  • 87
  • 207
0

Try removing the default route and adding one manually:

ip route del default
ip route add default via 192.168.1.1 dev wlan0

If this works, you'll have to find a way to execute this automatically every time your RPi connects to the network. My first guess would be to add this script to /usr/lib/dhcpcd/dhcpcd-hooks/.

Dmitry Grigoryev
  • 28,277
  • 6
  • 54
  • 147