The Raspberry Pi Zero is so compact that it does not come with a built-in network interface for initial setup. Aside from using a serial connection, another practical option is to connect it directly to a computer over USB and access it through SSH. This works by enabling Ethernet Gadget mode, which makes the Pi present itself as a USB network adapter.

Ethernet Gadget mode is essentially USB networking. A familiar example is USB tethering from an Android phone, where the phone appears to the computer as a network device and shares a connection. In the same way, most computers can automatically recognize the Pi Zero in this mode and bring up a network interface for it.

Enable USB networking in the boot files

To make this work, two files in the boot partition need to be adjusted: config.txt and cmdline.txt.

First, add the following line at the very end of config.txt on a new line to enable the USB controller overlay:

dtoverlay=dwc2

Next, open cmdline.txt, find the rootwait parameter, and append the following right after it, separated by a space. This loads the required modules during boot so the USB network device comes up automatically:

modules-load=dwc2,g_ether

To allow SSH access on first boot, create an empty file named ssh in the boot partition:

touch ssh

Connect the Pi Zero through the correct USB port

The Raspberry Pi Zero has two Micro-USB ports. One is power-only, while the other also supports OTG. For USB networking, the connection to the computer must go through the OTG-capable USB port, not the dedicated power port.

Once connected, you can try reaching the device with:

ssh [email protected]

At this point, the address is typically assigned through DHCP or resolved through the hostname. If you would rather connect using a fixed IP address, the USB network interface on the Pi needs to be configured manually.

Assign a static IP to the USB interface

You can inspect the available network interfaces with ifconfig -a. The interface used for the USB link appears as usb0, and that is the one that should be configured.

Open the network interfaces file:

sudo nano /etc/network/interfaces

Add the following block at the end of the file to give usb0 a static address:

allow-hotplug usb0
iface usb0 inet static
        address 192.168.7.2
        netmask 255.255.255.0
        network 192.168.7.0
        broadcast 192.168.7.255
        gateway 192.168.7.1

Then bring the interface down and back up, and verify it:

sudo ifdown usb0
sudo ifup usb0
ifconfig usb0

Set the computer's USB network address

The Pi Zero is acting as the USB device, so the computer on the other end also needs a matching IP configuration before the link can work properly.

Set the computer-side USB network adapter to:

IP: 192.168.7.1
Mask: 255.255.255.0
Gateway: 192.168.7.1

With that in place, the Pi can be reached directly over SSH by IP address:

ssh [email protected]

This gives the Pi Zero a simple wired management path over a single USB cable, which is especially useful during first-time setup or whenever no separate network connection is available.