User avatar
RichardBronosky
Posts: 82
Joined: Thu Feb 14, 2013 5:38 pm
Location: Atlanta, GA, USA

Creating /boot/ssh and /boot/wpa_supplicant.conf (for a headless setup) works, but how?

Tue Nov 28, 2017 6:55 am

Have you ever wondered HOW this works? I did. And it was quite the rabbit hole to go down.
  • First I Googled and found the documentation about creating the file /boot/ssh
  • I never managed to find any official documentation on creating the file /boot/wpa_supplicant.conf (Though I did find lots of references that stated that it did work.)
  • Then I studied https://github.com/RPi-Distro/pi-gen extensively.
  • But I couldn't find anything that looked for /boot/ssh or /boot/wpa_supplicant.conf
  • Then I started looking for packages that may contain the implementation.

    Code: Select all

    ~/src/pi-gen $ cat $(find stage[012] -type f -name '*package*') | tr " " "\n" | grep 'pi' | sort
    libraspberrypi-bin
    libraspberrypi-dev
    libraspberrypi-doc
    libraspberrypi0
    pi-bluetooth
    python-rpi.gpio
    raspberrypi-bootloader
    raspberrypi-net-mods
    raspberrypi-sys-mods
    raspi-config
    raspi-copies-and-fills
    rpi-update
    
  • I grabbed all the matching packages I could find in https://archive.raspberrypi.org/debian/pool/main/r/
  • I got lucky with raspberrypi-net-mods and raspberrypi-sys-mods
I finally discovered that it is a bit of systemd magic that makes each happen.

raspberrypi-net-mods.service

Code: Select all

[Unit]
Description=Copy user wpa_supplicant.conf
ConditionPathExists=/boot/wpa_supplicant.conf
Before=dhcpcd.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/mv /boot/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf
ExecStartPost=/bin/chmod 600 /etc/wpa_supplicant/wpa_supplicant.conf

[Install]
WantedBy=multi-user.target
Notice the ConditionPathExists and ExecStart! That's pretty clever.

Similarly, but slightly more clever.

raspberrypi-sys-mods.sshswitch.service

Code: Select all

[Unit]
Description=Turn on SSH if /boot/ssh is present
ConditionPathExistsGlob=/boot/ssh{,.txt}
After=regenerate_ssh_host_keys.service

[Service]
Type=oneshot
ExecStart=/bin/sh -c "update-rc.d ssh enable && invoke-rc.d ssh start && rm -f /boot/ssh ; rm -f /boot/ssh.txt"

[Install]
WantedBy=multi-user.target
Now that I've uncovered this, I can finally finish the project I started with this thread nearly 5 years ago. It's been a long journey of discovery, this $35 whim I indulged in back in May 2012
-- Bruno Bronosky
1x RPi 1 (B)
1x RPi 1+ (B)
2x RPi 2
11x RPi 3
4x RPi Zero W
2x RPi camera module
3x AIY Voice kits
https://github.com/RichardBronosky/pi-gen-extender
https://github.com/RichardBronosky/raspbian-boot-setup


User avatar
RichardBronosky
Posts: 82
Joined: Thu Feb 14, 2013 5:38 pm
Location: Atlanta, GA, USA

Re: Creating /boot/ssh and /boot/wpa_supplicant.conf (for a headless setup) works, but how?

Tue Jul 02, 2019 7:59 pm

That simply states that it works. It doesn't tell you how/why. My post is about the technical implementation.
-- Bruno Bronosky
1x RPi 1 (B)
1x RPi 1+ (B)
2x RPi 2
11x RPi 3
4x RPi Zero W
2x RPi camera module
3x AIY Voice kits
https://github.com/RichardBronosky/pi-gen-extender
https://github.com/RichardBronosky/raspbian-boot-setup

User avatar
DougieLawson
Posts: 36832
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Creating /boot/ssh and /boot/wpa_supplicant.conf (for a headless setup) works, but how?

Tue Jul 02, 2019 8:19 pm

RichardBronosky wrote:
Tue Jul 02, 2019 7:59 pm
That simply states that it works. It doesn't tell you how/why. My post is about the technical implementation.
You can create both of those on Windows with Notepad++.

/boot/ssh can be an empty file.
/boot/wpa_supplicant.conf needs to look like

Code: Select all

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
        ssid="your WiFi SSID goes here"
        psk="your WiFi password goes here"
}
Note: change the country=?? line to have your ISO 2-char country code.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Raspbian”