This guide explains how to set up a Raspberry Pi for headless operation over USB, enabling SSH access without the need for an external monitor, keyboard, or network connection.
- A Raspberry Pi (Zero, Zero W, or other models supporting USB gadget mode)
- A microSD card with Raspberry Pi OS installed
- A computer with an available USB port
Edit the boot configuration file on the SD card:
sudo nano /boot/cmdline.txtAdd the following before rootwait:
modules-load=dwc2,g_ether
Save and exit.
Edit the Raspberry Pi configuration file:
sudo nano /boot/config.txtAdd the following line at the end:
dtoverlay=dwc2,dr_mode=peripheral
Save and exit.
Edit the network interfaces file:
sudo nano /etc/network/interfacesAdd the following:
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
Save and exit.
To enable SSH, create an empty file named ssh in the /boot directory:
touch /boot/ssh- Connect the Raspberry Pi to your computer using a USB cable.
- Wait for the Pi to boot up.
- SSH into the Raspberry Pi using:
ssh pi@192.168.7.2(Default password: raspberry)
- Ensure your computer recognizes the Raspberry Pi as a network device.
- Try restarting both the Raspberry Pi and your computer.
- Use
ifconfigorip ato check the USB interface status.
- Raspberry Pi USB Gadget Mode Documentation
- Official Raspberry Pi OS Setup Guide
Enjoy your headless Raspberry Pi setup!