Configure Raspberry Pi for headless use with USB serial console

30/11/2017 - 15:21
Average: 4 (1 vote)

The Zero, A and A+ are a great cheap little device, but connecting to it without any screen and keyboard can be a bit of a pain.
Thankfully it is possible to configure it to use USB gadget mode to provide a serial console over the USB connector (or even a USB ethernet connection if needed).
Unfortunately it won't work as system console as the gadget driver is loaded as a module, but we can use it for serial console.

This tutorial will show how to do that on plain SD card under Linux. You can also modify the SD card image directly in case you need to make the modification for multiple SD cards.

Preparse a Raspbian SD card

First, prepare a SD card of raspbian as your normally would, I will not go into the details here as there are plenty of sources for that. You can use full or lite version.

Mount the SD card partitions

When done preparing the SD card, remove the card and re-insert so the system will re-read the new partition scheme of the SD card.

Then mount the filesystems, this depend a bit on your environment, on Ubuntu and derivates like Linux Mint the root and boot partitions will be mounted automatically.

If they are not, check in the dmesg the devices nodes that the SD card shows up as.

For example if the card is available as a mmc device nodes the partitions will show up as something looking like this:

mmcblk0: mmc0:59b4 USDU1 15.0 GiB
  mmcblk0: p1 p2

brw-rw---- 1 root disk 179, 0 Nov 16 11:17 /dev/mmcblk0
brw-rw---- 1 root disk 179, 1 Nov 16 11:17 /dev/mmcblk0p1
brw-rw---- 1 root disk 179, 2 Nov 16 11:17 /dev/mmcblk0p2

p1 is the /boot partition. We will use BOOT to point to the mount point of this partition below.
p2 is the / partition. We will use ROOTFS to point to the mount point of this partition below.

If you are using a SD to USB adapter they will most likey show up as /dev/sdb1 and /dev/sdb2 (or sdc, sde, etc. Depending on amount of drives in your system)

Mount these as you normally would in case your system didn't auto mount them. Location is up to you.

Optional, modify Raspbian SD image in place

You can also modify the image directly, before writing it to a SD card. In this case, we need to configure a loopback device and mount the image partitions.

First, configure a loopback device with losetup for the Raspbian image you are using:

sudo losetup -f --show -P 2017-09-07-raspbian-stretch-lite.img

Note the loopback device node the command prints out, usually something close to /dev/loop0. Now mount the loopback device partitions, /dev/loop0p1 (BOOT) and /dev/loop0p2 (ROOTFS).

Now make the below modifications inside these moutpoints, unmount, detach the image from the loopback device (sudo losetup -d /dev/loop0) and then write the image to your SD card.

Configure Raspbian image

Next we need to adjust the configuration of the image, enable USB OTG driver, enable USB serial gadget driver and last enable a console on the USB serial device so we can login.

USB Driver setting

We need to use the dwc2 USB controller driver, luckily on the Pi Zero dwc2 is the default driver so we don't need to change anything for that. Should be default according to documentation, but it didn't work, so:

Enable dwc2 driver in BOOT/config.txt, add the line at the end. At the same time you can enable/disable anything extra you might need like SPI or I2C.


Modify default cmdline.txt to enable serial gadget driver

We need to enable loading of the serial USB gadget driver.

In BOOT/cmdline.txt

Add "modules-load=dwc2,g_serial" to the kernel command line file, cmdline.txt in /boot and remove the "init=/usr/lib/raspi-config/" part as otherwise the first boot will resize the root partition and the USB drivers won't engage until the next boot and it will first look like the configuration failed. Note the root=PARTUUID=xxxxxxxx-yy part, don't modify that.

The resulting command line in cmdline.txt should look something close to this :

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=72d5f72a-02 rootfstype=ext4 elevator=deadline rootwait modules-load=dwc2,g_serial

Optionally, if you need networking over USB, use g_cdc instead of g_serial

Enable serial console login on USB gadget serial node

If we where on a running system it would be a matter of running

sudo systemctl enable getty@ttyGS0.service

But as we are modifying the filesystem on another computer we need to manually enable getty on ttyGS0

Go to (ROOT)/etc/systemd/system/ and create a symlink

ln -s /lib/systemd/system/getty@.service getty@ttyGS0.service

Boot with serial console

We are now done, umount the partitions and insert the SD card into your Raspberry PI and connect a USB cable to the USB port (not POWER!). Your Pi should now boot and a USB serial device should pop up on the host device after a short while, start your terminal software (minicom, picocom) and point it to the new device node, probably /dev/ttyACM0. Check dmesg.

Press ENTER and you should be presented with a login prompt.

Problems ?

First, if possible, connect a monitor and see what happens.

Raspbian is quite slow to boot on the Pi Zero, it might take a while before the serial device appers as it will be loaded quite late in the boot stage.

If a message, "udc-core: couldn't find an available UDC - added [g_serial] to list of pending drivers." is printed, then you didn't enable the dwc2 driver properly.


Raspberry Pi
Serial console