Raspberry Pi - Headless Installation

From Alpine Linux
Jump to: navigation, search


This wiki describes a method to prep an sd card for a raspberry pi so that the pi will connect to your local wifi on first boot and allow password-less root login over ssh to continue setup. This allows you to setup a raspberry pi without connecting a keyboard or monitor.

This method assumes that you are working from a linux machine and are already familiar with prepping an sd card with Alpine Linux for a raspberry pi. The basic procedure is to first create the sd card as normal but then also copy a one-time use overlay file into the root of the sd card. This file gets extracted on boot and creates /etc/.default_boot_services and replaces /bin/hostname with a custom shell script which will connect to your wifi network and start sshd.

Note that this is a hacky and insecure procedure though I've tried to put things back the way I found them after the script runs. Once you ssh in, run through the setup-alpine script, and run lbu commit -d the overlay will be replaced and the system will be back to a normal configuration after a reboot.

I've tested this with Alpine Linux 3.11.2 aarch64 on the Pi4 and armhf on the Pi Zero W. The Pi Zero W takes about 2 minutes and 20 seconds before it responds to a ping, though if you watch it boot it looks like it pulls an IP address and gets to the login prompt in about 1 minute and 17 seconds. The more powerful Pi4 comes up faster.

Create Overlay File

It is assumed that you have already extracted the Alpine Linux archive to the sd card. In a working directory create the following directory/file structure:

mkdir bin etc touch bin/hostname etc/.default_boot_services chmod +x bin/hostname

[sodface@sodpro working]$ tree -a ├── bin │   └── hostname └── etc └── .default_boot_services

Edit bin/hostname as follows, updating ssid and psk to match your local network:

Contents of bin/hostname

#!/bin/sh apk add wpa_supplicant cat <<EOF >> /etc/network/interfaces auto lo iface lo inet loopback auto wlan0 iface wlan0 inet dhcp hostname localhost EOF cat <<EOF >> /etc/wpa_supplicant/wpa_supplicant.conf network={ ssid="Changeme" key_mgmt=WPA-PSK psk="Changeme" } EOF service wpa_supplicant start service networking start rm /var/run/ifstate /sbin/setup-sshd -c openssh cp /etc/ssh/sshd_config /etc/ssh/sshd_config.orig cat <<EOF >> /etc/ssh/sshd_config PermitEmptyPasswords yes PermitRootLogin yes EOF service sshd restart mv /etc/ssh/sshd_config.orig /etc/ssh/sshd_config rm /bin/hostname ln -s /bin/busybox /bin/hostname

Create the overlay file:

[sodface@sodpro working]$ tar czvf localhost.apkovl.tar.gz bin/ etc/ bin/ bin/hostname etc/ etc/.default_boot_services

Copy the localhost.apkovl.tar.gz into the root of the sd card, unmount, insert into the pi and boot. The above script uses dhcp on the wlan0 interface so you'll need to get into your dhcp server and watch for a new lease to determine the IP address assigned to the pi. Alternatively, you could change the script to use a static ip though I haven't tested that so I don't have verified syntax to provide at the time I'm writing this.

Once you know the IP address, you should be able to ssh to the pi as root without a password and run the setup-alpine script as usual. I would also recommend installing rng-tools and updating startup services:

apk add rng-tools rc-update add rngd boot rc-update add wpa_supplicant boot rc-update del networking boot rc-update -u

Be sure to create a new overlay file (deleting the old one) before the first reboot:

lbu commit -d reboot