The following lets you connect a Raspberry Pi 5 to a laptop via USB-C cable and have an Ethernet speedy connection via the same cable. One cable for both power and data flow, without hiccups of WiFi, and works perfectly with Visual Studio Code remote SSH.
Tested on 2025-05-29.
NOTE: Currently I cannot get it to work on Windows, for it stubbornly keeps mounting the Raspberry as a COM device.
-
Be sure to enable the SSH server on
raspi-config
. Also, you should disable the GUI on boot. -
Edit the file
/boot/firmware/cmdline.txt
and add the following just AFTERrootwait
:
modules-load=dwc2,g_ether
- Edit the file
/boot/firmware/config.txt
and confirm it has an UNCOMMENTED line near the end that isotg_mode=1
. Then add below the line[all]
(it MUST to be after the[all]
line for it to work properly) the following:
dtoverlay=dwc2
- Now we need to add our connection name by running the following command:
sudo nmcli con add type ethernet con-name ethernet-usb0
- Now edit the file we just created, named
/etc/NetworkManager/system-connections/ethernet-usb0.nmconnection
. You will be adding the lines forautoconnect
andinterface-name
, and then modifying the line withmethod=
to changeauto
toshared
:
[connection]
id=ethernet-usb0
uuid=<random group of characters here>
type=ethernet
autoconnect=true
interface-name=usb0
[ethernet]
[ipv4]
method=shared
[ipv6]
addr-gen-mode=default
method=auto
[proxy]
- Create yet another new file (again with vi or nano) at
/usr/local/sbin/usb-gadget.sh
with the following contents:
#!/bin/bash
nmcli con up ethernet-usb0
- Make this new file executable:
sudo chmod a+rx /usr/local/sbin/usb-gadget.sh
- Create yet another file named
/lib/systemd/system/usbgadget.service
with the following text:
[Unit]
Description=My USB gadget
After=NetworkManager.service
Wants=NetworkManager.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/sbin/usb-gadget.sh
[Install]
WantedBy=sysinit.target
- Run the following command to enable this new service:
sudo systemctl enable usbgadget.service
- Reboot your pi5 so all your changes can be put to use. Run the following command to reboot it:
sudo shutdown -r now
- On your laptop, you should now have a new Ethernet interface of type
RNDIS/Ethernet Gadget
. Look at its IP. Suppose that the IP assigned to this interface is10.24.0.138
, the Raspi address should be10.24.0.1
, so you can justssh <user>@10.24.0.1
.