Wifi to Ethernet with Rpi

My internet went out the other day, and while I could just use my laptop over a mobile hotspot, I kinda just felt like using my desktop.  So, since my desktop doesn’t have wifi, and I wanted to take the challenge, I decided to try getting an Ethernet connection over the 4G hotspot.  The idea was this, essentially extending a wifi network to Ethernet:

First things first, boot the pi and enable ip forwarding:

echo '1' > /proc/sys/net/ipv4/ip_forward

Next was to connect the pi to the 4G hotspot.  This was just a matter of using wpa_supplicant:

network={
ssid="4G Hotspot"
psk="password"
}

Next was just to make sure the default route was over the wifi interface using either the route -n or ip route command.  Now we just need to allow traffic to be forwarded from the ethernet interface to the wifi interface.  We need to add this iptables rule (it allows the other devices to use the internet through the Rpi):

iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

Read more about MASQUERADE here

Voila!  Now just make sure the pi is on my LAN network, and point my desktop’s gateway to my pi’s ip address (under the ipv4 network interface settings in Windows), and it has internet access over the 4G hotspot!   Now I just have to make sure windows doesn’t go off and download updates…

If you wanted to rebroadcast this internet using another wifi interface, that’s totally possible (just like a wifi relay), but it takes a bit more work.  You would basically do everything the same (set up the iptables rule over the wifi interface connected to the internet side, etc) but you also have to set up a DHCP server on the wifi hotspot to assign addresses and a gateway address.  I use isc-dhcp-server, and I encourage you to try setting it up.

You could even take this a step further and run openvpn on the pi to have all traffic routed over the VPN as well (running openvpn should automatically modify the default route).  There are lots of cool applications of this, for example extending a LAN over a wireless gap to be used by Ethernet devices.  Comment your thoughts!

Leave a Reply

Your email address will not be published. Required fields are marked *