28 June 2025

Access internet on Raspberry Pi when connected to two network interfaces

I had a Raspberry Pi 3B connected via LAN cable to a Wifi router which wasn't connected to the internet. Through the router I could connect to the RPi via my desktop PC's Wifi. So VNC and SSH worked nicely in terms of being able to view the RPi desktop and being able to exchange files via scp. However, when I activated my phone's hotspot and the RPi's Wifi connected to the hotspot, I could not access internet.

I soon found out that this happened because the RPi gave a higher priority to the LAN connection than to the Wifi hotspot. So it was just a matter of changing the priority in a few simple steps.

Type `ip route show` to see the existing connections. You may see something like this:

default via 192.168.1.1 dev eth0 proto dhcp src 192.168.1.2 metric 100

default via 192.168.42.1 dev wlan0 proto dhcp src 192.168.42.125 metric 600

What this means is that the gateway for the LAN connection (eth0) is 192.168.1.1 and the IP is 192.168.1.2. The gateway for the wireless connection (wlan0) is 192.168.42.1 and the IP is 192.168.42.125. Since the LAN is listed first, it has the higher priority. So you simply need to add the route to the wireless IP address:

sudo ip route add default via 192.168.42.125 dev wlan0 

You can confirm it got added to the top of the list by typing `ip route show`.

Then simply restart the network service. If `sudo systemctl restart networking` or `service networking restart` gives this error: `Failed to restart networking.service: Unit networking.service not found`, then you can simply type (it didn't disrupt my VNC connection):

sudo ifconfig eth0 down && sudo ifconfig eth0 up &

This will restart eth0 and you'll be able to connect to the internet. You can check by typing:

ping 8.8.8.8 

No comments: