# raspberry pi zero - connecting over usb (RNDIS Gadget)

## optiluca

Hi all.  I'm really struggling to connect to a raspberry pi zero over usb (whereby the usb connection gets treated like an ethernet connection, which I can then bridge to my internet connection if needed).

The setup should be easy:

http://blog.gbaman.info/?p=791

but it's not turning out that way!

I've added the kernel modules, and indeed when I plug the raspberry pi I now get.

```
luca@optipad ~ $ ifconfig

enp0s25: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500

        ether f0:de:f1:0e:7f:a9  txqueuelen 1000  (Ethernet)

        RX packets 0  bytes 0 (0.0 B)

        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

        device interrupt 20  memory 0xf2600000-f2620000  

enp0s26u1u2i1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        ether 32:b5:75:40:ee:91  txqueuelen 1000  (Ethernet)

        RX packets 67  bytes 17728 (17.3 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 305  bytes 66049 (64.5 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

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 1  (Local Loopback)

        RX packets 1749  bytes 139740 (136.4 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 1749  bytes 139740 (136.4 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 192.168.1.6  netmask 255.255.255.0  broadcast 192.168.1.255

        inet6 fe80::224:d7ff:fe17:f6b0  prefixlen 64  scopeid 0x20<link>

        ether 00:24:d7:17:f6:b0  txqueuelen 1000  (Ethernet)

        RX packets 12174  bytes 9276656 (8.8 MiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 10863  bytes 1626876 (1.5 MiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

```

enp0s26u1u2i1 is a new entry - the connection to my raspberry pi.  Now the documentation suggests that I shold be able to simply

```

ssh raspberrypi.local
```

 - but clearly that doesn't work.

The first issue is that the device doesn't have an ip address.  Network manager refuses to negotiate a DHCP connection, and indeed if I run

```
dhclient -v enp0s26u1u2i1

Internet Systems Consortium DHCP Client 4.3.4 Gentoo-r0

Copyright 2004-2016 Internet Systems Consortium.

All rights reserved.

For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/enp0s26u1u2i1/32:b5:75:40:ee:91

Sending on   LPF/enp0s26u1u2i1/32:b5:75:40:ee:91

Sending on   Socket/fallback

DHCPDISCOVER on enp0s26u1u2i1 to 255.255.255.255 port 67 interval 5

DHCPDISCOVER on enp0s26u1u2i1 to 255.255.255.255 port 67 interval 13

DHCPDISCOVER on enp0s26u1u2i1 to 255.255.255.255 port 67 interval 8

DHCPDISCOVER on enp0s26u1u2i1 to 255.255.255.255 port 67 interval 9

DHCPDISCOVER on enp0s26u1u2i1 to 255.255.255.255 port 67 interval 13

DHCPDISCOVER on enp0s26u1u2i1 to 255.255.255.255 port 67 interval 13

No DHCPOFFERS received.

No working leases in persistent database - sleeping.

```

However, this works:

```
dhcpcd enp0s26u1u2i1

unknown option: optipad

unknown option: optipad

enp0s26u1u2i1: adding address fe80::2a26:6ff0:14b1:644f

DUID 00:01:00:01:19:b1:d7:7f:f0:de:f1:0e:7f:a9

enp0s26u1u2i1: IAID 75:40:ee:91

enp0s26u1u2i1: soliciting a DHCP lease

enp0s26u1u2i1: soliciting an IPv6 router

enp0s26u1u2i1: probing for an IPv4LL address

enp0s26u1u2i1: using IPv4LL address 169.254.191.89

enp0s26u1u2i1: adding route to 169.254.0.0/16

forked to background, child pid 4247

```

At this point I ran:

```
arp-scan --interface=enp0s26u1u2 --localnet

```

And got a hit at 169.254.252.49!  And indeed, if I

```
ssh pi@169.254.252.49
```

I gain access to my raspberry pi.  However, running dhcpcd makes me lose internet access on my wireless interface, and I don't have internet access from the raspberry pi.  Ideally I'd get the connection in networkmanager, and set up a bridge in there - any ideas as to why dhclient and dhcpcd would behave differently?  Why am I losing internet access?  How do I bridge these connections while running networkmanager??  Any help much appreciated!  :Smile: 

Thanks,

Luca

----------

## Ant P.

If you have a properly configured mDNS/Avahi setup (which it's implied you are, since you said you're using .local addresses) then there's no need for IPv4 to ssh; the v6 link-local addresses should work just the same. It sounds like something else is going wrong.

----------

## optiluca

Thanks for the pointer  :Smile:   After setting /etc/nsswitch.conf as per https://wiki.gentoo.org/wiki/Avahi (IPV6 settings) - I can get a connection:

```

luca@optipad ~ $ ssh pi@raspberrypi.local

pi@raspberrypi.local's password: 

The programs included with the Debian GNU/Linux system are free software;

the exact distribution terms for each program are described in the

individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent

permitted by applicable law.

Last login: Thu Jun 30 06:17:12 2016
```

This is very sporadic and cuts out almost immediately though - NetworkManager keeps trying to connect to the device and is presumably breaking things it seems.  I tried adding an exclusion rule for NetworkManager not to manage this device:

/etc/NetworkManager.conf

```
[keyfile]

hostname=optipad

unmanaged-devices=mac:ba:f8:02:6d:78:25;mac:32:b5:75:40:ee:91

```

b

But the mac address keeps changing!  So does the device name - it's currently on enp0s26u1u2 but it was enp0s26u1 earlier...

This is quite miserable - in windows I can just connect with no issue which is a little ironic considering we're dealing with a linux box!

----------

## Ant P.

You'll probably have to force it to not use udev device names. The kernel ought to name it "usb0" or similar.

----------

## pac1

I too am struggling with the Raspberry Pi Zero.  I want to connect to the Pi via SSH and have a working internet connection through the usb-ethernet connection.

I'd like to come up with something comprehensive that address the various issues encountered and provides a smooth path for anyone trying this for the first time.

Issues encountered: 

- Local Only IP Addresses can't be bridged

- Breaking the main Network Manager connection

- Repeated connect and disconnect messages from Network Manager

- Strange behaviour by certain NICs

- Changing IP and MAC addresses with each reboot

- Using DHCP or Using Static Addresses

- Timing the loading and use of g_ether module on the PI so we don't end up with a random MAC address

- Routing packets from the USB Ethernet connection to the WAN.

There are several individuals in a thread on the Raspberry Pi forum, working on this from various perspectives and with varying skillsets.

You can find a thread of posts on the Raspberry Pi forums under the title 

Need Help Improving Zero USB Ethernet Gadget setup / HOWTO

Here's a link to the post: https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=168285

I'm working on a bash script, others prefer python.  There's always more than one way to do 

Here's my git project:  https://github.com/callahanp/raspberrypi-zero-usb-ssh-internet it is currently incomplete.

There are others referred to in the Raspberry Pi Forum Thread.

I don't think there's a comprehensive answer yet, but there are people who have been successful configuring the pi and the connection to achieve a fully connectable Pi-Zero using the following techniques:

- Starting the g_ether module only after the main computer has fully booted and connected to the internet.  This has been done by  loading g_ether after a timed delay.

- Assigning static mac addresses for both ends of the USB  connection.

- Using DHCP to assign ip addresses to both ends of the USB connection

- Using a bridge or IP Masqerade to get the pi on the internet. Note that Local only connections apparently can't be used.

- ssh to the pi is done using the pi's IP address.  We can probably configure things to use the pi's hostname with some routing magic.

I think there's several scripts involved:

1.  Configure the pi

2.  Configure the host

3.  Load the module on the pi and get things going

4.  Do anything on the host that needs doing after the pi starts up.

----------

