RPi setup before first boot

Sometimes you just want a quick reference on how to set up UART, wifi credentials and ssh on the RPi before the initial boot.

Download and flash

Download and flash image to the SD card.

Modify the boot partition

Once the flashing is done, mount the SD card on your laptop. This will expose the boot partition. The script was tested on Ubuntu 20.04 and Rpi 3 Model b. The OS used is Raspberry Pi OS based on Debian Buster.

To run the commands you’ll have to prepare the following:

  • wifi credentials (WIFI_SSID WIFI_PASSWORD)
  • country code (WIFI_COUNTRY_CODE)
WIFI_SSID=""
WIFI_PASSWORD=""
WIFI_COUNTRY_CODE=""

# Enable uart
echo "enable_uart=1" >> /media/$USER/boot/config.txt

# To enable ssh
touch /media/$USER/boot/ssh

cat > /media/$USER/boot/wpa_supplicant.conf << EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=$WIFI_COUNTRY_CODE

network={
        ssid="$WIFI_SSID"
        psk="$WIFI_PASSWORD"
        }
EOF

Once this was executed, put the SD card in the RPi and power it up.

Accessing the RPi

UART

If the UART cable is correctly connected to the RPi pins (Ground UART TX UART RX) and you’ve started the serial terminal emulator (i.e. sudo picocom -b 112500 /dev/ttyUSB0) you should see the login prompt.

SSH

This last step isn’t full proof and it’s success heavily depends on your router. If you’re lucky the router will auto resolve the hostname of the RPi. As a result you can do:

ssh pi@raspberrypi
# password: raspberry
Written on September 24, 2020