Raspberry Pi Headless Setup with SSH and WLAN

You don’t want to connect a monitor and keyboard to setup and run a headless Raspberry Pi? It needs only a few simple steps, to get a fully headless setup of Raspbian OS Bookworm on your Raspberry Pi.

My tutorial was executed on Fedora Desktop.

First, write the downloaded raspbian image to the SD-Card

Be careful, to write the image to the right device!

xz -d 2023-10-10-raspios-bookworm-armhf-lite.img.xz
sudo dd if=2023-10-10-raspios-bookworm-armhf-lite.img of=/dev/mmcblk0 bs=4M

Reinsert the SD-Card and create / add some configuration files

Create a WLAN Configuration File

With this config, your Raspi can autoconnect to your home wifi on first boot. This is necessary to connect via ssh.

Before Raspbian Bullseye:

With older versions of Raspbian OS wpa_supplicant is used to configure the wifi connection.
You need a wpa_supplicant template file with your WiFi Settings.

vi wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=DE
network={
ssid="YOURSSID"
psk="YOURPASSWORD"
scan_ssid=1
}

Copy the earlier created WLAN config file:

cp wpa_supplicant.conf /run/media/juergen/bootfs
Raspbian OS Bullseye or newer versions:

Rasbian OS now uses NetworkManager to manage wifi connections. You can use the following template to create an nmconnection-file.

cat <<your_ssid>>.nmconnection           
[connection]
id=<<your_ssid>>
type=wifi
interface-name=wlan0

[wifi]
mode=infrastructure
ssid=<<your_ssid>>

[wifi-security]
auth-alg=open
key-mgmt=wpa-psk
psk=<<your_wlan_password>>

[ipv4]
method=auto

[ipv6]
addr-gen-mode=default
method=auto

[proxy]

Copy the nmconnection file with sudo to the sd-card rootfs:

sudo cp <<your_ssid>>.nmconnection /run/media/juergen/rootfs/etc/NetworkManager/system-connections

Additional configs

Create an empty file called ‘ssh’ on the boot partition to enable ssh daemon:

touch /run/media/juergen/bootfs/ssh

Change the hostname:

sudo sed -i 's/raspberrypi/<<your_hostname>>/g' /run/media/juergen/rootfs/etc/hosts
sudo sed -i 's/raspberrypi/<<your_hostname>>/g' /run/media/juergen/rootfs/etc/hostname

Since Raspbian (Debian) Bullseye there is no default user on the system, so we have to create our own. Openssl encrypts the clear text password with SHA512

echo -n juergen: > /run/media/juergen/bootfs/userconf
echo "your_password_in_clear_text" | openssl passwd -6 -stdin >> /run/media/juergen/bootfs/userconf

Unmount the SD-Card partitions:

sudo umount /run/media/juergen/bootfs
sudo umount /run/media/juergen/rootfs

Now insert SD-Card in Raspberry Pi and boot. Then you can ping the Pi and login with SSH:

If the screen gets black during boot process, then you must disable display auto configuration in config.txt

sed -i 's/dtoverlay=vc4-kms-v3d/#dtoverlay=vc4-kms-v3d/g' /run/media/juergen/bootfs/config.txt

First time boot of the new installed Raspberry Pi

Please keep in mind, that the first boot takes longer because of resizing partition and ssh key creation.

ping yourhostname
ssh juergen@yourhostname

Login to your new installed Raspberry Pi via SSH and continue the setup.

Change the Locale and Keyboard layout if needed:

Set 'DE' for German:
sudo sed -i 's/XKBLAYOUT="gb"/XKBLAYOUT="de"/g' /etc/default/keyboard

Change your Locale:
sudo dpkg-reconfigure locales

and your timezone:
sudo dpkg-reconfigure tzdata

Install updates

sudo apt update
sudo apt dist-upgrade
sudo apt upgrade

Update firmware

sudo rpi-update

# Get current installed firmware version
sudo vcgencmd version
Oct 17 2023 15:42:39 
Copyright (c) 2012 Broadcom
version 30f0c5e4d076da3ab4f341d88e7d505760b93ad7 (clean) (release) (start)

Optionally

Optionally install some elementary packages to make life easier 😉

sudo apt install vim dnsutils

If you want to activate autologin or some other devices, use raspi-config tool

sudo raspi-config

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.