TODO: Create a standalone document by adding step-by-step procedures.
How to connect to the Internet over USB from the Raspberry Pi Zero
This How-To document explains how I managed to get an Internet connection to the Pi Zero over USB using a Windows 7 host PC, a Linux server, and a USB data cable. If the Internet connection sharing (ICS) option works without any issues on the host PC or a SSH server can be installed on the same PC, the Linux server is not required.
1 Windows 7
1.1 Create an SSH connection to the Pi Zero over USB
I was able to connect to the Raspberry Pi Zero using only a host PC and a USB data cable by following this article. I had to install the RNDIS/Ethernet Gadget driver as explained in this document to recognize the Pi Zero as an ethernet device. Not only that, I needed to install the Bonjour SDK for Windows to discover the little computer on the local network and connect to it by its name, not by a randomly assigned IP address. So far, so good.
1.3 Connect to the Pi from the proxy server
Now, if the proxy server is different from the host PC, to which the Pi Zero is connected via USB, there is no way to connect to the Raspberry from the proxy server because only the host PC can see it. For example, the Pi Zero (P) is connected to the host PC (H) and uses the proxy server (S) for outgoing traffic. I can SSH into P from H because P serves as a USB ethernet device for H, but I cannot connect to P from S because S has no physical connection to P and only serves as a SOCKS5 proxy server. To address this issue, I initiated two additional SSH tunnels:
- Remote port forwarding from S to H created on H
- Local port forwarding from H to P created on H
In this way, I was able to SSH into P from S through the two SSH tunnels above.
2 Slackware using Avahi
2.1 Create an SSH connection to the Pi Zero over USB
On the host,
- Install avahi with the
–enable-autoipd
configure option. The avahi package from SlackBuild doesn’t include this configure option. - Install nss-mdns either manually or using sbopkg:
sbopkg -i nss-mdns
- Copy /etc/nsswitch.conf-mdns to /etc/nsswitch.conf:
cp /etc/nsswitch.conf-mdns /etc/nsswitch.conf
- Start the avahi daemon:
/etc/rc.d/rc.avahidaemon start
- Assign an IP address to the USB device:
avahi-autoipd -D usb0
- SSH into the Pi:
ssh pi@raspberrypi.local
3 Slackware using a static IP address
On the Pi,
- Once a connection to the Pi using Avahi is established, SSH into the Pi.
- Disable the avahi service because it’s not needed for a static IP address:
sudo systemctl disable avahi-daemon
- Add the following lines to /etc/network/interfaces:
allow-hotplug usb0 iface usb0 inet static address 192.168.2.2 netmask 255.255.255.0 network 192.168.2.0 broadcast 192.168.2.255 gateway 192.168.2.1 dns-nameservers 8.8.8.8
- Reboot:
sudo reboot
On the host,
- Assign an IP address to the usb0 device:
ifconfig usb0 192.168.2.1
- Allow IP forwarding on the host:
echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
- SSH into the Pi:
ssh pi@192.168.2.2
If you want to connect to the Pi in the 192.168.2.0 subnet from a computer in the 192.168.1.0 subnet, run the following command on the computer from which you want to connect to the Pi:
route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.2
4 More ideas
It would be great if the Pi Zero appears as a mass storage on the host PC and contains programs required to create an Internet connection for itself. Maybe, adding the mass storage driver in Raspbian should do it.
5 References
- Raspberry Pi Zero – Programming over USB! (Part 2)
- Setting Up Windows USB Ethernet Networking
- Bonjour SDK for Windows to discover the Pi
- Share the internet with the host
- Fix the SOCKS DNS bug in tsocks
- Setting Up Linux USB Ethernet Networking
- Hostname resolution
- Ethernet over USB
- Forwarding traffic between 2 interfaces
- Raspberry Pi Zero USB/Ethernet Gadget Tutorial