2

The Raspberry Pi Zero W is connected to the internet with wlan0 and it needs to share that internet with the computer via USB. The Raspberry Pi Zero W also needs internet access. I have gadget mode already set up.

So when I plug it in the computer recognizes as Ethernet but it doesn't connect properly.

The Raspberry Pi Zero W has internet and is running a nginx server.

Please comment if you need more information.

pi@raspberrypi:~ $ ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 24  bytes 2158 (2.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24  bytes 2158 (2.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.7.1 netmask 255.255.255.0 broadcast 192.168.7.255 inet6 fe80::170:33e3:ade5:d88a prefixlen 64 scopeid 0x20<link> ether c2:d0:fa:7a:ff:38 txqueuelen 1000 (Ethernet) RX packets 1 bytes 96 (96.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 67 bytes 8888 (8.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.27 netmask 255.255.255.0 broadcast 192.168.0.255 inet6 fe80::5982:2aea:a56:b63a prefixlen 64 scopeid 0x20<link> inet6 fd00::746e:b5ce:9dcd:13a0 prefixlen 64 scopeid 0x0<global> ether b8:27:eb:e0:c9:38 txqueuelen 1000 (Ethernet) RX packets 141 bytes 14473 (14.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 150 bytes 22053 (21.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

So what I want is:

pi0w with internet --> usb --> computer with no wifi card but now it has internet over wifi from the pi0w
allancoding
  • 123
  • 1
  • 6

2 Answers2

2

Searching around here and elsewhere online I had a hard time finding a discussion of the Pi Zero ethernet gadget mode and port forwarding, which seems very strange -- although it could be that most people are using OTG gadget mode just as a way to wire the Zero into another computer, since it doesn't have an ethernet jack, and don't need to share the Pi's internet. Also, the original Zero had no onboard wifi, and use of the ethernet gadget dates back to that.

Port forwarding simply means forwarding various (or all) connections from one interface to another. On linux this is generally done with the kernel's netfilter features, commonly used to configure a firewall. It can be controlled via iptables. First forwarding needs to be enabled:

sudo sysctl -w net.ipv4.ip_forward=1

Then set some iptables rules:

sudo iptables -I FORWARD -i usb0 -o wlan0 -j ACCEPT
sudo iptables -I FORWARD -i wlan0 -o usb0 -j ACCEPT
sudo iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE

Hopefully, there isn't some network daemon running that interferes with this.

You'll probably have to set DNS servers on the connected computer; try ping 8.8.8.8 and ping google.com from there; if the former works and the latter does not that's the issue. You could then use google's public DNS servers (8.8.8.8, 8.8.4.4, etc.), or try the ones the Pi is using which should be listed in /etc/resolv.conf.

goldilocks
  • 60,325
  • 17
  • 117
  • 234
1

Did you tried this? Seems it is what you need. https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/ethernet-gadget

dingo27mobile
  • 119
  • 2
  • 11