1

I've been trying to configure my Pi-3b+ and Pi-Zero to share a network connection for about a week now without success. Can anyone tell me how to configure the two pi's to enable the following network?

                          Pi-3B+
          wired   ┌─────bridge─────┐   wired            wan
Pi-Zero <-------> |(usb0)  br0 (eth0)| <-------> router <-----> internet
       \                    |                    / DHCP-server
      (dhcp)              (dhcp)            192.168.0.1

Pi-Zero Current Configuration

The Pi Zero is properly configured as a USB OTG Gadget using this guide and I've been able to SSH into it from my Windows PC when plugged into the Windows PC. I've edited it's dhcpcd.conf file to give it a static IP of 192.168.7.2 (this would need to change, I assume).

Pi-3B+ Current Configuration

The Pi-3B+ is currently set up to use DHCP and gets it's IP from the router. The router uses the eth0 MAC address to set this IP. The Pi can see the usb0 interface and eth0 interfaces just fine:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.27  netmask 255.255.255.0  broadcast 192.168.0.255
        ether ab:cd:ef:gh:ij:kl  txqueuelen 1000  (Ethernet)
        RX packets 1334182  bytes 686391055 (654.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1441095  bytes 1126857152 (1.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether ab:cd:ef:gh:ij:kl  txqueuelen 1000  (Ethernet)
        RX packets 26756  bytes 752390 (734.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Attempted Steps I've tried to follow this guide as well and here's where I ran into problems. So far I've run the following commands:

brctl addbr br0
brctl addif br0 usb0 <<<<<<< This errors out and says it is not possible to add this interface
brctl addif br0 eth0 <<<<<<<< This kills my SSH connection entirely

My hunch is that the bridge has it's own MAC address and the router doesn't recognize it so it assigns a new IP. However I can't seem to find the Pi at all on the network once I run this command.

Any advice or suggestions on how to properly do this? Do I need to configure /etc/network/interfaces first? I'm very wary of this setup process since these Pi's are headless and a real pain to recover from any mis-configuration that knocks out SSH. Thanks!

Edited again Ok so I realized part of the problem was with a conflict between dhcpcd and the /etc/network/interfaces file

Here's my new configuration which boots but still doesn't assign an IP to the pi zero

/etc/network/interfaces:

auto br0
iface br0 inet manual
       bridge_ports usb0 eth0

/etc/dhcpcd.conf:

...

denyinterfaces eth0 usb0

profile static_br0
static ip_address=192.168.0.27/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 1.1.1.1 8.8.8.8

interface br0
fallback static_br0
0

You have decided to use routing instead of a bridge because the bridge does not accept Gadget mode interface usb0. Here I will give you an example to setup routing. To simplify things I will use systemd-networkd. Because I don't have a Pi Zero I couldn't test it but I have emulated it with a wired ethernet connection to my laptop. So I'm confident that it also works with usb0.

Example setup:

          wired                     wired           wan
Pi-Zero <───────> (usb0)RPi(eth0) <───────> router <───> INTERNET
      \           /             \
    (dhcp)   192.168.4.1       (dhcp)

Be sure that the subnet for the Pi-Zero (here 192.168.4.0/24) is different from that of your router.

Switch over to systemd-networkd with:

# disable classic networking
rpi ~$ sudo -Es
rpi ~# systemctl mask networking.service
rpi ~# systemctl mask dhcpcd.service
rpi ~# mv /etc/network/interfaces /etc/network/interfaces~
rpi ~# sed -i '1i resolvconf=NO' /etc/resolvconf.conf

# enable systemd-networkd
rpi ~# systemctl enable systemd-networkd.service
rpi ~# systemctl enable systemd-resolved.service
rpi ~# ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

Now create these two files:

rpi ~# cat > /etc/systemd/network/04-eth0.network <<EOF
[Match]
Name=eth0

[Network]
DHCP=yes
EOF

rpi ~# cat > /etc/systemd/network/06-usb0.network <<EOF
[Match]
Name=usb0

[Network]
Address=192.168.4.1/24
IPForward=yes
DHCPServer=yes

[DHCPServer]
DNS=84.200.69.80 84.200.70.40
EOF

Extend the network service with:

rpi ~# systemctl edit systemd-networkd.service

In the empty editor insert these statements, save them and quit the editor:

[Service]
ExecStartPre=/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
ExecStopPost=/sbin/iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

Reboot.
That's it.

1

It is normal that you lose the ssh connection when you add the interface, you used for ssh, to the bridge. When the interface is added to the bridge it looses all properties to work as normal ip interface. It is only used for the bridge. You have to ensure that the bridge interface br0 itself gets an ip address before adding eth0 to the bridge. Interface br0 is used instead of eth0 then.

The other error, that you cannot add the usb0 interface to the bridge, seems to be the truth. I do not have a Pi-Zero so I cannot confirm this directly but it is very likely that it doesn't work. This emulation of an ethernet connection with an USB connection is a special mode called USB Gadget mode and isn't a real ethernet interface usable on the link layer of a bridge. You may consider to use routing instead of a bridge.

As you see it is very annoying to configure and modify network interfaces only being headless. You may also consider to use the serial debug interface. How to do it you can look at How can I diagnose a headless Raspberry Pi Zero W that hangs?.

If possible you can also use the WiFi interface of the RasPi 3B+ to connect with ssh. Then you are free to configure the other interfaces without loosing the connection if you don't touch wlan0.

Update:
You asked in a comment: "Does brctl work on the link layer in this case?". brctl does not work on the link layer. It is only the management tool. It is the bridge br0 that works on OSI layer 2 (data link layer). That means the bridge has nothing to do with ip addresses. It only uses mac addresses. That's why ip addresses on bridge member interfaces eth0 and usb0 (if possible) does not make sense.

  • Thanks for the info! I was able to get the bridge configured by connecting a keyboard and monitor and configuring the bridge without any internet connection. After I had the bridge config set up properly I was able to connect to router again and DHCP ran properly. I can now ssh/ping/etc my Pi 3B+ – Nick Mar 26 at 13:43
  • One question though is why USB gadget mode wouldn't work? Does brctl work on the link layer in this case? I can see from ip addr show that packets are moving back and forth but there's no IP assigned to the USB interface (indicating the DHCP requests aren't being forwarded)... – Nick Mar 26 at 13:45
  • Ok so I just edited my post with my updated config. The bridge gets an IP now although I can't quite tell if it's the fallback config or DHCP. The USB device still has no connection. I originally didn't want to go the routing route because of IP tables and not knowing how to make ufw work with custom rules like that – Nick Mar 26 at 21:22
  • @Nick I have updated the answer. – Ingo Mar 27 at 0:19
  • thanks for the information. I've torn down the bridge and am attempting to route the packets using iptables. So far I've added the following two rules: iptables -A FORWARD -i usb0 -o eth0 -j ACCEPT iptables -A FORWARD -i eth0 -o usb0 -j ACCEPT And enabled IPv4 forwarding in sysctl. Still not working but I think I need to reconfigure the Pi Zeros so they send data to the right address. I might also need to enable NAT or add a static route since I can't get data moving now. – Nick Mar 27 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.