Home Interests Raspberry Pi

Connect to Your Raspberry Pi Over USB Using Gadget Mode

Connect to Your Raspberry Pi Over USB
Program your Pi without a keyboard, monitor, or network connection
howchoo   (467)
August 7, 2023
8 minutes

Share

You'll Need 2

What you'll need
Interests
Howchoo is reader-supported. As an Amazon Associate, we may earn a small affiliate commission at no cost to you when you buy through our links.
Posted in these interests:
linux • 12 guides
pi • 92 guides

Program your Pi without a keyboard, monitor, or network connection

We’ve written previously about how to configure WiFi and SSH on your Raspberry Pi without having to connect any peripheral devices (headless). As long as you’re comfortable on the command line, this makes it much easier to work with your Pi.

How to Enable SSH on Raspberry Pi OS Without a Screen
The SSH protocol is disabled by default.

Now, I’ve been doing projects with Raspberry Pi’s since the first version, and I only recently found out about a Linux feature that allows you to connect to the Pi directly over USB: it’s called USB gadget mode. With “gadget mode” enabled, we don’t need to configure the WiFi network or connect any peripheral devices, we simply connect the Pi to our computer with a USB cable, and now we’ve got SSH access.

In this guide, I’ll show you how enable gadget mode and connect to your Pi via USB.

Note: I believe this feature was initially enabled for the Pi Zero, and it does not work on earlier versions of the Pi. I have, however, tested with the Raspberry Pi 4, and it works as long as you connect via the USB-C connector.

1 – Flash Raspberry Pi OS onto your SD card

First we need to flash Raspberry Pi OS onto an SD card. If you need help with this process, please follow our guide on how to install Raspberry Pi OS on your Raspberry Pi.

How to Install Raspberry Pi OS on Your Raspberry Pi
Get the new official Raspberry Pi OS on your Pi.

2 – Edit config.txt on the boot partition

With Raspberry Pi OS installed on the SD card (and the SD card still mounted on your computer), you can use Finder or a shell to navigate to the boot directory. Now edit the file called config.txt, and append the following:

dtoverlay=dwc2

Save and exit.

3 – Enable SSH

We can SSH by simply adding an empty file called ssh directly in the boot directory.

On the command line, you can navigate to the boot directory and run:

touch ssh

Note: The file must be called “ssh” not “SSH” or “ssh.txt” or anything other than “ssh”.

4 – Edit cmdline.txt

Now, edit the file called cmdline.txt. Look for rootwait, and add modules-load=dwc2,g_ether immediately after.

Note the formatting of cmdline.txt is very strict. Commands are separated by spaces, and newlines are not allowed.

Here’s an example of what my cmdline.txt file looks like, but you should not copy this. Simply add the required command mentioned above.

console=serial0,115200 console=tty1 root=PARTUUID=6c586e13-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether quiet init=/usr/lib/raspi-config/init_resize.sh

Save and exit.

5 – Connect over USB and boot the Pi

That’s it for configuration! Now you can eject the SD card, and insert it into the the Pi. Using a USB cable, connect to the Raspberry Pi from your computer.

After the Pi boots up (this will take a while initially), the Pi should appear as a USB Ethernet device, and you can SSH into it using:

ssh [email protected]

Enjoy!

NEXT UP

How to Run a Minecraft Server on the Raspberry Pi

A whole world trapped inside your Pi.
howchoo   (467)
December 7, 2023

There are several ways to go about running a Minecraft server on the Raspberry Pi. In this guide, I’ll cover how to install Nukkit—a cross-platform Minecraft server that’s super easy to set up on the Raspberry Pi. This server should work with PCs, consoles, and tablets running Minecraft 1.14. I’ll be using a Raspberry Pi

Continue Reading

howchoo

 467 guides

Introducing Howchoo, an enigmatic author whose unique pen name reflects their boundless curiosity and limitless creativity. Mysterious and multifaceted, Howchoo has emerged as a captivating storyteller, leaving readers mesmerized by the uncharted realms they craft with their words. With an insatiable appetite for knowledge and a love for exploration, Howchoo's writing transcends conventional genres, blurring the lines between fantasy, science fiction, and the surreal. Their narratives are a kaleidoscope of ideas, weaving together intricate plots, unforgettable characters, and thought-provoking themes that challenge the boundaries of imagination.

Discover interesting things!

Explore Howchoo's most popular interests.

Explore