10

I have a Raspberry PI 2 model B with Raspbian version Jessie and I'm trying to bring the internet from my PC to the Raspberry. I have 2 network interfaces between them, one is over Ethernet and the other one is over Wifi. I have configured the Ethernet ip address in the Raspberry to be static, because I have a small computer network at home all with static IP adresses and I want the raspberry to be part of it. As my internet is very slow (I use a dial up modem) I don't want to share this slow internet through the ethernet interface because I don't want all the computers connected to this network to drain my internet speed. As I only intend to bring the internet to the Raspberry I have created a Wifi hotspot in my PC which dynamically assigns an IP adress to my Raspberry (the hotspot software has some sort of DHCP) and that's how I share my slow internet just with the Raspberry.

But then my problem begins, by default after I turn it on (or if I disconnect and connect again), the Raspberry doesn't connect to internet. By typing ifconfig I get:

eth0     
Link encap:Ethernet  HWaddr b8:27:eb:4e:35:65  
inet addr:100.100.100.25  Bcast:100.100.100.255  Mask:255.255.255.0
inet6 addr: fe80::ba27:ebff:fe4e:3565/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

wlan0     
Link encap:Ethernet  HWaddr 74:da:38:55:f3:a2  
inet addr:192.168.137.201  Bcast:192.168.137.255  Mask:255.255.255.0
inet6 addr: fec0::12:c4f1:c3fc:eb1e:3153/64 Scope:Site
inet6 addr: 2002:be0f:9cea:12:1bc0:1969:c17d:f854/64 Scope:Global
inet6 addr: fe80::bdca:7255:2e27:8341/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

When I type route -n I get:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         100.100.100.1   0.0.0.0         UG    202    0        0 eth0
0.0.0.0         192.168.137.1   0.0.0.0         UG    303    0        0 wlan0
100.100.100.0   0.0.0.0         255.255.255.0   U     202    0        0 eth0
192.168.137.0   0.0.0.0         255.255.255.0   U     303    0        0 wlan0

Here you can see how the metric of my static Ethernet connection gateway (100.100.100.1) is smaller than the metric of my DHCP Wifi connection gateway (192.168.137.1) so my Raspberry tries to get the internet through the wrong interface

after I type:

sudo route delete  default gateway 192.168.137.1 

and then :

sudo route add  default gateway 192.168.137.1

my routing table is fixed to:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.137.1   0.0.0.0         UG    0      0        0 wlan0
0.0.0.0         100.100.100.1   0.0.0.0         UG    202    0        0 eth0
100.100.100.0   0.0.0.0         255.255.255.0   U     202    0        0 eth0
192.168.137.0   0.0.0.0         255.255.255.0   U     303    0        0 wlan0

and the Raspberry is able to access to internet.

My questions are:

  1. Is there a way to automatize this process so I won't have to manually do it every time I turn on the Raspberry ?
  2. Is there a way to automatize it so that it will always recognize it must get the internet from the Wifi independently of the IP range given by the hotspot DCHP to this interface?
  3. Why was the lower metric automatically assigned to the Ethernet interface?

I have already checked the solution in How to make a change to the routing table persist? but is doesn't work for me because in Raspbian Jessie you don't directly edit the /etc/network/interfaces rather you edit the /etc/dhcpcd.conf to get the static ip address

10

Is there a way to automatize this process so I won't have to manually do it every time I turn on the Raspberry?

That's not needed if question #2 can be solved - and it can...

Is there a way to automatize it so that it will always recognize it must get the internet from the Wifi independently of the IP range given by the hotspot DCHP to this interface?

In /etc/dhcpcd.conf add

interface wlan0;
metric 200;

interface eth0
metric 300;

This will assign the lower metric to the WLAN interface, so that one will be chosen for Internet requests.

Why was the lower metric automatically assigned to the Ethernet interface?

Because that's the way metrics are assigned according to man dhcpcd.conf (5):

 metric metric
         Metrics are used to prefer an interface over another one, lowest
         wins.  dhcpcd will supply a default metric of 200 +
         if_nametoindex(3).  An extra 100 will be added for wireless
         interfaces.
  • +1 Thank you so much I didn't have an answer yet. I was running a manual script every time I needed to do this – VMMF Jul 8 '16 at 21:03
  • Note, this doesn't work for non DHCP connections. If you have a static connection in /etc/network/interfaces you can set the gateway metric by replacing gateway xxx.xxx.xxx.xxx with up route add default gw xxx.xxx.xxx.xxx metric ### – Sameer Puri Jul 23 '17 at 20:04
0

What I do is simply to make it run on boot.

Add sudo route delete default and sudo route add default gateway 192.168.137.1, in that specific order, in your /etc/init.d/rc.local file.

I forgot if /etc/init.d/rc.local works out of the box for Jessie, or even if you should touch it or not, but it should be trivial to find another solution (I already did).

And this solves #1.


For prioritization of connections, I recommend you install ifmetric. Then, simply prioritize your connections in /etc/network/interfaces

Highest priority is 0, next is 1, then 2, and so on.

iface eth0 inet dhcp
 metric 1

iface wlan0 inet static
 address 192.168.0.1
 netmask 255.255.255.0
 network 192.168.0.0
 metric 0

iface eth1 inet dhcp
 metric 2

And that's it for #2.


I think #3 happened because the devs thought WiFi is prioritized in peasant non-poweruser environments. I could only speculate since I can't find any info on this.

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.