Wednesday, April 25, 2018

Sharing your laptop's internet connection with your Raspberry Pi


In the previous post, we set up a headless Raspberry Pi and connected to it via a PuTTY SSH terminal session running over the local NIC. In order to update and install software onto the RPi, we will need to give it access to the Internet and all of its code respositories. For example, you will need this when you run 'sudo raspi-config' and need to select Option 8 to update to the latest script version:


Since it is the Windows laptop that has access to the global network, we will enable Internet Connection Sharing between the global interface and the local NIC so the RPi can route through the laptop to the web. I found a couple of references on how to do this, but encountered differences in behavior on Windows 7 and Windows 10.

https://raspberrypi.stackexchange.com/questions/23627/how-to-share-my-internet-connection-on-a-windows-computer-with-a-raspberry-pi

https://raspberrypi.stackexchange.com/questions/63153/connect-pi-to-windows-10-with-internet-connection-sharing 

 To get it working on the Windows 10 laptop:
  • Go to the NIC that is connected to the outside world (in my case, this was my laptop's WiFi adapter) and enable Internet Connection Sharing:
  • When ICS is enabled, the wired NIC will automatically be assigned an IP address of '192.168.137.1' with subnet '255.255.255.0'.
  • Use PuTTY to login to the RPi at the IP address you found using Wireshark and edit the file /etc/dhcpcd.conf  to set a new static IP address and DNS server:
interface eth0
static ip_address=192.168.137.2
static routers=192.168.137.1
static domain_name_servers=192.168.137.1 8.8.8.8
  • Reboot the RPi and login over SSH again using the new static IP address (192.168.137.2). You should be able to run 'sudo apt-get update' to download RPi updates from the global repository:

    
    
Next steps:

3 comments:

  1. Since the Raspberry Pi used is a Model 3 B+. the question came up: why not just connect over WiFi to the Internet?
    In my work setting, the only open WiFi access is via a guest network that requires you acknowledge terms of service via a web browser before you can connect. Since I only have an SSH terminal session available to me, I can't do that. Instead I have joined the network using my Windows laptop and then have share that connection with the RPi.

    ReplyDelete
  2. On some occasions I have not been able to SSH into the RPi after rebooting my laptop. If I look at the Windows Control Panel -> Network and Internet -> Network Connections, I see that the wired ethernet reports that the cable is disconnected. Also, if I run the 'ipconfig /all' command from a CMD prompt window, the Ethernet interface reports 'Media disconnected'. This can be fixed by disabling and re-enabling the interface from the 'Network Connections' path described above.

    ReplyDelete
  3. Also, if I am unable to get to the Internet from the RPi, I can fix it by disabling and re-enabling ICS on the laptop.

    ReplyDelete