4

I am trying to use my Raspberry Pi Zero together with my Raspberry Pi 2 and I want to share network access from my Raspberry Pi 2 to my Raspberry Pi Zero. I can easily create a local network connection between my Raspberry Pis, but I have a problem sharing network access.

First of I followed this guide, Adafruit - Turning your Raspberry PI Zero into a USB Gadget - Ethernet Gadget, to turn my Raspberry Pi Zero into a Ethernet gadget. Then I added this to /etc/network/interfaces:

allow-hotplug usb0
iface usb0 inet static
        address 10.0.1.2
        netmask 255.255.255.0
        gateway 10.0.0.1

I configured usb0 on my Raspberry Pi 2 like this:

allow-hotplug usb0
iface usb0 inet static
        address 10.0.1.1
        netmask 255.255.255.0
        gateway 10.0.0.1

I could ssh into my Raspberry Pi Zero with ssh pi@10.0.1.2 but my next goal was to share network access, so I could run stuff like ping google.com on my Raspberry Pi Zero. I found this guide, Configuring Raspberry Pi as a Wireless-to-Wired Ethernet Island, which explains how to forward network access from wlan0 to eth0 and I tried doing the same with usb0.

I added pre-up iptables-restore < /etc/network/iptables.conf to /etc/network/interfaces. This is the content of my iptables.conf file:

# Generated by iptables-save v1.4.21 on Tue Feb 16 16:41:38 2016
*mangle
:PREROUTING ACCEPT [472:58950]
:INPUT ACCEPT [324:32281]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [30694:35635555]
:POSTROUTING ACCEPT [30694:35635555]
COMMIT
# Completed on Tue Feb 16 16:41:38 2016
# Generated by iptables-save v1.4.21 on Tue Feb 16 16:41:38 2016
*filter
:INPUT ACCEPT [57:4180]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2194:2536976]
-A FORWARD -i usb0 -j ACCEPT
-A FORWARD -i wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
COMMIT
# Completed on Tue Feb 16 16:41:38 2016
# Generated by iptables-save v1.4.21 on Tue Feb 16 16:41:38 2016
*nat
:PREROUTING ACCEPT [6:821]
:INPUT ACCEPT [1:242]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -o wlan0 -j SNAT --to-source 10.0.0.115
COMMIT
# Completed on Tue Feb 16 16:41:38 2016

I enabled ip forwarding in the kernel by uncommenting this line in /etc/sysctl.conf

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

Finally I executed sysctl --system.


Unfortunately the network is still unreachable on my Raspberry Pi Zero after rebooting both my Raspberry Pi 2 and Raspberry Pi Zero. This is the output of route on my Raspberry Pi 2:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         10.0.0.1        0.0.0.0         UG    0      0        0 wlan0
10.0.0.0        *               255.255.255.0   U     0      0        0 wlan0
10.0.1.0        *               255.255.255.0   U     0      0        0 usb0
Greenonline
  • 2,969
  • 5
  • 27
  • 38
Linus
  • 529
  • 1
  • 7
  • 17

1 Answers1

3

As it turns out, I was really close to getting it right. Thanks to the awesome guys over at the raspberry pi forum, I got it working.

What I did was changing the gateway on the pi zero to the ip of the pi 2:

allow-hotplug usb0
iface usb0 inet static
        address 10.0.1.2
        netmask 255.255.255.0
        gateway 10.0.1.1 #<-- I changed this

And removed the gateway from the usb0 interface on the pi 2:

allow-hotplug usb0
iface usb0 inet static
        address 10.0.1.1
        netmask 255.255.255.0
        #gateway 10.0.0.1 <-- I removed this

pre-up iptables-restore < /etc/network/iptables.conf

After rebooting everything, I could ping any ip address within my local network. I had to add dns-nameservers 10.0.1.1 8.8.8.8 8.8.4.4 to the usb0 interface on the pi zero to be able to ping google.com for instance.

Linus
  • 529
  • 1
  • 7
  • 17