The Raspberry Pi is a popular single-board computer used in many DIY projects. While typically used with a monitor, keyboard, and mouse for on-site interaction, there are instances where you may need to access your Raspberry Pi from a PC or laptop using just a USB connection.

We will walk you through the process, giving you access to your Raspberry Pi via a USB connection so that you’re able to enter commands and even access its GUI desktop.

flashing raspberry pi os on external ssd for usb boot in raspberry pi

USB Gadget Mode

The key feature facilitating direct USB connection to the Raspberry Pi on Linux is known as “USB gadget mode”. This feature is particularly useful foraccessing the Raspberry Pi via SSH; instead of using Wi-Fi, you may do it by connecting your Raspberry Pi to a laptop via USB.

With gadget mode activated, there’s no need to configure a Wi-Fi network or attach peripheral devices. The feature is available for Raspberry Pi 4 and Raspberry Pi Zero models, however, it is unclear whether it will work on the new Raspberry Pi 5.

An image showing advanced settings on raspberry os install with raspberry pi imager

Our in-depth analysis ofkey differences between Raspberry Pi 4 and Raspberry Pi 5goes deeper into what has changed with the new flagship model.

Essential Components Required for the Setup

Here’s a list of what you’ll need:

Installing the Operating System

Start by installing Raspberry Pi OS on your microSD card. This has been made really user-friendly thanks to the officialRaspberry Pi Imager tool, which simplifies microSD card flashing.

Install and launch Raspberry Pi Imager on your PC. ClickChoose OS, then selectRaspberry Pi OSfrom the list. Click onChoose SD cardand select the storage medium where you intend to flash the OS: your attached microSD card.

An image showing properties of the raspberry pi usb connection as an RNDIS/ Ethernet device

Access advanced settings by clicking the gear icon located at the bottom right. Configure the hostname asraspberrypi.local, enable SSH, and set the username topi(or whatever you want) with a password of your preference.

Click onSave. Finally, initiate the flashing process by clickingWrite.

Setting Up Your Raspberry Pi

To enable USB gadget mode on Raspberry Pi, we’ll need to make some configuration changes. While the microSD card is still connected to your computer, navigate to thebootdirectory using the file browser or command line. Within this directory, locate and edit theconfig.txtfile by appending the following line to it:

This configures the Raspberry Pi to enable the DWC2 (DesignWare Core) module, which manages the USB (Universal Serial Bus) functionality. It refers to the USB controller used in the Raspberry Pi hardware. The DWC2 module is important for enabling USB gadget mode, allowing the Raspberry Pi to act as a USB device, such as an Ethernet adapter, when connected to another computer via USB.

An image showing selection of interface options in raspberry config shell menu

If this file contains the lineotg_mode=1, comment it out using a “#”:

Theotg_mode=1setting activates USB gadget mode, but it is unnecessary whendtoverlay=dwc2is in use. This is becausedtoverlay=dwc2already manages the USB gadget mode.

An image showing selection of VNC in interface options of raspi-config

In thecmdline.txtfile, find therootwaitcommand and immediately add the following line after it.

This line adds two important modules during boot:dwc2andg_ether. Thedwc2module manages the DWC2 USB controller, essential for USB gadget mode, whileg_etherenables Ethernet gadget functionality. Together, they allow the Raspberry Pi to emulate an Ethernet adapter via USB, enabling SSH access without a direct network connection.

A reference snippet of thecmdline.txtfile should look like this:

Save the file and exit the text editor.

Additional Configuration Options for Windows Users

Some additional adjustments may be required for Windows users. To resolve the.localhost address of your Raspberry Pi, you need to installBonjour Print Servicesfor Windows.

RNDIS Driver Installation

If your Raspberry Pi appears only as a COM port in the Device Manager, you’ll need to install theRNDIS Driver.

Network Sharing Configuration

To share your network connection from the Windows host to the Raspberry Pi, navigate toControl Panel\Network and Internet\Network Connections.

Search forUSB Ethernet/RNDIS Gadgetto identify the Raspberry Pi Network Adapter. Take note of its name (you can change this if you want to). Right-click on the network you want to share and go toProperties. Then, go to theSharingtab.

SelectAllow other network users to connect through this computer’s Internet connection.

Accessing the Raspberry Pi via SSH

Once your USB connection is established, you can access the Pi via SSH. Open a terminal on your computer and enter the command below:

This will give you access to the Raspberry Pi’s command line.

Accessing the Raspberry Pi Graphical Interface with VNC

To use the graphical interface, VNC (Virtual Network Computing) is a highly recommended tool.VNC allows you to access the Raspberry Pi’s GUI remotelyfrom another computer. In your SSH session, enter the following command:

Within the Raspberry Pi Configuration menu, navigate toInterface Optionsand pressEnter.

Use the down arrow key to selectVNC. Confirm your choice by pressingEnteragain.

On your PC or laptop, install the RealVNC Viewer application. Launch the RealVNC Viewer on your computer. In the RealVNC Viewer interface, under theHostsection, input the addressraspberrypi.localand pressEnter.

After connecting, you should see the Raspberry Pi’s graphical interface displayed on your computer screen.

Troubleshooting

There are common errors that you may run into when setting up a USB connection between your Raspberry Pi and a PC.

If you encounter theCannot Currently Show the Desktoperror while attempting to connect to VNC, return to your SSH session and execute the command below:

Navigate toSystem Optionsand chooseBoot / Auto Login. Use the down arrow key to selectDesktop Autologin Desktop GUI, automatically logged in as ‘pi’ user. Then navigate toDisplay Optionsand chooseVNC session.

PressEnter, then selectFinish. You’ll be prompted to reboot; selectYes. This should resolve the error and allow you to connect to VNC successfully.

Also, ensure that your Raspberry Pi’s operating system is always up-to-date as this eliminates compatibility issues and enhances overall performance

Control the Raspberry Pi From a PC via USB

Once the Raspberry Pi board is connected to your laptop (or other PC) via USB, you may access its command line via SSH. Additionally, by enabling VNC, you can also access Raspberry Pi’s graphical interface from your laptop using the RealVNC Viewer application.

This method provides a convenient way to interact with the Raspberry Pi using only a USB connection—especially useful if you can’t access a wireless network.