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:
- Is there a way to automatize this process so I won't have to manually do it every time I turn on the Raspberry ?
- 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?
- 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